C#winform中怎么实现无边框窗体的拖动、最大化、最小化以及关闭。

2024-12-22 14:49:23
推荐回答(2个)
回答1:

无边框窗体的移动:bool formMove = false;//窗体是否移动
Point formPoint;//记录窗体的位置 private void AddFriendForm2_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;//开始移动
}
} private void AddFriendForm2_MouseMove(object sender, MouseEventArgs e)
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X, formPoint.Y);
Location = mousePos;
}
} private void AddFriendForm2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
} 最大化最小化那些其实都是自己加的,可以用image点击事件里面实现最大化最小化以及关闭我用的是label: private void label1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
} private void label2_Click(object sender, EventArgs e)
{
this.Close();
}

回答2:

画, mousedown mouseup mousemove down时标记拖动开始,记下鼠标位置,move时的鼠标位置,2个位置差改变localtion