Google
 

Sunday, July 22, 2007

Moving a form without a title bar

How to make a form without a title bar movable?

This is a common request in programming forums, and here is a simple solution that depends on handling mouse down and mouse move events:


public partial class Form1 : Form
{
int m_PrevX;
int m_PrevY;

public Form1()
{
InitializeComponent();
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;

Left = Left + (e.X - m_PrevX);
Top = Top + (e.Y - m_PrevY);

}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button!=MouseButtons.Left)
return;

m_PrevX = e.X;
m_PrevY = e.Y;
}

4 comments:

Lyle Mcclanahan said...

Excellent example. I knew what I was trying to do.. I just couldnt put it into code. Thank You.

Anonymous said...

Thanks.
A lot better than using System.Runtime.InteropServices!!!

Shanti said...

Awesome Man....... Keep up the Good Work. U made the Situation soo simple yar.

Jasmin Marinelli said...

Thank you !
Simple and easy ...