【问题标题】:C# Click form & drag, classC# 单击窗体并拖动,类
【发布时间】:2012-11-08 10:27:11
【问题描述】:

我知道你可以使用表单点击和拖动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);
    }
}

我做错了什么?我无法让它工作。而且,它也会闪烁

【问题讨论】:

  • 我认为闪烁很可能是正在重绘的窗口。

标签: c# winforms events mouseevent


【解决方案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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-03-04
    • 2011-06-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2018-07-24
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多