简体   繁体   English

拖放-在Winforms中移动标签

[英]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. 有时它也会完全消失,留下痕迹或重置为0,0位置。 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. 我曾经使这个功能100%可用,但是最近进行了一些调整之后,它又重新出现在了狗身上,我不确定如何使它起作用。

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. OnMouseMove()代码错误。 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 . 我最近遇到了这样的问题,我通过将标签的背景色设置为Color.TransparentColor.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? 您是否还尝试覆盖其父类的OnPaint方法?

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

也许你应该把重点放在像

label1.Focus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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