C#可拖动窗体(实现无标题栏的登录窗体)
所谓“可拖动窗体”就是无需拖动标题栏就可以拖动的窗体。
代码:
public partial class frmLogin : Form
{
private Point _MouseOffset;
private bool _IsMouseDown = false;
public frmLogin()
{
InitializeComponent();
}
private void frmLogin_MouseDown(object sender, MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X;
yOffset = -e.Y;
_MouseOffset = new Point(xOffset, yOffset);
_IsMouseDown = true;
}
}
private void frmLogin_MouseMove(object sender, MouseEventArgs e)
{
if (_IsMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(_MouseOffset.X, _MouseOffset.Y);
Location = mousePos;
}
}
private void frmLogin_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_IsMouseDown = false;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnLogin_Click(object sender, EventArgs e)
{
lbStatus.Text = "正在初始化系统...";
Thread.Sleep(1000);
lbStatus.Text = "正在初始化权限数据...";
Thread.Sleep(1000);
}
}