C#实现窗体靠近屏幕边界自动隐藏

【转载】

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 namespace BounHide
10 {
11     public partial class hide : Form
12     {
13         public hide()
14         {
15             InitializeComponent();
16         }
17         /// <summary>
18         /// 定义了窗体隐藏、出现的事件
19         /// </summary>
20         /// <param name="sender"></param>
21         /// <param name="e"></param>
22         private void timer1_Tick(object sender, EventArgs e)
23         {
24             if (this.Bounds.Contains(Cursor.Position))
25             {
26                 switch (this.StopAanhor)
27                 {
28                     case AnchorStyles.Top:
29                         //窗体在最上方隐藏时,鼠标接触自动出现
30                         this.Location = new Point(this.Location.X,0) ;
31                         break;
32                          //窗体在最左方隐藏时,鼠标接触自动出现
33                     case AnchorStyles.Left:
34                         this.Location = new Point(0, this.Location.Y);
35                         break;
36                     //窗体在最右方隐藏时,鼠标接触自动出现
37                     case AnchorStyles.Right:
38                         this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
39                         break;
40                 }
41             }
42             else
43             {
44                 //窗体隐藏时在靠近边界的一侧边会出现2像素原因:感应鼠标,同时2像素不会影响用户视线
45                 switch (this.StopAanhor)
46                 {
47                     //窗体在顶部时时,隐藏在顶部,底部边界出现2像素
48                     case AnchorStyles.Top:
49                         this.Location = new Point(this.Location.X,(this.Height-2)*(-1));
50                         break;
51                     //窗体在最左边时时,隐藏在左边,右边边界出现2像素
52                     case AnchorStyles.Left:
53                         this.Location = new Point((-1)*(this.Width-2),this.Location.Y);
54                         break;
55                     //窗体在最右边时时,隐藏在右边,左边边界出现2像素
56                     case AnchorStyles.Right:
57                         this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);
58                         break;
59                 }
60             }
61         }
62         internal AnchorStyles StopAanhor = AnchorStyles.None;
63         /// <summary>
64         /// 固定了窗体位置的类型
65         /// </summary>
66         private void mStopAnhor()
67         {
68             if (this.Top <= 0)
69             {
70                 StopAanhor = AnchorStyles.Top;
71             }
72             else if (this.Left <= 0)
73             {
74                 StopAanhor = AnchorStyles.Left;
75             }
76             else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
77             {
78                 StopAanhor = AnchorStyles.Right;
79             }
80             else
81             {
82                 StopAanhor = AnchorStyles.None;
83             }
84         }
85         private void hide_LocationChanged(object sender, EventArgs e)
86         {
87             this.mStopAnhor();
88         }
89         private void hide_Load(object sender, EventArgs e)
90         {
91             this.timer1.Start();
92         }
93     }
94 }
原文地址:https://www.cnblogs.com/masonchi/p/3446106.html