Winform隐藏标题栏后移动窗口
为了美观,有时需要将Winform的标题栏隐藏,但带来的结果是无法像之前那样任意移动窗口。此时,需要重载窗口事件OnMouseDown,OnMouseMove,OnMouseUp三个事件。
Winform隐藏标题栏后实现窗口移动
1 #region 隐藏标题栏后移动窗口
2 /// <summary>
3 /// 鼠标按下,开启移动
4 /// </summary>
5 /// <param name="e"></param>
6 protected override void OnMouseDown(MouseEventArgs e)
7 {
8 base.OnMouseDown(e);
9 m_mousePos = Cursor.Position;
10 m_isMouseDown = true;
11 }
12
13 /// <summary>
14 /// 鼠标抬起,关闭移动
15 /// </summary>
16 /// <param name="e"></param>
17 protected override void OnMouseUp(MouseEventArgs e)
18 {
19 base.OnMouseUp(e);
20 m_isMouseDown = false;
21 this.Focus();
22 }
23
24 /// <summary>
25 /// 移动窗口
26 /// </summary>
27 /// <param name="e"></param>
28 protected override void OnMouseMove(MouseEventArgs e)
29 {
30 base.OnMouseMove(e);
31 if (m_isMouseDown)
32 {
33 Point tempPos = Cursor.Position;
34 this.Location = new Point(Location.X + (tempPos.X - m_mousePos.X), Location.Y + (tempPos.Y - m_mousePos.Y));
35 m_mousePos = Cursor.Position;
36 }
37
38 }
39 #endregion
2 /// <summary>
3 /// 鼠标按下,开启移动
4 /// </summary>
5 /// <param name="e"></param>
6 protected override void OnMouseDown(MouseEventArgs e)
7 {
8 base.OnMouseDown(e);
9 m_mousePos = Cursor.Position;
10 m_isMouseDown = true;
11 }
12
13 /// <summary>
14 /// 鼠标抬起,关闭移动
15 /// </summary>
16 /// <param name="e"></param>
17 protected override void OnMouseUp(MouseEventArgs e)
18 {
19 base.OnMouseUp(e);
20 m_isMouseDown = false;
21 this.Focus();
22 }
23
24 /// <summary>
25 /// 移动窗口
26 /// </summary>
27 /// <param name="e"></param>
28 protected override void OnMouseMove(MouseEventArgs e)
29 {
30 base.OnMouseMove(e);
31 if (m_isMouseDown)
32 {
33 Point tempPos = Cursor.Position;
34 this.Location = new Point(Location.X + (tempPos.X - m_mousePos.X), Location.Y + (tempPos.Y - m_mousePos.Y));
35 m_mousePos = Cursor.Position;
36 }
37
38 }
39 #endregion
以后还会用到,Mark下。