无边框窗体移动
using System;
using System.Windows.Forms;
class form_move
{
Form f1;
bool beginMove = false;
int currentXPosition = 0;
int currentYPosition = 0;
public form_move(Form f)
{
f1 = f;
f.MouseDown += new MouseEventHandler(f_MouseDown);
f.MouseMove += new MouseEventHandler(f_MouseMove);
f.MouseUp += new MouseEventHandler(f_MouseUp);
f.MouseLeave += new EventHandler(f_MouseLeave);
}
void f_MouseMove(object sender, MouseEventArgs e)
{
if (beginMove == true)
{
f1.Left += Control.MousePosition.X - currentXPosition;
f1.Top += Control.MousePosition.Y - currentYPosition;
currentXPosition = Control.MousePosition.X;
currentYPosition = Control.MousePosition.Y;
}
}
void f_MouseLeave(object sender, EventArgs e)
{
currentXPosition = 0;
currentYPosition = 0;
}
void f_MouseUp(object sender, MouseEventArgs e)
{
beginMove = false;
}
void f_MouseDown(object sender, MouseEventArgs e)
{
beginMove = true;
currentXPosition = Control.MousePosition.X;//或直接用e.X;
currentYPosition = Control.MousePosition.Y;
}
}