Unusual dragging issue

£可爱£侵袭症+ 提交于 2019-12-24 07:36:10

问题


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

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.

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.

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).


回答1:


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.

Suggested solution:

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.

UPDATE:

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.



来源:https://stackoverflow.com/questions/8579521/unusual-dragging-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!