What's the easiest way to modify a Winforms track bar so that when a user clicks on it the track bar jumps to the location of the mouse?
修改Winforms轨迹栏的最简单方法是什么,以便当用户点击它时,轨迹栏会跳转到鼠标的位置?
By default the track bar moves to half way between it's current location and where the mouse was clicked.
默认情况下,轨迹栏移动到其当前位置和单击鼠标的位置之间。
Update: I'm trying to do this so that I can use the track bar on a touch-screen. With a touch-screen dragging is really hard, hence the requirement for the track bar to jump straight to where the user taps.
更新:我正在尝试这样做,以便我可以在触摸屏上使用轨道栏。触摸屏拖动非常困难,因此要求轨迹栏直接跳到用户点击的位置。
1 个解决方案
#1
I've never done it, but I would start by looking at the location of the mouse click. I could then figure out how far across the current extent of the trackbar the click was located and set the tracker's location to that spot.
我从来没有这样做过,但我会先看一下鼠标点击的位置。然后我可以计算出点击所在轨道栏的当前范围有多远,并将跟踪器的位置设置为该点。
I'd start by looking into overriding the control and manipulating the Mouse-related event functions (OnMouseDown()
, e.g.). Note that in some cases it may be important to call the base class' implementation of an overridden function, be sure to consider it on a case-by-case basis. (I just ran afoul of this, so it's fresh in my mind.)
我首先考虑覆盖控件并操纵与鼠标相关的事件函数(例如OnMouseDown())。请注意,在某些情况下,调用基类的重写函数实现可能很重要,请务必根据具体情况考虑它。 (我刚碰到这个,所以它在我的脑海里很新鲜。)
Note that you should only change the behavior of standard controls in this manner if you have a very compelling reason. They're standard because they behave in a predictable way across many hundreds/thousands of programs. Making something behave differently for just your program can violate the Principle of Lease Astonishment. The only trackbar I can think of that I expect to behave the way you describe is for some sort of media player.
请注意,如果您有非常令人信服的理由,则只应以这种方式更改标准控件的行为。它们是标准的,因为它们在数百/数千个程序中以可预测的方式运行。为你的程序制作不同的行为可能会违反租赁惊讶原则。我能想到的唯一一个轨道栏,我期望按照你描述的方式行事,就是某种媒体播放器。
Edit: Based on your comment, yeah, I think you've got a compelling reason. :)
编辑:基于你的评论,是的,我认为你有一个令人信服的理由。 :)
#1
I've never done it, but I would start by looking at the location of the mouse click. I could then figure out how far across the current extent of the trackbar the click was located and set the tracker's location to that spot.
我从来没有这样做过,但我会先看一下鼠标点击的位置。然后我可以计算出点击所在轨道栏的当前范围有多远,并将跟踪器的位置设置为该点。
I'd start by looking into overriding the control and manipulating the Mouse-related event functions (OnMouseDown()
, e.g.). Note that in some cases it may be important to call the base class' implementation of an overridden function, be sure to consider it on a case-by-case basis. (I just ran afoul of this, so it's fresh in my mind.)
我首先考虑覆盖控件并操纵与鼠标相关的事件函数(例如OnMouseDown())。请注意,在某些情况下,调用基类的重写函数实现可能很重要,请务必根据具体情况考虑它。 (我刚碰到这个,所以它在我的脑海里很新鲜。)
Note that you should only change the behavior of standard controls in this manner if you have a very compelling reason. They're standard because they behave in a predictable way across many hundreds/thousands of programs. Making something behave differently for just your program can violate the Principle of Lease Astonishment. The only trackbar I can think of that I expect to behave the way you describe is for some sort of media player.
请注意,如果您有非常令人信服的理由,则只应以这种方式更改标准控件的行为。它们是标准的,因为它们在数百/数千个程序中以可预测的方式运行。为你的程序制作不同的行为可能会违反租赁惊讶原则。我能想到的唯一一个轨道栏,我期望按照你描述的方式行事,就是某种媒体播放器。
Edit: Based on your comment, yeah, I think you've got a compelling reason. :)
编辑:基于你的评论,是的,我认为你有一个令人信服的理由。 :)