TListView和鼠标滚轮滚动 [英] TListView and mouse wheel scrolling

查看:647
本文介绍了TListView和鼠标滚轮滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中有一个TListView组件。这是相当长的时间,我希望用户能够滚动它,如果鼠标在组件和滚轮上滚动。我没有找到任何OnMouseWheel,OnMouseWheelDown或OnMouseWheelUp事件为TListView对象。如何做到这一点?



Regards,
evilone

解决方案

这是我的代码:

 键入
TMyListView = class(TListView)
protected
函数DoMouseWheelDown(Shift:TShiftState; MousePos:TPoint):Boolean;覆盖
函数DoMouseWheelUp(Shift:TShiftState; MousePos:TPoint):Boolean;覆盖
结束

type
TMouseWheelDirection =(mwdUp,mwdDown);

函数GenericMouseWheel(Handle:HWND; Shift:TShiftState; WheelDirection:TMouseWheelDirection):Boolean;
var
i,ScrollCount,Direction:Integer;
分页:布尔;
begin
结果:= ModifierKeyState(Shift)= []; //只响应未修改的轮子动作
如果结果然后开始
分页:= DWORD(Mouse.WheelScrollLines )= WHEEL_PAGESCROLL;
ScrollCount:= Mouse.WheelScrollLines;
case WheelDirection
mwdUp:
如果分页然后开始
方向:= SB_PAGEUP;
ScrollCount:= 1;
end else begin
方向:= SB_LINEUP;
结束
mwdDown:
如果分页然后开始
方向:= SB_PAGEDOWN;
ScrollCount:= 1;
end else begin
方向:= SB_LINEDOWN;
结束
结束
for i:= 1 to ScrollCount do begin
SendMessage(Handle,WM_VSCROLL,Direction,0);
结束
结束
结束

函数TMyListView.DoMouseWheelDown(Shift:TShiftState; MousePos:TPoint):Boolean;
begin
//不调用继承的
结果:= GenericMouseWheel(Handle,Shift,mwdDown);
结束

函数TMyListView.DoMouseWheelUp(Shift:TShiftState; MousePos:TPoint):Boolean;
begin
//不调用继承的
结果:= GenericMouseWheel(Handle,Shift,mwdUp);
结束

GenericMouseWheel 非常漂亮。它适用于任何具有垂直滚动条的控件。我使用树视图,列表视图,列表框,备忘录,丰富的编辑等。



你会丢失我的 ModifierKeyState 例程,但您可以替换自己的方法来检查车轮事件是否未被修改。你想要这样做的原因是,例如,CTRL +鼠标滚轮意味着缩放,而不是滚动。



对于它的价值,它看起来像这样: p>

 键入
TModifierKey = ssShift..ssCtrl;
TModifierKeyState = TModifierKey的集合;

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


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

解决方案

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;

这篇关于TListView和鼠标滚轮滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆