分为三个事件组成 参考网上代码
bool formMove = false;//窗体是否移动
Point formPoint;//记录窗体的位置
// private bool p;
Point formPoint;//记录窗体的位置
// private bool p;
/// <summary>
/// 鼠标按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmLogin_MouseDown(object sender, MouseEventArgs e)
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
}
/// 鼠标按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmLogin_MouseDown(object sender, MouseEventArgs e)
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
}
/// <summary>
/// 窗体拖动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmLogin_MouseMove(object sender, MouseEventArgs e)
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X + 3, formPoint.Y + 30);
Location = mousePos;
}
}
/// <summary>
/// 鼠标松开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmLogin_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
}
/// 鼠标松开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmLogin_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
}