如何在 C# 中移动 TrackBar 以响应鼠标事件?

发布于 2024-10-08 21:06:24 字数 499 浏览 8 评论 0原文

这可能是一个 n00b 查询。我需要根据鼠标按下事件更改轨迹栏值。我的实现如下:

private void MoveTrackBarToMouseClickLocation(TrackBar a_tBar, int a_mouseX)
{
    // Jump to the clicked location

        double dblValue;
        dblValue = ((double)a_mouseX / (double)a_tBar.Width) * (a_tBar.Maximum -    a_tBar.Minimum);
        a_tBar.Value = Convert.ToInt32(dblValue);
}

该部分工作正常。当按下鼠标按钮时,我无法使滚动正常工作。例如,如果我单击轨迹栏,然后按下鼠标说出值 50,我希望能够在鼠标按下时向右或向左滚动(从值 = 50 开始)。

我希望我已经把我的小问题说清楚了。 任何帮助表示赞赏。 谢谢

This is probably a n00b query. I have a need where I want to change the trackbar value based on a mouse down event. This I achieved as follows:

private void MoveTrackBarToMouseClickLocation(TrackBar a_tBar, int a_mouseX)
{
    // Jump to the clicked location

        double dblValue;
        dblValue = ((double)a_mouseX / (double)a_tBar.Width) * (a_tBar.Maximum -    a_tBar.Minimum);
        a_tBar.Value = Convert.ToInt32(dblValue);
}

That part works fine. I am having trouble getting the scroll working while the mouse button is pressed. e.g. If I click on the trackbar and it takes me to say value 50 with the mouse down, I want to be able to scroll right or left (from value=50) while that mouse is down.

I hope I have made my small issue clear.
Any help is appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-10-15 21:06:24

您需要在 中执行代码MouseMove 事件,以及 MouseDown 事件。

当鼠标移动其中之一时,会发生此事件按钮被按住。相比之下,您当前处理的 MouseDown 事件仅在每次按下鼠标按钮时引发一次。这就是为什么当用户移动鼠标时 TrackBar 不移动,但在第一次按下按钮时正常工作的原因。

您没有显示连接事件处理程序和/或调用 MoveTrackBarToMouseClickLocation 函数的代码,因此这是我能得到的最具体的内容。但如果您已经成功连接了 MouseDown 事件,那么这应该是一个简单的修复。

You need to execute your code in the MouseMove event, as well as the MouseDown event.

This event occurs when the mouse is moved while one of the buttons is held down. In contrast, the MouseDown event that you currently handle only gets raised once each time the mouse button is pressed down. That's why the TrackBar is not moving when the user moves the mouse, but is working properly the first time the button is pressed.

You didn't show the code where you wired up the event handlers and/or call the MoveTrackBarToMouseClickLocation function, so that's as specific as I can get. But if you managed to wire up the MouseDown event already, this should be a simple fix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文