异常拖动问题 [英] Unusual dragging issue

查看:286
本文介绍了异常拖动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Picturebox,用户可以向上或向下拖动。

I've got a Picturebox, which the user can drag up or down.

该程序是钢琴的复杂音乐人员编辑器,因此实现员工移动笔记的唯一方法是使用多个if语句和修改坐标。

The program in question is a complex music staff editor for a piano, and thus the only way to achieve the moving of notes on a staff is with a number of if-statements and by modifying co-ordinates.

问题是用户无法将PictureBox组件向下移动,但是当对象被拖动时,没有任何反应。该类继承自PictureBox。

The issue is that the user is unable to move the PictureBox component down, but when the object is dragged up, nothing happens. The class inherits from PictureBox.

我只想强调,PictureBox在向下拖动时工作,但向上拖动时不移动。拖动是按照间隔进行的,即PictureBox只能放置在某些位置(因此需要特定的坐标)。

I would just like to emphasise that the PictureBox works when dragged downwards, but does not move when dragged upwards. The dragging is done in intervals, i.e. the PictureBox can only be placed in certain places (hence the need for specific co-ordinates).

推荐答案

您当前的解决方案有时可能会运行,但是当您尝试拖动控件并将其重新插入到中指定的坐标(如果语句)时,可以非常频繁地调用该事件

Your current solution may work sometimes, however the event can be invoked very often when you try to drag the control and snap it back to the coordinates you specified in if statements.

建议的解决方案:

我建议您使用 MouseMove 事件包含您要拖动的控件的窗体或父项。这些值也应该是可配置的,而不是硬编码。
代码只会改变一点(比较当前的鼠标坐标而不是控件的 Left Top 属性)并且它应该正常工作。

I would suggest you to use MouseMove event in the form or parent that contains the control you want to drag. The values should also be configurable, not hardcoded. The code would change only a little (comparing current mouse coordinates instead of control's Left and Top properties) and it should work correctly.

更新:

代码,所以现在可以让您在三个地方之一( y 等于 138 148 158 )。我改变它只是一点点不要求你改变很多的代码,但我强烈建议你使用第一个方法描述:)。

I've corrected your code, so it now works allowing you to put a control in one of the three places (y equals to 138, 148 or 158). I change it only a bit not to require you to change a lot of code, but I strongly recommend you to use the first method described :).

    int currentY;
    bool isDragging = false;

    private void OnDrag(object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            //calculate Y position relative to parent
            int parentY = this.Top + e.Y;

            if (parentY < currentY)
            {
                if (parentY > 158 && parentY >= 148)
                {
                    if (this.Top != 148)
                    {
                        currentY += (this.Top - 148);
                        this.Top = 148;
                    }
                }
                else if (parentY < 148 /*&& parentY >= 138*/)
                {
                    if (this.Top != 138)
                    {
                        currentY += (this.Top - 138);
                        this.Top = 138;
                    }
                }

                //And so on

            }
            else if (parentY > currentY)
            {
                if (/*parentY <= 158 &&*/ parentY >= 148)
                {
                        currentY += (this.Top - 158);
                        this.Top = 158;
                }
                else if (parentY < 148 && parentY >= 138)
                {
                        currentY += (this.Top - 148);
                        this.Top = 148;
                }

                //And so on

            }
        }
    }

    public void MusicNote_MouseDown(object sender, MouseEventArgs e)
    {
        currentY = this.Top + e.Y;

        this.Capture = true;

        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            isDragging = true;
        }

        this.MouseMove += new MouseEventHandler(OnDrag);
    }

    public void MusicNote_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;

        this.Capture = false;

        this.MouseMove -= new MouseEventHandler(OnDrag);
    }

然而,以前的解决方案可能更符合您的要求。

The previous solution would, however, work probably more as you want it to.

这篇关于异常拖动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆