MouseWheelDown 事件连续触发多次
Windows 7 上的 Delphi 7
我想使用鼠标滚轮向下滚动列表 (TElTree)...所以我开始在 ElTree 的 OnMouseWheelDown 事件中编写一些代码。然后我注意到它一次滚动 2 行。控制面板中的鼠标滚轮设置设置为一次仅滚动 1 行。如果我在事件处理程序中放置一个断点,我会发现事件处理程序本身被快速连续执行两次......为什么?如何确保它只执行一次(请提供代码)?
Delphi 7 on Windows 7
I want to scroll down a list (TElTree) using the mouse wheel... so I start writing some code in the ElTree's OnMouseWheelDown event. Then I notice that it's scrolling 2 rows at once. The mouse wheel settings in Control Panel are set to scroll only 1 line at a time. If I place a breakpoint inside the event handler, I discover that the event handler is itself being executed twice in quick succession.... Why? How to ensure it only executes once (code please)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当滚轮滚动时,操作系统会发送
WM_MOUSEWHEEL
消息。wParam
参数的高位字表示车轮转动了多远。如果它转动了一次“咔哒声”,则其值将为 120 或WHEEL_DELTA
。但如果您的滚轮识别出的滚动次数少于一次点击的次数,则该值可能会小于该值。正确的
WM_MOUSEWHEEL
消息处理程序需要考虑该参数,并且要么保留一个“滚动累加器”来跟踪滚轮滚动的距离,要么能够滚动少于整行。如果消息处理程序假设每条消息都表示一次完全点击滚轮,那么他们将会感到抱歉。When the wheel scrolls, the OS sends
WM_MOUSEWHEEL
messages. The high-order word of thewParam
parameter indicates how far the wheel has turned. If it has turned one "click," then its value will be 120, orWHEEL_DELTA
. But it might be less than that if your scroll wheel recognizes scrolling less than a click's worth.Correct
WM_MOUSEWHEEL
message handlers need to consider that parameter and either keep a "scrolling accumulator" to keep track of how far the wheel has scrolled or else have the ability to scroll less than a full line. Message handlers that assume that each message signifies a full click of the wheel will be sorry.解决方案非常简单:调用要执行 OnMouseWheel 的函数后,必须设置 Handled:=True ,以免多次调用该例程。例如
Solution is quite simple: After calling the function you want to perform
OnMouseWheel
you must setHandled:=True
so that the routine is not called multiple times. e.g.