Recent

Author Topic: TListView - mouse wheel only if focused  (Read 1667 times)

thbu

  • Newbie
  • Posts: 4
TListView - mouse wheel only if focused
« on: April 04, 2020, 01:30:48 am »
Hi,

I have a TListView inside a TScrollBox. When I'm using the mouse wheel to scroll the TScrollBox, scrolling will stop as soon as the mouse cursor is over the TListView.

Is there a way to prevent the TListView from "stealing" the mouse wheel? Ideally in the way that I can still scroll inside the TListView after I click it (make it focused)?

Thanks for any suggestions.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: TListView - mouse wheel only if focused
« Reply #1 on: April 04, 2020, 03:27:41 am »
The OnMouseWheel event has a HANDLED value you can set to TRUE;

 This may stop that effect and pass the messages to the background control, not sure but I think you could redirect the message effects to the parent control when handling this...
 
 You can experiment on that a bit..

 Most likely if you set it via to what ever the control state it , for example focused..

 handled := Not Focused; etc..
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: TListView - mouse wheel only if focused
« Reply #2 on: April 04, 2020, 04:19:52 am »
Ok, I found out how you can do it easy...

 Disable the listview while you are not in it..

 Enable := False;

 This will pass all mouse messages through it to the scrollbox

 when you click on it, it will actually go to the scroll box but this is where you can then determine to enable it again by testing the area of the scroll box you clicked to see if the list view lives there.

  when you move your mouse outside the list box the OnExit can be used to disable it again...

 of course every time you do this you will need to click twice to re-enable the Listview …

 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ScrollBox1MouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. begin
  4.   ListView1.Enabled :=  PtInRect(ListView1.BoundsRect,Point(X,Y));
  5. end;  
  6. procedure TForm1.ListView1Exit(Sender: TObject);
  7. begin
  8.   ListView1.Enabled := False;
  9. end;
  10.                                      
  11.                                                                      
  12.  
  13.  
« Last Edit: April 04, 2020, 04:27:52 am by jamie »
The only true wisdom is knowing you know nothing

thbu

  • Newbie
  • Posts: 4
Re: TListView - mouse wheel only if focused
« Reply #3 on: April 04, 2020, 10:30:47 am »
Thank you very much jamie for this suggestion.

It has a few issues:
- as you pointed out you have to click twice to get into the ListView
- the items in the ListView are drawn with gray background when it is disabled. Can this be changed easily without painting the items myself?
- PtInRect(ListView1.BoundsRect,Point(X,Y)) must be shifted if the ScrollBox is scrolled down.

It can be solved, but I'd be happy to hear if there are other ways to tackle the issue more directly.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: TListView - mouse wheel only if focused
« Reply #4 on: April 04, 2020, 11:33:57 am »
Why do you put a ListView into a ScrollBox after all? A ScrollBox is a viewport to an arrangement of controls which cover a larger area than is directly seen; it provides scrollbars to get access to the hidden controls. But a ListView has scrollbars of its own. I don't see a reason why it should be inside a ScrollBox.

thbu

  • Newbie
  • Posts: 4
Re: TListView - mouse wheel only if focused
« Reply #5 on: April 04, 2020, 01:33:18 pm »
There are other things inside the ScrollBox.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: TListView - mouse wheel only if focused
« Reply #6 on: April 04, 2020, 03:23:42 pm »
I believe you would need to override the WM_MOUSEWHEEL message via  Window Procedure Hook of the ListView.

 
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: TListView - mouse wheel only if focused
« Reply #7 on: April 04, 2020, 04:08:15 pm »
I don't understand something here..

All I need to do is ensure the ListView does not have a focus and it already does pass all the mouse messages to the parent..

 Just Ensure the ListView.Focused := False; and when you click on it , it then becomes focused and thus starts accepting the scroll wheel...

 I think your issue you need to move the focus to the scrollbox or somewhere else.
 
 Because with me If I have the ListView un-focused it works as you wish..

Let me retract that a bit, it seems I am getting some random behavior over here. I'll post back

« Last Edit: April 04, 2020, 04:23:49 pm by jamie »
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: TListView - mouse wheel only if focused
« Reply #8 on: April 04, 2020, 04:54:24 pm »
This seems to work...
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListView1MouseWheel(Sender: TObject; Shift: TShiftState;
  2.   WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  3. Const Busy:Boolean = False;
  4.   Var
  5.     OldState:Boolean;
  6. begin
  7.   Handled := Not TListView(Sender).Focused;
  8.   If Handled then
  9.   Begin
  10.   Beep;
  11.    OldState := TListview(Sender).Enabled;
  12.    TListView(Sender).Enabled := False;
  13.    MousePos := TListView(Sender).ClientToScreen(MousePos);
  14.    SendMessage(TListview(Sender).parent.handle,WM_MOUSEWHEEL,MakeLong(0,WheelDelta),
  15.    MakeLong(MousePos.X,MousePos.Y));
  16.    TListView(Sender).Enabled := OldState;
  17.   End;
  18. end;                              
  19.  

But you still need to move the focus elsewhere because it will ignore this of course if the listview gets focused.
The only true wisdom is knowing you know nothing

thbu

  • Newbie
  • Posts: 4
Re: TListView - mouse wheel only if focused
« Reply #9 on: April 06, 2020, 02:49:09 am »
This seems to work...

Thank you jamie! It does indeed work and solves my problem.

 

TinyPortal © 2005-2018