Sunday, March 13, 2005

Moving Winform by Clicking on Client Area

When your winform is a custom shape form and doesn't have the title bar, you might want to allow the user to move the form by clicking on the client area.

Here is a code snippet of how to do it :

public class Form1 : System.Windows.Forms.Form
{
private Point offset;

private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Point loc = this.Location;
Point mp = Control.MousePosition;

offset = new Point(-(mp.X - loc.X), -(mp.Y - loc.Y));
}

private void Form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(offset.X, offset.Y);
Location = mousePos;
}
}
}


Other winform code details has been omitted for code clarity here.

Labels:

0 Comments:

Post a Comment

<< Home