locked
How to drag a winforms application by clicking on the body and dragging it. RRS feed

  • Question

  • Hi, there. I am creating a borderless winforms application. I want to make possible for the users to move the form around the screen when clicked and drag. Any help would be much appreciated.
    Best Regards Gopal Adhikari
    Visit My Blog at: http://hacktheugly.wordpress.com
    Friday, August 21, 2009 12:08 AM

Answers

  • You'll need to trap the mouse down/mouse up/and mouse move events.  When the mouse is pressed, start tracking, and "moving" your form position to follow the movements of the mouse.  When it's released, stop tracking.

    For details, check OnMouseDown , OnMouseMove , and OnMouseUp methods.  You should override all three in your form to handle the tracking.



    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Aland Li Monday, August 24, 2009 11:47 AM
    Friday, August 21, 2009 12:22 AM
  • This should be all it takes.  Just add it to your form class:

    class Form1 : Form
    {
       // Normal form stuff...
    
       bool trackPosition = false;
       Point lastPoint = new Point();
    
       protected override void OnMouseDown(MouseEventArgs e)
       {
          trackPosition = true;
          lastPoint = e.Location;
          base.OnMouseDown(e);
       }
       protected override void OnMouseUp(MouseEventArgs e)
       {
          trackPosition = false;
          base.OnMouseUp(e);
       }
    
       protected override void OnMouseMove(MouseEventArgs e)
       {
          if (trackPosition)
          {
              Point newPoint = e.Location;
              Size shift = new Size(newPoint.X - lastPoint.X, newPoint.Y - lastPoint.Y);
              lastPoint = e.Location;
              this.Location += shift;
          }
          base.OnMouseMove(e);
       }
    }
    
    
    Basically, it just "tracks" the last position of the mouse, but only when the mouse is pressed.  This was from memory, and untested... so you may need to fix one or two things in there.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by Reed Copsey, JrMVP Friday, August 21, 2009 1:18 AM
    • Marked as answer by Aland Li Monday, August 24, 2009 11:50 AM
    Friday, August 21, 2009 12:37 AM
  • Classic bug.  Fix:

            //lastPoint = e.Location;


    Hans Passant.
    • Marked as answer by Aland Li Monday, August 24, 2009 11:50 AM
    Friday, August 21, 2009 1:35 AM

All replies

  • You'll need to trap the mouse down/mouse up/and mouse move events.  When the mouse is pressed, start tracking, and "moving" your form position to follow the movements of the mouse.  When it's released, stop tracking.

    For details, check OnMouseDown , OnMouseMove , and OnMouseUp methods.  You should override all three in your form to handle the tracking.



    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Aland Li Monday, August 24, 2009 11:47 AM
    Friday, August 21, 2009 12:22 AM
  • A sample code would be helpful please.

    Best Regards Gopal Adhikari
    Visit My Blog at: http://hacktheugly.wordpress.com
    Friday, August 21, 2009 12:23 AM
  • This should be all it takes.  Just add it to your form class:

    class Form1 : Form
    {
       // Normal form stuff...
    
       bool trackPosition = false;
       Point lastPoint = new Point();
    
       protected override void OnMouseDown(MouseEventArgs e)
       {
          trackPosition = true;
          lastPoint = e.Location;
          base.OnMouseDown(e);
       }
       protected override void OnMouseUp(MouseEventArgs e)
       {
          trackPosition = false;
          base.OnMouseUp(e);
       }
    
       protected override void OnMouseMove(MouseEventArgs e)
       {
          if (trackPosition)
          {
              Point newPoint = e.Location;
              Size shift = new Size(newPoint.X - lastPoint.X, newPoint.Y - lastPoint.Y);
              lastPoint = e.Location;
              this.Location += shift;
          }
          base.OnMouseMove(e);
       }
    }
    
    
    Basically, it just "tracks" the last position of the mouse, but only when the mouse is pressed.  This was from memory, and untested... so you may need to fix one or two things in there.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by Reed Copsey, JrMVP Friday, August 21, 2009 1:18 AM
    • Marked as answer by Aland Li Monday, August 24, 2009 11:50 AM
    Friday, August 21, 2009 12:37 AM
  • It worked... but the form flickers when drag. Anyway thanks a lot for the help.
    Best Regards Gopal Adhikari
    Visit My Blog at: http://hacktheugly.wordpress.com
    Friday, August 21, 2009 1:05 AM
  • Classic bug.  Fix:

            //lastPoint = e.Location;


    Hans Passant.
    • Marked as answer by Aland Li Monday, August 24, 2009 11:50 AM
    Friday, August 21, 2009 1:35 AM
  • thanks nobugz you are awesome.
    Best Regards Gopal Adhikari
    Visit My Blog at: http://hacktheugly.wordpress.com
    Friday, August 21, 2009 3:09 PM