如何检测在 winforms 控件上按住的鼠标按钮?
我想实现一个自定义滚动条,但希望它像标准滚动条一样工作。 因此,我需要检测鼠标按钮是否按住栏末端的向上或向下箭头,以便用户可以使用栏末端滚动。
如何检测按钮是否被按住?
MouseDown 仅在第一次按下按钮时触发一次。 当释放按钮时,MouseUp 将触发,但是有没有比在 MouseDown 和 MouseUp 之间定期触发某种计时器更好的方法呢?
I want to implement a custom scrollbar but want it to work like the standard one. So I need to detect that the mouse button is held down over either the up or down arrow at the ends of the bar so that the user can scroll using the bar ends.
How can I detect that the button is being held?
MouseDown only fires once when the button is first pressed. MouseUp will fire when the button is released but is there a better way than to have some sort of timer that triggers periodically between MouseDown and MouseUp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不会比使用计时器做得更好(尽管我可能会感到惊讶)。
我之前出于同样的目的使用过 Timer,它们的工作原理通常是这样的:
Timer
Start()
,使用按钮按下时的间隔
约为200
毫秒。 此时鼠标也被捕获。Tick
都会产生一次滚动。 (第一个Tick
还将间隔更改为 ~25
ms)Timer.Stop()
s。Interval
从200
更改为25
意味着他们必须按住鼠标一段时间,但一旦这样做,滚动行动发生得更快/更顺利。“按住”按钮实际上不是一个事件,它更多的是一种状态(即它不会在特定时间点发生)。
I don't think you're going to be able to do better than using a
Timer
(although I may be surprised).I've used
Timer
s before for the same purpose, and they work, usually like this:Timer
Start()
s, using anInterval
of ~200
ms when the button goes down. The mouse is alsoCapture
d at this point.Tick
effects a scroll. (The firstTick
also changes the interval to ~25
ms)Timer.Stop()
s.The changing of the
Interval
from200
to25
means that they have to hold the mouse down for a while, but once they do, the scrolling action happens more quickly/smoothly.The button being "held" really isn't an event, it's more of a state (i.e. it doesn't occur at a specific point in time).