C#Click form&拖,上课

时间:2012-11-08 10:27:11

标签: c# winforms events mouseevent

我知道您可以使用this使用表单点击并拖动 但是说我希望它不使用该代码并使用我自己的代码,我该怎么做呢?

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

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

我做错了什么?我无法让它发挥作用。此外,它也会闪烁

1 个答案:

答案 0 :(得分:1)

问题是您使用的是相对于表单的鼠标点,而不是相对于屏幕。请参阅下面的解决方案我正在使用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;
        }
    }
}