為了美觀,去掉窗體的標(biāo)題欄朝巫,給窗體的FormBorderStyle
屬性設(shè)置為None
鸿摇,這時(shí),使用鼠標(biāo)拖動(dòng)和縮放窗口就會(huì)失效劈猿。下面幾種簡(jiǎn)單方法來(lái)恢復(fù)所需效果拙吉。
鼠標(biāo)的拖動(dòng)
- 注冊(cè) 窗體的
MouseDown
和MouseMove
事件,通過(guò)設(shè)置窗體的Location
來(lái)實(shí)現(xiàn)移動(dòng)揪荣。(較簡(jiǎn)單筷黔,不需要添加其他元素)
public partial class FmMain : Form {
private Point formPoint = new Point();
public FmMain() {
InitializeComponent();
}
private void FmMain_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
Point myPosittion = MousePosition;
myPosittion.Offset(-formPoint.X, -formPoint.Y);
Location = myPosittion;
}
}
private void FmMain_MouseDown(object sender, MouseEventArgs e) {
formPoint.X = e.X;
formPoint.Y = e.Y;
}
}
- 設(shè)置一個(gè)透明的 panel 置于底層,通過(guò)設(shè)置 panel 的
MouseDown
和MouseMove
事件仗颈,來(lái)實(shí)現(xiàn)窗體的拖動(dòng)佛舱。(多添加了一個(gè)元素,實(shí)現(xiàn)方式和第一種差不多)
public partial class FmMain : Form {
private Point mousePoint = new Point();
public FmMain() {
InitializeComponent();
}
private void panelMain_MouseDown(object sender, MouseEventArgs e) {
base.OnMouseDown(e);
this.mousePoint.X = e.X;
this.mousePoint.Y = e.Y;
}
private void panelMain_MouseMove(object sender, MouseEventArgs e) {
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left) {
this.Top = Control.MousePosition.Y - mousePoint.Y;
this.Left = Control.MousePosition.X - mousePoint.X;
}
}
}
網(wǎng)上還有其他方法挨决,相比較這兩種稍微有點(diǎn)復(fù)雜名眉。