让窗体拖到屏幕边缘自动隐藏的原理,通过Form1_LocationChanged的方法,当窗体位置发生改变时,判断其是否在屏幕边缘,在则隐藏。再通过Timer控件经过指定时间判断出鼠标的位置,若鼠标在屏幕左边、上边或右边,这根据窗体的位置,调出窗体。
以下给出源代码: (注:hide为窗体名称)
view plaincopy to clipboardprint?
1. private void hide_Load(object sender, EventArgs e)
2. {
3. System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer();
4. StopRectTimer.Tick += new EventHandler(timer1_Tick);
5. StopRectTimer.Interval = 100;
6. StopRectTimer.Enabled = true;
7. }
8. private void timer1_Tick(object sender, EventArgs e)
以下给出源代码: (注:hide为窗体名称)
view plaincopy to clipboardprint?
1. private void hide_Load(object sender, EventArgs e)
2. {
3. System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer();
4. StopRectTimer.Tick += new EventHandler(timer1_Tick);
5. StopRectTimer.Interval = 100;
6. StopRectTimer.Enabled = true;
7. }
8. private void timer1_Tick(object sender, EventArgs e)
9. {
10. if (this.Bounds.Contains(Cursor.Position))
11. {
12. switch (this.StopAanhor)
13. {
14. case AnchorStyles.Top:
15. this.Location = new Point(this.Location.X, 0);
16. break;
17. case AnchorStyles.Left:
18. this.Location = new Point(0, this.Location.Y);
19. break;
20. case AnchorStyles.Right:
21. this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
22. break;
11. {
12. switch (this.StopAanhor)
13. {
14. case AnchorStyles.Top:
15. this.Location = new Point(this.Location.X, 0);
16. break;
17. case AnchorStyles.Left:
18. this.Location = new Point(0, this.Location.Y);
19. break;
20. case AnchorStyles.Right:
21. this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
22. break;
23. }
24. }
25. else
26. {
24. }
25. else
26. {
27. switch (this.StopAanhor)
28. {
29. case AnchorStyles.Top:
30. this.Location = new Point(this.Location.X, (this.Height - 2) * (-1));
31. break;
32. case AnchorStyles.Left:
33. this.Location = new Point((-1) * (this.Width - 2), this.Location.Y);
34. break;
35. case AnchorStyles.Right:
36. this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);
28. {
29. case AnchorStyles.Top:
30. this.Location = new Point(this.Location.X, (this.Height - 2) * (-1));
31. break;
32. case AnchorStyles.Left:
33. this.Location = new Point((-1) * (this.Width - 2), this.Location.Y);
34. break;
35. case AnchorStyles.Right:
36. this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);
37. break;
38. }
39. }
40. }
41. internal AnchorStyles StopAanhor = AnchorStyles.None;
42. private void mStopAnhor() printform
43. {
38. }
39. }
40. }
41. internal AnchorStyles StopAanhor = AnchorStyles.None;
42. private void mStopAnhor() printform
43. {
44. if (this.Top <= 0)
45. {
46. StopAanhor = AnchorStyles.Top;
47. }
48. else if (this.Left <= 0)
49. {
50. StopAanhor = AnchorStyles.Left;
51. }
45. {
46. StopAanhor = AnchorStyles.Top;
47. }
48. else if (this.Left <= 0)
49. {
50. StopAanhor = AnchorStyles.Left;
51. }
52. else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
53. {
54. StopAanhor = AnchorStyles.Right;
55. }
56. else
57. {
58. StopAanhor = AnchorStyles.None;
59. }
60. }
53. {
54. StopAanhor = AnchorStyles.Right;
55. }
56. else
57. {
58. StopAanhor = AnchorStyles.None;
59. }
60. }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论