鼠标移动灵敏度

发布于 2024-08-26 00:02:27 字数 742 浏览 10 评论 0原文

我使用 MouseMove 事件来移动对象(例如标签)。

简单原理(示意图):

OnMouseMove(e MouseEventArgs)
    if e.Button == Left
        deltaX = e.X - lastX
        foreach label in labels
            label.Location.X += deltaX
        lastX = e.X

一旦标签数量增加,我开始看到标签沿着移动轨迹的痕迹。 我有类似 III II III II II III II 的东西,但想要有类似 IIII 的东西作为痕迹。我想知道鼠标何时“开始”和“停止移动”这样的事情。

我沿着水平轴移动标签。 MouseDown(设置 LastX)并继续。没有人知道什么时候停止,只有鼠标移动的感觉。当然,我可以使用 MouseUp 来知道移动何时结束,但是如果用户保持按下按钮并停止移动,我想反映最新的标签位置。

有没有办法防止这种痕迹呢?

尝试过

label.Visible = false
label.Location.X += deltaX
label.Visible = true

没有帮助。

Parent.SuspendLayout 和 ResumeLayout 没有多大帮助,因为我需要在每次鼠标移动时执行此操作,所以没有任何效果。

I use MouseMove event to move objects(say labels).

simple principle(schematic):

OnMouseMove(e MouseEventArgs)
    if e.Button == Left
        deltaX = e.X - lastX
        foreach label in labels
            label.Location.X += deltaX
        lastX = e.X

Once the labels number increase, I start to see the labels traces along the moving trajectory.
I have something like I I I II III II I I III II, but want to have something like I I I I as traces. I'd like to know when the mouse "starts" and "stops to move" something like this.

I move labels along a horizontal axe. MouseDown(set LastX) and go on. Nobody knows when stops, only mouse move sensibility. Surely I can use MouseUp to know when the movement ends, but however if user maintains the button down and stop moving I want to reflect the latest label position.

Is there a way to prevent this kind of traces?

tried

label.Visible = false
label.Location.X += deltaX
label.Visible = true

does not help.

parent.SuspendLayout and ResumeLayout does not help a lot, because I need doing this at every mouse movement, so any effect.

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

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

发布评论

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

评论(1

落墨 2024-09-02 00:02:27

编辑:我刚刚注意到您对鼠标何时停止的编辑。您可以使用计时器来帮助您。将其设置为您想要更新位置的时间间隔,并在时间过去后自动重置。鼠标按下时启动,鼠标松开时停止。当计时器到期时,更新标签的位置。

--上下文的原始答案:--

是的,您可以暂停在父控件上绘制,移动标签,然后恢复绘制并刷新。

来自 这个问题

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

private const int WM_SETREDRAW = 11; 

public static void SuspendDrawing(Control parent)
{
    SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
}

public static void ResumeDrawing(Control parent)
{
    SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
    parent.Refresh();
}

示例用法:

private void OnMouseMove(MouseEventArgs e)
{
    int deltaX = e.X - lastX;
    // Suspend drawing here

    foreach (Label label in labels)
    {
        label.Location.X += deltaX;
    }

    lastX = e.X;
    // Resume drawing here
}

编辑: 如果您只想在差异大于 n 像素时显示位置的变化,那么您应该使用毕达哥拉斯定理来计算旧位置和新位置之间的距离,并仅当差值大于 n 时才移动它。当鼠标按钮出现时,将标签移动到根据鼠标应有的位置。

伪代码:

difference = Math.Sqrt(x * x, y * y);

if (difference > n)  // n is whatever number you want
{
     // move the labels
     // set the old position to the new position
}

Edit: I've just noticed your edit about when the mouse stops. You could use a timer to help you. Have one set to the interval you want to update the positions on and auto reset when it elapses. Start it on mouse down and stop it on mouse up. When the timer elapses, update the locations of the labels.

--Original answer for context:--

Yes, you can suspend drawing on the parent control, move the labels, and then resume drawing and refresh.

From this SO question:

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

private const int WM_SETREDRAW = 11; 

public static void SuspendDrawing(Control parent)
{
    SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
}

public static void ResumeDrawing(Control parent)
{
    SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
    parent.Refresh();
}

Example Usage:

private void OnMouseMove(MouseEventArgs e)
{
    int deltaX = e.X - lastX;
    // Suspend drawing here

    foreach (Label label in labels)
    {
        label.Location.X += deltaX;
    }

    lastX = e.X;
    // Resume drawing here
}

Edit: If you only want to show the change in position if the difference is greater than n pixels, then you should use the Pythagorean Theorem to calculate the distance between the old position and the new position and only move it if the difference is greater than n. When the mouse button comes up, move the labels to the location they are supposed to be according to the mouse.

Pseudo-code:

difference = Math.Sqrt(x * x, y * y);

if (difference > n)  // n is whatever number you want
{
     // move the labels
     // set the old position to the new position
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文