1

I know you can use form click and drag with this But say I wanted it not using that code and using my own, how would I do it?

This is what I have so far:

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

What am I doing wrong? I can't get it working. Also, it flickers aswell

Community
  • 1
  • 1
Ben
  • 5,627
  • 9
  • 35
  • 49

1 Answers1

1

The problem is that you were using the mouse point relative to the form, not relative to the screen. See my solution below. I am using Cursor.Position to get the mouse screen 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;
        }
    }
}
Dave New
  • 38,496
  • 59
  • 215
  • 394