C#单击表单&;拖动,类

C#单击表单&;拖动,类,c#,winforms,events,mouseevent,C#,Winforms,Events,Mouseevent,我知道您可以使用表单单击并拖动 但是如果说我不想使用那个代码和我自己的代码,我该怎么做呢 这就是我到目前为止所做的: public partial class Form1 : Form { MouseDragFormMove dragForm; public Form1() { InitializeComponent(); dragForm = new MouseDragFormMove(this); dragForm.

我知道您可以使用表单单击并拖动 但是如果说我不想使用那个代码和我自己的代码,我该怎么做呢

这就是我到目前为止所做的:

public partial class Form1 : Form
{
    MouseDragFormMove dragForm;

    public Form1()
    {
        InitializeComponent();

        dragForm = new MouseDragFormMove(this);
        dragForm.AllowMouseDownDrag = true;
    }
}       
public class MouseDragFormMove
{
    private bool _status;
    public bool AllowMouseDownDrag
    {
        get { return _status; }
        set { _status = value; }
    }

    private Form parent;
    public MouseDragFormMove(Form self)
    {
        _status = false;
        parent = self;
        parent.MouseDown +=new MouseEventHandler(parent_MouseDown);
        parent.MouseUp += new MouseEventHandler(parent_MouseUp);
        parent.MouseMove +=new MouseEventHandler(parent_MouseMove);            
    }

    public void showPos()
    {
        MessageBox.Show(parent.Location.X + ", " + parent.Location.Y);
    }

    private Point CPoint;
    private Point MPoint;
    private bool isDragging;

    private void parent_MouseDown(object sender, MouseEventArgs e)
    {
        CPoint = parent.Location;
        MPoint = getMousePoint(e, CPoint);
        isDragging = true;
    }
    private void parent_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }
    private void parent_MouseMove(object sender, MouseEventArgs e)
    {
        CPoint = parent.Location;
        MPoint = getMousePoint(e, CPoint);

        if (isDragging && _status)
        {
            parent.Location = MPoint;
        }
    }
    private Point getMousePoint(MouseEventArgs e, Point FP)
    {
        int x = FP.X + (e.Location.X * 2);
        int y = FP.Y + (e.Location.Y * 2);

        return new Point(x, y);
    }
}

我做错了什么?我不能让它工作。此外,它也会闪烁

问题是您使用的鼠标点是相对于窗体的,而不是相对于屏幕的。请参阅下面我的解决方案。我正在使用
Cursor.Position
获取鼠标屏幕位置

public class MouseDragFormMove
{
    private bool _status;
    public bool AllowMouseDownDrag
    {
        get { return _status; }
        set { _status = value; }
    }

    private Form parent;
    public MouseDragFormMove(Form self)
    {
        _status = false;
        parent = self;
        parent.MouseDown += new MouseEventHandler(parent_MouseDown);
        parent.MouseUp += new MouseEventHandler(parent_MouseUp);
        parent.MouseMove += new MouseEventHandler(parent_MouseMove);
    }

    private Point MPoint;
    private bool isDragging;
    private Point touchPoint;

    private void parent_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;

        // Capture the point relative to the form
        touchPoint = e.Location;
    }
    private void parent_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }

    private void parent_MouseMove(object sender, MouseEventArgs e)
    {
        MPoint = new Point(Cursor.Position.X - touchPoint.X, Cursor.Position.Y - touchPoint.Y);

        if (isDragging && AllowMouseDownDrag && !parent.Location.Equals(MPoint))
        {
            parent.Location = MPoint;
        }
    }
}

问题是您使用的鼠标点是相对于窗体的,而不是相对于屏幕的。请参阅下面我的解决方案。我正在使用
Cursor.Position
获取鼠标屏幕位置

public class MouseDragFormMove
{
    private bool _status;
    public bool AllowMouseDownDrag
    {
        get { return _status; }
        set { _status = value; }
    }

    private Form parent;
    public MouseDragFormMove(Form self)
    {
        _status = false;
        parent = self;
        parent.MouseDown += new MouseEventHandler(parent_MouseDown);
        parent.MouseUp += new MouseEventHandler(parent_MouseUp);
        parent.MouseMove += new MouseEventHandler(parent_MouseMove);
    }

    private Point MPoint;
    private bool isDragging;
    private Point touchPoint;

    private void parent_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;

        // Capture the point relative to the form
        touchPoint = e.Location;
    }
    private void parent_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }

    private void parent_MouseMove(object sender, MouseEventArgs e)
    {
        MPoint = new Point(Cursor.Position.X - touchPoint.X, Cursor.Position.Y - touchPoint.Y);

        if (isDragging && AllowMouseDownDrag && !parent.Location.Equals(MPoint))
        {
            parent.Location = MPoint;
        }
    }
}

我认为闪烁很可能是被重新绘制的窗口。我认为闪烁很可能是被重新绘制的窗口。