简体   繁体   中英

Drag and Drop - Moving a Label in Winforms

I've created a label override that will allow my new control to be moved around the screen easily.

I've attached the code below, but when I run the application or attempt to move the label it's always off. It also will sometimes just completely vanish, leave a trail or reset to the 0,0 location. Have a look at the screenshot.

I used to have this working 100%, but after some recent tweaking it has gone to the dogs again and I'm not sure how to get it working.

Screenshot: 尝试拖动

Code:

internal sealed class DraggableLabel : Label
{

    private bool _dragging;
    private int _mouseX, _mouseY;

    public DraggableLabel()
    {
        DoubleBuffered = true;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
      if (_dragging)
        {
            Point mposition = PointToClient(MousePosition);
            mposition.Offset(_mouseX, _mouseY);

            Location = mposition;
        }
        base.OnMouseMove(e);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
       if (e.Button == MouseButtons.Left)
        {
           _dragging = true;
           _mouseX = -e.X;
           _mouseY = -e.Y;
           BringToFront();
           Invalidate();
        }
        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (_dragging)
        {
            _dragging = false;
            Cursor.Clip = new Rectangle();
            Invalidate();
        }

        base.OnMouseUp(e);
    }

}

The OnMouseMove() code is bad. Do it like this instead:

private Point lastPos;

protected override void OnMouseMove(MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
        int dx = e.X - lastPos.X;
        int dy = e.Y - lastPos.Y;
        Location = new Point(Left + dx, Top + dy);
        // NOTE: do NOT update lastPos, the relative mouse position changed
    }
    base.OnMouseMove(e);
}

protected override void OnMouseDown(MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
        lastPos = e.Location;
        BringToFront();
        this.Capture = true;
    }
    base.OnMouseDown(e);
}

protected override void OnMouseUp(MouseEventArgs e) {
    this.Capture = false;
    base.OnMouseUp(e);
}

The screen shot also shows evidence of the form not redrawing properly. You didn't leave any clue as to what might cause that.

I had a problem like this recently, and I solved it by setting the background color of the label to Color.Transparent .

If this doesn't solve it, then you should consider monitoring your event handling. It could be that you're registering more than one mouse move event and thus each method would interfere with the other.

EDIT :

Have you also tried overriding the OnPaint method of its parent class?

protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

也许你应该把重点放在像

label1.Focus();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM