gpt4 book ai didi

delphi - TListView和鼠标滚轮滚动

转载 作者:行者123 更新时间:2023-12-03 15:00:21 26 4
gpt4 key购买 nike

我在表单中有一个 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, Shift, mwdUp);
end;

GenericMouseWheel 非常漂亮。它适用于任何带有垂直滚动条的控件。我将它与 TreeView 、 ListView 、列表框、备忘录、丰富编辑等一起使用。

您将缺少我的 ModifierKeyState 例程,但您可以替换您自己的方法来检查滚轮事件是否未被修改。您想要执行此操作的原因是,例如,CTRL+鼠标滚轮意味着缩放而不是滚动。

就其值(value)而言,它看起来像这样:

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

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

关于delphi - TListView和鼠标滚轮滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5297234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com