我使用了许多滚动控件: TTreeViews、TListViews、DevExpress cxGrids和cxTreeLists等。当鼠标滚轮旋转时,无论鼠标光标位于哪个控件上,带有焦点的控件都会收到输入。
如何将鼠标滚轮输入指向鼠标光标所在的控件?在这方面,Delphi IDE工作得非常好。
发布于 2010-02-12 20:07:54
尝试覆盖表单的MouseWheelHandler方法,如下所示(我还没有对此进行全面测试):
procedure TMyForm.MouseWheelHandler(var Message: TMessage);
var
Control: TControl;
begin
Control := ControlAtPos(ScreenToClient(SmallPointToPoint(TWMMouseWheel(Message).Pos)), False, True, True);
if Assigned(Control) and (Control <> ActiveControl) then
begin
Message.Result := Control.Perform(CM_MOUSEWHEEL, Message.WParam, Message.LParam);
if Message.Result = 0 then
Control.DefaultHandler(Message);
end
else
inherited MouseWheelHandler(Message);
end;发布于 2010-02-12 20:59:28
覆盖TApplication.OnMessage事件(或创建一个TApplicationEvents组件),并在事件处理程序中重定向WM_MOUSEWHEEL消息:
procedure TMyForm.AppEventsMessage(var Msg: tagMSG;
var Handled: Boolean);
var
Pt: TPoint;
C: TWinControl;
begin
if Msg.message = WM_MOUSEWHEEL then begin
Pt.X := SmallInt(Msg.lParam);
Pt.Y := SmallInt(Msg.lParam shr 16);
C := FindVCLWindow(Pt);
if C = nil then
Handled := True
else if C.Handle <> Msg.hwnd then begin
Handled := True;
SendMessage(C.Handle, WM_MOUSEWHEEL, Msg.wParam, Msg.lParam);
end;
end;
end;它在这里工作得很好,尽管您可能想要添加一些保护,以防止在发生意想不到的事情时递归。
发布于 2010-02-12 19:14:25
您可能会发现这篇文章很有用:send a scroll down message to listbox using mousewheel, but listbox doesn't have focus [1],它是用C#编写的,但是转换到Delphi语言应该不是太大的问题。它使用钩子来实现想要的效果。
要找出鼠标当前位于哪个组件上,可以使用FindVCLWindow函数,可以在本文中找到此函数的示例:Get the Control Under the Mouse in a Delphi application [2]。
https://stackoverflow.com/questions/2251019
复制相似问题