-
首先要把Winform默認的邊框去掉:
this.FormBorderStyle = FormBorderStyle.None; this
圖1 默認窗口 圖2 去掉默認邊框 spa
-
在窗體上拖入一個panel,設置panel屬性,並拖入本身想要的控件。
-
panel1.Dock = DockStyle.Fill;
-
panel1.BackgroundImage = 誠信.Properties.Resources.login;
-
panel1.BackgroundImageLayout = ImageLayout.Stretch;
-
給panel添加三個事件:
MouseUp、MouseMove、MouseDown。便可實現鼠標拖動窗體移動。
-
private bool formMove = false;//窗體是否移動
-
private Point formPoint;//記錄窗體的位置
-
-
#region 拖動窗體移動
-
private void panel1_MouseUp(object sender, MouseEventArgs e)
-
{
-
-
if (e.Button == MouseButtons.Left)//按下的是鼠標左鍵
-
{
-
formMove = false;//中止移動
-
}
-
-
}
-
-
private void panel1_MouseMove(object sender, MouseEventArgs e)
-
{
-
if (formMove == true)
-
{
-
Point mousePos = Control.MousePosition;
-
mousePos.Offset(formPoint.X, formPoint.Y);
-
Location = mousePos;
-
}
-
}
-
-
private void panel1_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.FrameBorderSize.Height;//SystemInformation.CaptionHeight -
-
formPoint = new Point(xOffset, yOffset);
-
formMove = true;//開始移動
-
}
-
}
-
#endregion
-
運行結果
圖3 運行結果orm