public Form1()
{
InitializeComponent();
SetMouseEvents(this.Controls);
}
private void SetMouseEvents(Control.ControlCollection controls)
{
foreach (Control c in controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
c.MouseUp += new MouseEventHandler(c_MouseUp);
c.MouseMove += new MouseEventHandler(c_MouseMove);
SetMouseEvents(c.Controls);
}
}
void c_MouseMove(object sender, MouseEventArgs e)
{
if (moveEnabled)
this.Location = new Point(Control.MousePosition.X - oldFormPosition.X, Control.MousePosition.Y - oldFormPosition.Y);
}
void c_MouseUp(object sender, MouseEventArgs e)
{
moveEnabled = false;
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c=(Control)sender;
moveEnabled = true;
Point rec = c.PointToScreen(new Point(e.X, e.Y));
oldFormPosition = new Point(rec.X - this.Location.X, rec.Y - this.Location.Y);
}
bool moveEnabled = false;
Point oldFormPosition;