鼠标按下并向下移动

发布于 2024-09-30 03:53:57 字数 459 浏览 0 评论 0原文

有什么问题吗?

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        position_x = e.Location.X;
        position_y = e.Location.Y;
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
     int position_x_next = e.Location.X;
     int position_y_next = e.Location.Y;

     if (position_x_next < position_x)
     {
         MessageBox.Show("it moved left");
     }
    }

What's wrong with it?

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        position_x = e.Location.X;
        position_y = e.Location.Y;
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
     int position_x_next = e.Location.X;
     int position_y_next = e.Location.Y;

     if (position_x_next < position_x)
     {
         MessageBox.Show("it moved left");
     }
    }

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

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

发布评论

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

评论(5

我不会写诗 2024-10-07 03:53:57

您需要跟踪鼠标的先前位置和新位置。

创建 2 个新变量:mouseIsDownpreviousMouseX

MouseDown 处理程序中,将 mouseIsDown 设置为 true,将 previousMouseX 设置为 mouse.x并在 MouseUp 处理程序中将其设置为 false

然后,添加一个 MouseMove 处理程序来检查 previousMouseX 是否大于 mouse.x。如果是这样,则鼠标向左移动。请务必在此处更新 previousMouseX

You need to keep track of the previous position of the mouse and the new position.

Create 2 new variables, mouseIsDown and previousMouseX.

In your MouseDown handler, set mouseIsDown to true and previousMouseX to mouse.x and set it to false in a MouseUp handler.

Then, add a MouseMove handler that checks if previousMouseX is greater than mouse.x. If so, the mouse is moving left. Make sure to update previousMouseX here as well.

许一世地老天荒 2024-10-07 03:53:57

您应该检查 MouseEventArgs 每次查看按钮何时按下并捕获 X 和 Y 坐标 - 如果 X 坐标小于上次的值,您可以判断它正在向左移动。

You should be checking the MouseEventArgs every time to see when the button is down and to capture the X and Y coordinates - you can tell it is moving left if the X coordinate is less than it was the last time.

吖咩 2024-10-07 03:53:57

实际上很简单。存储它在上次移动事件中的位置(位于 e 参数中)。然后将当前位置与上一个位置进行比较。如果它的 X 坐标位于左侧,则它会向左移动。

例如,如果您想区分精确向左移动和对角线左上移动,您应该添加一个条件来检查 Y 轴的变化是否足够小以解决用户不精确的问题。

It's pretty simple actually. Store the position it was in the last move event (it's in the e parameter). Then compare the current position with the last one. If its X coordinate is to the left it's moving left.

If you want to distinguish moving exactly left from moving diagonally left-up for instance, you should add a condition to check if the variation in the Y axis is small enough to account for user imprecision.

你的他你的她 2024-10-07 03:53:57

俗气的方法:

在鼠标按下事件中设置一个标志 IsMouseDown = true。
在鼠标移动事件中检查 IsMouseDown == true
在 MouseUp 中设置 IsMouseDown = true

The cheesy way:

Set a flag in the mouse down event IsMouseDown = true.
In the mouse move event check if IsMouseDown == true
In MouseUp set IsMouseDown = true

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