Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

delphi - TListView and mouse wheel scrolling

I have a TListView component in a form. It's quite long and I want user to able scroll it, if mouse is over the component and wheel is scrolled. I do not find any OnMouseWheel, OnMouseWheelDown or OnMouseWheelUp event for TListView object. How can I do that?

Regards, evilone

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's my code to do this:

type
  TMyListView = class(TListView)
  protected
    function DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint): Boolean; override;
    function DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint): Boolean; override;
  end;

type    
  TMouseWheelDirection = (mwdUp, mwdDown);

function GenericMouseWheel(Handle: HWND; Shift: TShiftState; WheelDirection: TMouseWheelDirection): Boolean;
var
  i, ScrollCount, Direction: Integer;
  Paging: Boolean;
begin
  Result := ModifierKeyState(Shift)=[];//only respond to un-modified wheel actions
  if Result then begin
    Paging := DWORD(Mouse.WheelScrollLines)=WHEEL_PAGESCROLL;
    ScrollCount := Mouse.WheelScrollLines;
    case WheelDirection of
    mwdUp:
      if Paging then begin
        Direction := SB_PAGEUP;
        ScrollCount := 1;
      end else begin
        Direction := SB_LINEUP;
      end;
    mwdDown:
      if Paging then begin
        Direction := SB_PAGEDOWN;
        ScrollCount := 1;
      end else begin
        Direction := SB_LINEDOWN;
      end;
    end;
    for i := 1 to ScrollCount do begin
      SendMessage(Handle, WM_VSCROLL, Direction, 0);
    end;
  end;
end;

function TMyListView.DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint): Boolean;
begin
  //don't call inherited
  Result := GenericMouseWheel(Handle, Shift, mwdDown);
end;

function TMyListView.DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint): Boolean;
begin
  //don't call inherited
  Result := GenericMouseWheel(Handle, Shift, mwdUp);
end;

GenericMouseWheel is quite nifty. It works for any control with a vertical scroll bar. I use it with tree views, list views, list boxes, memos, rich edits, etc.

You'll be missing my ModifierKeyState routine but you can substitute your own method for checking that the wheel event is not modified. The reason you want to do this is the, for example, CTRL+mouse wheel means zoom rather than scroll.

For what it's worth, it looks like this:

type
  TModifierKey = ssShift..ssCtrl;
  TModifierKeyState = set of TModifierKey;

function ModifierKeyState(Shift: TShiftState): TModifierKeyState;
const
  AllModifierKeys = [low(TModifierKey)..high(TModifierKey)];
begin
  Result := AllModifierKeys*Shift;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...