垂直拖动项目,Y 位置不起作用

发布于 2024-12-22 15:59:15 字数 949 浏览 2 评论 0 原文

我有一个图片框,其中显示音符的图像。我希望能够在包含它的面板内上下拖动它时根据其放置位置的相应 Y 位置来更改音高的值。使用下面的代码,音高确实发生了变化,但似乎 Y 值只是随机的,当我拖动得越高,它应该向上,我拖动得越低,它应该向上。

private void StartDrag(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = true;
                pitch = e.Y;
                this.Location = new Point(this.Location.X, pitch);
            }

        }

        private void StopDrag(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = false;
                pitch = e.Y;
            }

        }

        private void NoteDrag(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (isDragging)
            {
                this.Top = this.Top + (e.Y - this.pitch); //move in Vertical direction
            }
        }

I have a picturebox which displays an image of a music note. I want to be able to change the value of the pitch when dragging it up and down inside the panel which contains it according to the corresponding Y position of where it is dropped. Using the code below, the pitch does change, but it seems that the Y value is just random, when it should be going up the higher I drag and down the lower I drag.

private void StartDrag(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = true;
                pitch = e.Y;
                this.Location = new Point(this.Location.X, pitch);
            }

        }

        private void StopDrag(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = false;
                pitch = e.Y;
            }

        }

        private void NoteDrag(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (isDragging)
            {
                this.Top = this.Top + (e.Y - this.pitch); //move in Vertical direction
            }
        }

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

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

发布评论

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

评论(1

卷耳 2024-12-29 15:59:15

当您更改控件的位置(通过修改 this.Top)时,通过 MouseEventArgs 返回的鼠标坐标也会发生变化。您应该使用 e 参数rel="nofollow">Cursor.Position 获取绝对(屏幕)坐标,然后使用 PointToClient< 控件的 /code> 方法。这样你的坐标将独立于你的控件的位置。

为了更好地理解发生的情况,在执行所有这些操作之前,请在表单中添加两个标签,并在 NoteDrag 方法中添加类似的内容:

// show relative coordinate 
this.label1.Text = e.Y.ToString();

// show absolute coordinate 
this.label2.Text = Cursor.Position.Y.ToString();

When you change the position of your control (by modifying this.Top), mouse coordinates returned with MouseEventArgs also change. Instead of using the e parameter, you should use Cursor.Position to get absolute (screen) coordinates, and then transform them using the PointToClient method of your parent control. That way your coordinates will be independent of the position of your control.

To get a better understanding what happens, before doing all this, add two labels to your form, and add something like this inside your NoteDrag method:

// show relative coordinate 
this.label1.Text = e.Y.ToString();

// show absolute coordinate 
this.label2.Text = Cursor.Position.Y.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文