【QQ揭秘】如何实现窗体靠边隐藏?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22 | /// /// 靠边隐藏的类型。 /// public enum DockHideType { /// /// 不隐藏 /// None = 0, /// /// 靠上边沿隐藏 /// Top, /// /// 靠左边沿隐藏 /// Left, /// /// 靠右边沿隐藏 /// Right } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21 | /// /// 窗体的显示或隐藏状态 /// public enum FormDockHideStatus { /// /// 已隐藏 /// Hide = 0, /// /// 准备隐藏 /// ReadyToHide, /// /// 正常显示 /// ShowNormally } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 | private void dockedForm_LocationChanged(object sender, EventArgs e) { this.ComputeDockHideType(); if (!this.IsOrg) { this.lastBoard = this.dockedForm.Bounds; this.IsOrg = true; } } /// /// 判断是否达到了隐藏的条件?以及是哪种类型的隐藏。 /// private void ComputeDockHideType() { if (this.dockedForm.Top <= 0) { this.dockHideType = DockHideType.Top; if (this.dockedForm.Bounds.Contains(Cursor.Position)) { this.formDockHideStatus = FormDockHideStatus.ReadyToHide; return; } this.formDockHideStatus = FormDockHideStatus.Hide; return; } else { if (this.dockedForm.Left <= 0) { this.dockHideType = DockHideType.Left; if (this.dockedForm.Bounds.Contains(Cursor.Position)) { this.formDockHideStatus = FormDockHideStatus.ReadyToHide; return; } this.formDockHideStatus = FormDockHideStaus.Hide; return; } else { if (this.dockedForm.Left < Screen.PrimaryScreen.Bounds.Width - this.dockedForm.Width) { this.dockHideType = DockHideType.None; this.formDockHideStatus = FormDockHideStatus.ShowNormally; return; } this.dockHideType = DockHideType.Right; if (this.dockedForm.Bounds.Contains(Cursor.Position)) { this.formDockHideStatus = FormDockHideStatus.ReadyToHide; return; } this.formDockHideStatus = FormDockHideStatus.Hide; return; } } } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 | /// /// 定时器循环判断。 /// private void CheckPosTimer_Tick(object sender, EventArgs e) {//当鼠标移动到窗体的范围内(此时,窗体的位置位于屏幕之外) if (this.dockedForm.Bounds.Contains(Cursor.Position)) { if (this.dockHideType!= DockHideType.Top) { if (this.dockHideType!= DockHideType.Left) { if (this.dockHideType!= DockHideType.Right) { return; } if (this.formDockHideStatus == FormDockHideStatus.Hide) { this.dockedForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.dockedForm.Width, this.dockedForm.Location.Y); return; } } else { if (this.formDockHideStatus == FormDockHideStatus.Hide) { this.dockedForm.Location = new Point(0, this.dockedForm.Location.Y); return; } } } else { if (this.formDockHideStatus == FormDockHideStatus.Hide) { this.dockedForm.Location = new Point(this.dockedForm.Location.X, 0); return; } } } else //当鼠标位于窗体范围之外,则根据DockHideType的值,决定窗体的位置。 { switch (this.dockHideType) { case DockHideType.None: { if (this.IsOrg && this.formDockHideStatus == FormDockHideStatus.ShowNormally && (this.dockedForm.Bounds.Width != this.lastBoard.Width || this.dockedForm.Bounds.Height != this.lastBoard.Height)) { this.dockedForm.Size = new Size(this.lastBoard.Width, this.lastBoard.Height); } break; } case DockHideType.Top: { this.dockedForm.Location = new Point(this.dockedForm.Location.X, (this.dockedForm.Height - 4) * -1); return; } case DockHideType.Left: { this.dockedForm.Location = new Point(-1 * (this.dockedForm.Width - 4), this.dockedForm.Location.Y); return; } default: { if (anchorStyles2 != DockHideType.Right) { return; } this.dockedForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 4, this.dockedForm.Location.Y); return; } } } } |
1 | this.autoDocker1.Initialize(this); |