加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – TListView和鼠标滚轮滚动

发布时间:2020-12-15 10:07:44 所属栏目:大数据 来源:网络整理
导读:我在表单中有一个TListView组件.它很长,我希望用户能够滚动它,如果鼠标在组件上方并滚动滚轮.我没有为TListView对象找到任何OnMouseWheel,OnMouseWheelDown或OnMouseWheelUp事件.我怎样才能做到这一点? 问候, 恶者 解决方法 这是我的代码: type TMyListVie
我在表单中有一个TListView组件.它很长,我希望用户能够滚动它,如果鼠标在组件上方并滚动滚轮.我没有为TListView对象找到任何OnMouseWheel,OnMouseWheelDown或OnMouseWheelUp事件.我怎样才能做到这一点?

问候,
恶者

解决方法

这是我的代码:
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,mwdUp);
end;

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

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读