C# 拖放-在Winforms中移动标签

C# 拖放-在Winforms中移动标签,c#,winforms,C#,Winforms,我已经创建了一个标签覆盖,允许我的新控件在屏幕上轻松移动 我已经附加了下面的代码,但是当我运行应用程序或尝试移动标签时,它总是关闭。它有时也会完全消失,留下痕迹或重置到0,0位置。看一下屏幕截图 我曾经让它100%工作,但经过最近的一些调整后,它又变成了狗,我不知道如何让它工作 屏幕截图: 代码: internal sealed class DraggableLabel : Label { private bool _dragging; private int _mouseX

我已经创建了一个标签覆盖,允许我的新控件在屏幕上轻松移动

我已经附加了下面的代码,但是当我运行应用程序或尝试移动标签时,它总是关闭。它有时也会完全消失,留下痕迹或重置到0,0位置。看一下屏幕截图

我曾经让它100%工作,但经过最近的一些调整后,它又变成了狗,我不知道如何让它工作

屏幕截图:

代码:

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);
    }

}

也许你应该关注你的元素,比如

label1.Focus();

也许你应该关注你的元素,比如

label1.Focus();

我最近遇到了这样一个问题,我通过将标签的背景色设置为
color.Transparent
解决了这个问题

如果这不能解决问题,那么你应该考虑监控你的事件处理。可能是您正在注册多个鼠标移动事件,因此每个方法都会相互干扰

编辑:

您是否也尝试过重写其父类的OnPaint方法

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

我最近遇到了这样一个问题,我通过将标签的背景色设置为
color.Transparent
解决了这个问题

如果这不能解决问题,那么你应该考虑监控你的事件处理。可能是您正在注册多个鼠标移动事件,因此每个方法都会相互干扰

编辑:

您是否也尝试过重写其父类的OnPaint方法

protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
OnMouseMove()代码不正确。改为这样做:

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);
}
屏幕截图还显示表单未正确重画的证据。你没有留下任何线索来说明是什么原因造成的。

OnMouseMove()代码是错误的。改为这样做:

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);
}

屏幕截图还显示表单未正确重画的证据。你没有留下任何线索来说明是什么原因造成的。

这表明你应该使用它来防止将来发生这种事情。可能有助于您开始。这听起来与我以前的100%工作非常不同,但经过最近的一些调整后,它又被淘汰了——正如您所看到的,在您的问题中提到这一点并没有什么坏处。这表明您应该使用它来防止将来发生此类事情。可能会帮助您开始。这听起来与我以前的100%工作非常不同,但经过最近的一些调整后,它又被淘汰了。-如您所见,在您的问题中提到这一点不会有什么坏处。就是这样!谢谢你!。是的,现在我看到了一些我应该做的不同的事情!就这样!谢谢你!。是的,现在我看到了一些我应该做的不同的事情!