如何将鼠标滚轮输入定向到光标下方进行控制而不是聚焦?
我使用了许多滚动控件:TTreeViews、TListViews、DevExpress cxGrids 和 cxTreeLists 等。当鼠标滚轮旋转时,无论鼠标光标位于哪个控件上,具有焦点的控件都会接收输入。
如何将鼠标滚轮输入定向到鼠标光标所在的任何控件? Delphi IDE 在这方面工作得非常好。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
滚动原点
使用鼠标滚轮进行操作会生成
WM_MOUSEWHEEL
消息 正在发送:鼠标滚轮的奥德赛 1)
WM_MOUSEWHEEL
消息放入前台窗口线程的消息队列中。Application.ProcessMessage
)。该消息的类型为TMsg
,它有一个hwnd
成员,指定消息所针对的窗口句柄。Application.OnMessage
事件被触发。Handled
参数True
会停止进一步处理消息(后续步骤除外)。Application.IsPreProcessMessage
方法。Application.IsHintMsg
方法。IsHintMsg
方法中处理消息。无法阻止消息进一步处理。DispatchMessage
被调用。TWinControl.WndProc
方法接收消息。此消息的类型为TMessage
,它缺少窗口(因为这是调用此方法的实例)。TWinControl.IsControlMouseMsg
方法来检查鼠标消息是否应定向到其非窗口子控件之一。WndProc
方法,请参阅步骤 10。(2) 这永远不会发生,因为WM_MOUSEWHEEL
包含其在屏幕坐标中的鼠标位置,并且IsControlMouseMsg
> 假定鼠标位置位于客户端坐标 (XE2) 中。)TControl.WndProc
方法接收消息。TControl.MouseWheelHandler< /code>,请参阅步骤 13。
TControl.WMMouseWheel
方法接收消息。WM_MOUSEWHEEL
window m消息(对系统有意义,通常对 VCL 也有意义)被转换为CM_MOUSEWHEEL
控制消息消息(仅对 VCL 有意义),它提供了方便的 VCL 的ShiftState
信息而不是系统的按键数据。MouseWheelHandler
方法。TCustomForm
,则调用TCustomForm.MouseWheelHandler
方法。CM_MOUSEWHEEL
发送到焦点控件,请参阅步骤 14。TControl.MouseWheelHandler
方法。Capture
是通过GetCaptureControl
获取的,它会检查Parent <>无
(XE2)。)MouseWheelHandler
,请参阅步骤 13.1。CM_MOUSEWHEEL
发送到控件,请参阅步骤 14。TControl.CMMouseWheel
方法接收该消息。TControl.DoMouseWheel
方法。OnMouseWheel
事件被触发。TControl.DoMouseWheelDown
或TControl.DoMouseWheelUp
。OnMouseWheelDown
或OnMouseWheelUp
事件被触发。CM_MOUSEWHEEL
发送到父控件,请参见步骤14。(我相信这违背了MSDN在上面引用中给出的建议,但这无疑是一个深思熟虑的决定可能是由开发人员制作的。)备注、观察和注意事项
在这个处理消息链中的几乎每一步都可以通过不执行任何操作来忽略,通过更改消息参数来更改,通过对其进行操作来处理,并通过设置
Handled := True
或设置Message.Result
为非零来取消。仅当某个控件具有焦点时,应用程序才会收到此消息。但即使当
Screen.ActiveCustomForm.ActiveControl
被强制设置为nil
时,VCL 也会通过TCustomForm.SetWindowFocus
确保焦点控件,默认为以前活跃的形式。 (使用Windows.SetFocus(0)
,实际上永远不会发送消息。)由于
IsControlMouseMsg
2) 中的错误,TControl
只有在捕获了鼠标后才能接收WM_MOUSEWHEEL
消息。 这可以通过设置Control.MouseCapture := True
手动实现,但您必须采取特别注意迅速释放该捕获,否则会产生不需要的副作用,例如需要不必要的额外单击才能完成某件事。此外,鼠标捕获 通常仅发生在鼠标按下和鼠标松开事件之间,但不一定必须应用此限制。但即使消息到达控件,它也会被发送到其MouseWheelHandler
方法,该方法只是将其发送回表单或活动控件。因此,默认情况下,非窗口 VCL 控件永远不能对消息进行操作。我相信这是另一个错误,否则为什么所有的轮子处理都在TControl
中实现?为此,组件编写者可能已经实现了自己的MouseWheelHandler
方法,无论采用什么解决方案来解决这个问题,都必须注意不要破坏这种现有的自定义。能够使用滚轮滚动的原生控件,例如
TMemo
、TListBox
、TDateTimePicker
、TComboBox
、TTreeView
、TListView
等都是由系统自己滚动的。默认情况下,向此类控件发送CM_MOUSEWHEEL
没有任何效果。这些子类控件由于CallWindowProc
,VCL 在TWinControl.DefaultHandler
中负责处理>。奇怪的是,这个例程在调用 CallWindowProc 之前不会检查 Message.Result,并且一旦发送消息,就无法阻止滚动。该消息将返回其Result
集,具体取决于控件是否通常能够滚动或控件的类型。 (例如,TMemo
返回<> 0
,而TEdit
返回0
。)它是否实际滚动了对消息结果没有影响。VCL 控件 依赖于
TControl
和TWinControl
中实现的默认处理,如上所述。它们作用于DoMouseWheel
、DoMouseWheelDown
或DoMouseWheelUp
中的滚轮事件。据我所知,VCL 中没有任何控件重写MouseWheelHandler
来处理滚轮事件。纵观不同的应用程序,滚轮滚动行为作为标准似乎并没有统一的规定。例如:MS Word 滚动悬停的页面,MS Excel 滚动聚焦的工作簿,Windows Eplorer 滚动聚焦的窗格,网站实现滚动行为的方式各不相同,Evernote 滚动悬停的窗口,等等...而 Delphi 的自己的 IDE 通过滚动聚焦窗口以及悬停窗口来将所有内容置于顶部,除非悬停在代码编辑器上,然后当您滚动时代码编辑器窃取焦点 (XE2)。
幸运的是,微软至少提供了用户体验指南对于基于 Windows 的桌面应用程序:
所以问题中要求只滚动悬停的控件有足够的理由,但Delphi的开发人员并没有让它很容易实现。
结论和解决方案
首选解决方案是一种没有子类化窗口或针对不同窗体或控件的多种实现的解决方案。
为了防止获得焦点的控件滚动,该控件可能不会接收
CM_MOUSEWHEEL
消息。因此,任何控件的MouseWheelHandler
都可能不会被调用。因此,WM_MOUSEWHEEL
可能不会发送到任何控件。因此,唯一需要干预的地方是TApplication.OnMessage
。此外,消息可能无法从中逃脱,因此所有处理都应在该事件处理程序中进行,并且当绕过所有默认的 VCL 轮处理时,将考虑所有可能的情况。让我们从简单开始吧。当前悬停的启用窗口是通过
WindowFromPoint
。通过
FindControl
我们得到了一个参考到VCL控制。如果结果为nil
,则悬停的窗口不属于应用程序的进程,或者它是VCL 未知的窗口(例如下拉的TDateTimePicker
)。在这种情况下,消息需要转发回 API,我们对其结果不感兴趣。当窗口是 VCL 控件时,将考虑按特定顺序调用多个消息处理程序。当鼠标位置上存在启用的非窗口控件(类型为
TControl
或后代)时,它首先应该收到CM_MOUSEWHEEL
消息,因为该控件肯定是前台控件。该消息由WM_MOUSEWHEEL
消息构造并转换为其 VCL 等效项。其次,必须将WM_MOUSEWHEEL
消息发送到控件的DefaultHandler
方法,以允许对本机控件进行处理。最后,当没有先前的处理程序处理该消息时,必须再次将 CM_MOUSEWHEEL 消息发送到控件。最后两个步骤不能以相反的顺序进行,因为例如滚动框上的备忘录也必须能够滚动。当窗口捕获鼠标时,所有滚轮消息都应发送给它。由
GetCapture 检索的窗口
确保是当前进程的窗口,但不一定是VCL控件。例如,在拖动操作期间,会创建一个临时窗口(请参阅TDragObject.DragHandle
)接收鼠标消息。所有消息?不,WM_MOUSEWHEEL
没有发送到捕获窗口,所以我们必须重定向它。此外,当捕获窗口不处理该消息时,应该进行所有其他先前介绍的处理。这是 VCL 中缺少的一个功能:在拖动操作期间转动时,确实会调用 Form.OnMouseWheel,但获得焦点或悬停的控件不会收到该消息。这意味着,例如,无法将文本拖到备忘录的内容中超出备忘录可见部分的位置。这基本上完成了工作,并且它是下面介绍的单元的基础。要使其正常工作,只需将单元名称添加到项目中的 use 子句之一即可。它具有以下附加功能:
MouseWheelHandler
方法的控件类。TApplicationEvents
对象置于所有其他对象之前。OnMessage
事件分派给所有其他TApplicationEvents
对象。ScrollAnywhere.pas
免责声明:
此代码有意不滚动任何内容,它仅为 VCL 的
OnMouseWheel*
事件准备消息路由以获得适当的机会被解雇。此代码未在第三方控件上进行测试。当VclHandlingAfterHandled
或VclHandlingAfterUnhandled
设置为True
时,鼠标事件可能会被触发两次。在这篇文章中,我提出了一些主张,并且我认为 VCL 中存在三个错误,但是,这都是基于研究文档和测试的。请测试这个单元并评论发现和错误。我对这个相当长的答案表示歉意;我根本就没有博客。1) 厚颜无耻的命名取自A Key's Odyssey
2) 请参阅我的质量中心错误报告#135258
3) 请参阅我的质量中心错误报告#135305
Scrolling origins
An action with the mouse wheel results in a
WM_MOUSEWHEEL
message being sent:A mouse wheel's odyssey 1)
WM_MOUSEWHEEL
message into the foreground window’s thread’s message queue.Application.ProcessMessage
). This message is of typeTMsg
which has ahwnd
member designating the window handle the message is ment for.Application.OnMessage
event is fired.Handled
parameterTrue
stops further processing of the message (except for the next to steps).Application.IsPreProcessMessage
method is called.PreProcessMessage
method is called, which does nothing by default. No control in the VCL has overriden this method.Application.IsHintMsg
method is called.IsHintMsg
method. Preventing the message from further processing is not possible.DispatchMessage
is called.TWinControl.WndProc
method of the focused window receives the message. This message is of typeTMessage
which lacks the window (because that is the instance this method is called upon).TWinControl.IsControlMouseMsg
method is called to check whether the mouse message should be directed to one of its non-windowed child controls.WndProc
method, see step 10. (2) This will never happen, becauseWM_MOUSEWHEEL
contains its mouse position in screen coordinates andIsControlMouseMsg
assumes a mouse position in client coordinates (XE2).)TControl.WndProc
method receives the message.CM_MOUSEWHEEL
message and is send toTControl.MouseWheelHandler
, see step 13.TControl.WMMouseWheel
method receives the message.WM_MOUSEWHEEL
window message (meaningful to the system and often to the VCL too) is converted to aCM_MOUSEWHEEL
control message (meaningful only to the VCL) which provides for the convenient VCL'sShiftState
information instead of the system's keys data.MouseWheelHandler
method is called.TCustomForm
, then theTCustomForm.MouseWheelHandler
method is called.CM_MOUSEWHEEL
is sent to the focused control, see step 14.TControl.MouseWheelHandler
method is called.Capture
is gotten withGetCaptureControl
, which checks forParent <> nil
(XE2).)MouseWheelHandler
is called, see step 13.1.CM_MOUSEWHEEL
is sent to the control, see step 14.TControl.CMMouseWheel
method receives the message.TControl.DoMouseWheel
method is called.OnMouseWheel
event is fired.TControl.DoMouseWheelDown
orTControl.DoMouseWheelUp
is called, depending on the scroll direction.OnMouseWheelDown
or theOnMouseWheelUp
event is fired.CM_MOUSEWHEEL
is sent to the parent control, see step 14. (I believe this is against the advice given by MSDN in the quote above, but that undoubtedly is a thoughtful decision made by the developers. Possibly because that would start this very chain al over.)Remarks, observations and considerations
At almost every step in this chain of processing the message can be ignored by doing nothing, altered by changing the message parameters, handled by acting on it, and canceled by setting
Handled := True
or settingMessage.Result
to non-zero.Only when some control has focus, this message is received by the application. But even when
Screen.ActiveCustomForm.ActiveControl
is forcefully set tonil
, the VCL ensures a focused control withTCustomForm.SetWindowFocus
, which defaults to the previously active form. (WithWindows.SetFocus(0)
, indeed the message is never sent.)Due to the bug in
IsControlMouseMsg
2), aTControl
can only receive theWM_MOUSEWHEEL
message if it has captured the mouse. This can manually be achieved by settingControl.MouseCapture := True
, but you have to take special care of releasing that capture expeditiously, otherwise it will have unwanted side effects like the need for an unnecessary extra click to get something done. Besides, mouse capture typically only takes place between a mouse down and a mouse up event, but this restriction does not necessarily have to be applied. But even when the message reaches the control, it is sent to itsMouseWheelHandler
method which just sends it back to either the form or the active control. Thus non-windowed VCL controls can never act on the message by default. I believe this is another bug, otherwise why would all wheel handling have been implemented inTControl
? Component writers may have implemented their ownMouseWheelHandler
method for this very purpose, and whatever solution comes to this question, there has to be taken care of not breaking this kind of existing customization.Native controls which are capable of scrolling with the wheel, like
TMemo
,TListBox
,TDateTimePicker
,TComboBox
,TTreeView
,TListView
, etc. are scrolled by the system itself. SendingCM_MOUSEWHEEL
to such a control has no effect by default. These subclassed controls scroll as a result of theWM_MOUSEWHEEL
message sent to the with the subclass associated API window procedure withCallWindowProc
, which the VCL takes care of inTWinControl.DefaultHandler
. Oddly enough, this routine does not checkMessage.Result
before callingCallWindowProc
, and once the message is sent, scrolling cannot be prevented. The message comes back with itsResult
set depending on whether the control normally is capable of scrolling or on the type of control. (E.g. aTMemo
returns<> 0
, andTEdit
returns0
.) Whether it actually scrolled has no influence on the message result.VCL controls rely on the default handling as implemented in
TControl
andTWinControl
, as layed out above. They act on wheel events inDoMouseWheel
,DoMouseWheelDown
orDoMouseWheelUp
. For as far I know, no control in the VCL has overridenMouseWheelHandler
in order to handle wheel events.Looking at different applications, there seems to be no conformity on which wheel scroll behaviour is the standard. For example: MS Word scrolls the page that is hovered, MS Excel scrolls the workbook that is focused, Windows Eplorer scrolls the focused pane, websites implement scroll behaviour each very differently, Evernote scrolls the window that is hovered, etc... And Delphi's own IDE tops everything by scrolling the focused window as well as the hovered window, except when hovering the code editor, then the code editor steals focus when you scroll (XE2).
Luckily Microsoft offers at least user experience guidelines for Windows-based desktop applications:
So the question's requirement to only scroll the hovered control has enough grounds, but Delphi's developers haven't made it easy to implement it.
Conclusion and solution
The preferred solution is one without subclassing windows or multiple implementations for different forms or controls.
To prevent the focused control from scrolling, the control may not receive the
CM_MOUSEWHEEL
message. Therefore,MouseWheelHandler
of any control may not be called. Therefore,WM_MOUSEWHEEL
may not be send to any control. Thus the only place left for intervention isTApplication.OnMessage
. Furthermore, the message may not escape from it, so all handling should take place in that event handler and when all default VCL wheel handling is bypassed, every possible condition is to be taken care of.Let's start simple. The enabled window which currently is hovered is gotten with
WindowFromPoint
.With
FindControl
we get a reference to the VCL control. If the result isnil
, then the hovered window does not belong to the application's process, or it is a window not known to the VCL (e.g. a dropped downTDateTimePicker
). In that case the message needs to be forwarded back to the API, and its result we are not interested in.When the window ís a VCL control, multiple message handlers are to be considered calling, in a specific order. When there is an enabled non-windowed control (of type
TControl
or descendant) on the mouse position, it first should get aCM_MOUSEWHEEL
message because that control is definitely the foreground control. The message is to be constructed from theWM_MOUSEWHEEL
message and translated into its VCL equivalent. Secondly, theWM_MOUSEWHEEL
message has to be send to the control'sDefaultHandler
method to allow handling for native controls. And at last, again theCM_MOUSEWHEEL
message has to be send to the control when no previous handler took care of the message. These last two steps cannot take place in reversed order because e.g. a memo on a scroll box must be able to scroll too.When a window has captured the mouse, all wheel messages should be sent to it. The window retrieved by
GetCapture
is ensured to be a window of the current process, but it does not have to be a VCL control. E.g. during a drag operation, a temporary window is created (seeTDragObject.DragHandle
) that receives mouse messages. All messages? Noooo,WM_MOUSEWHEEL
is not sent to the capturing window, so we have to redirect it. Furthermore, when the capturing window does not handle the message, all other previously covered processing should take place. This is a feature which is missing in the VCL: on wheeling during a drag operation,Form.OnMouseWheel
indeed is called, but the focused or hovered control does not receive the message. This means for example that a text cannot be dragged into a memo's content on a location that is beyond the visible part of the memo.This essentially does the job, and it was the basis for the unit presented below. To get it working, just add the unit name to one of the uses clauses in your project. It has the following additional features:
MouseWheelHandler
method has to be called.TApplicationEvents
object in front of all others.OnMessage
event to all otherTApplicationEvents
objects.ScrollAnywhere.pas
Disclaimer:
This code intentionally does not scroll anything, it only prepares the message routing for VCL's
OnMouseWheel*
events to get the proper opportunity to get fired. This code is not tested on third-party controls. WhenVclHandlingAfterHandled
orVclHandlingAfterUnhandled
is setTrue
, then mouse events may be fired twice. In this post I made some claims and I considered there to be three bugs in the VCL, however, that is all based on studying documentation and testing. Please do test this unit and comment on findings and bugs. I apologize for this rather long answer; I simply do not have a blog.1) Naming cheeky taken from A Key’s Odyssey
2) See my Quality Central bug report #135258
3) See my Quality Central bug report #135305
尝试像这样重写表单的
MouseWheelHandler
方法(我还没有彻底测试过):Try overriding your form's
MouseWheelHandler
method like this (I have not tested this thoroughly):覆盖 TApplication.OnMessage 事件(或创建一个
TApplicationEvents 组件)并重定向 WM_MOUSEWHEEL 消息
事件处理程序:
它在这里工作正常,尽管您可能需要添加一些保护以保持
如果发生意外情况,它不会递归。
Override the TApplication.OnMessage event (or create a
TApplicationEvents component) and redirect the WM_MOUSEWHEEL message in
the event handler:
It works fine here, though you may want to add some protection to keep
it from recursing if something unexpected happens.
您可能会发现这篇文章很有用:使用鼠标滚轮向列表框发送向下滚动消息,但列表框没有焦点[1],它是用 C# 编写的,但转换为 Delphi 应该不会有太大问题。它使用钩子来实现想要的效果。
要找出鼠标当前位于哪个组件上,可以使用 FindVCLWindow 函数,可以在本文中找到示例:在 Delphi 应用程序中通过鼠标进行控制 [2]。
[1] http: //social.msdn.microsoft.com/forums/en-US/winforms/thread/ec1fbfa2-137e-49f6-b444-b634e4f44f21/
[2] http://delphi.about.com/od /delphitips2008/qt/find-vcl-window.htm
You might find this article useful: send a scroll down message to listbox using mousewheel, but listbox doesn't have focus [1], it is written in C#, but converting to Delphi shouldn't be too big a problem. It uses hooks to accomplish the wanted effect.
To find out which component the mouse is currently over, you can use the FindVCLWindow function, an example of this can be found in this article: Get the Control Under the Mouse in a Delphi application [2].
[1] http://social.msdn.microsoft.com/forums/en-US/winforms/thread/ec1fbfa2-137e-49f6-b444-b634e4f44f21/
[2] http://delphi.about.com/od/delphitips2008/qt/find-vcl-window.htm
这是我一直在使用的解决方案:
forms<之后将
amMouseWheel
添加到表单单元的实现部分的uses子句中 /code> 单位:将以下代码保存到
amMouseWheel.pas
:This is the solution I've been using:
Add
amMouseWheel
to the uses clause of the implementation section of the unit of your form after theforms
unit:Save the following code to
amMouseWheel.pas
:我遇到了同样的问题,并通过一些小技巧解决了它,但它有效。
我不想搞乱消息,决定只调用 DoMouseWheel 方法来控制我需要的。黑客是 DoMouseWheel 是受保护的方法,因此无法从表单单元文件访问,这就是为什么我在表单单元中定义我的类:
然后我编写了 TForm1.onMouseWheel 事件处理程序:
如您所见,它搜索表单上的所有控件,而不仅仅是直系孩子,结果是从父母到孩子进行搜索。对子项进行递归搜索会更好(但需要更多代码),但上面的代码工作得很好。
要使只有一个控件响应鼠标滚轮事件,您应该在实现时始终设置 Handled:=true。例如,如果面板中有列表框,则面板将首先执行DoMouseWheel,如果它没有处理事件,则将执行listbox.DoMouseWheel。如果鼠标光标下没有控件处理 DoMouseWheel,则聚焦控件将会处理,这似乎是相当足够的行为。
I had the same problem and solved it with some little hack, but it works.
I didn't want to mess around with messages and decided just to call DoMouseWheel method to control I need. Hack is that DoMouseWheel is protected method and therefore not accessible from form unit file, that's why I defined my class in form unit:
Then I wrote TForm1.onMouseWheel event handler:
As you see, it search for all the controls on form, not only immediate children, and turns out to search from parents to children. It would be better (but more code) to make recursive search at children, but code above works just fine.
To make only one control respond to mousewheel event, you should always set Handled:=true when it's implemented. If for example you have listbox inside panel, then panel will execute DoMouseWheel first, and if it didn't handle event, listbox.DoMouseWheel will execute. If no control under mouse cursor handled DoMouseWheel, the focused control will, it seems rather adequate behavior.
仅适用于 DevExpress 控件
它适用于 XE3。尚未在其他版本上进行测试。
如果不使用DevExpress控件,则执行->发送消息
Only for using with DevExpress controls
It works on XE3. It was not tested on other versions.
if you don't use DevExpress controls, then Perform -> SendMessage
在每个可滚动控件的 OnMouseEnter 事件中添加对 SetFocus 的相应调用,
因此对于 ListBox1:
这是否达到了预期的效果?
In the OnMouseEnter event for each scrollable control add a respective call to SetFocus
So for ListBox1:
Does this achieve the desired effect?