C#中winform窗口做成QQ窗口那样能收缩在电脑屏幕的上方

 1 private void Form1_Load(object sender, EventArgs e)
 2         {
 3             
 4             System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer();
 5             StopRectTimer.Tick += new EventHandler(timer1_Tick);
 6             StopRectTimer.Interval = 50;
 7             StopRectTimer.Enabled = true;
 8             this.TopMost = true;
 9             StopRectTimer.Start();
10         }
11 
12         private void timer1_Tick(object sender, EventArgs e)
13         {
14             if (this.Bounds.Contains(Cursor.Position))
15             {
16                 switch (this.StopAanhor)
17                 {
18                     case AnchorStyles.Top:
19                         this.Location = new Point(this.Location.X, 0);
20                         break;
21                     case AnchorStyles.Left:
22                         this.Location = new Point(0, this.Location.Y);
23                         break;
24                     case AnchorStyles.Right:
25                         this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
26                         break;
27                     case AnchorStyles.Bottom:
28                         this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
29                         break;
30                 }
31             }
32             else
33             {
34                 switch (this.StopAanhor)
35                 {
36                     case AnchorStyles.Top:
37                         this.Location = new Point(this.Location.X, (this.Height - 8) * (-1));
38                         break;
39                     case AnchorStyles.Left:
40                         this.Location = new Point((-1) * (this.Width - 8), this.Location.Y);
41                         break;
42                     case AnchorStyles.Right:
43                         this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 8, this.Location.Y);
44                         break;
45                     case AnchorStyles.Bottom:
46                         this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 8));
47                         break;
48                 }
49             }
50 
51         }
52 
53         internal AnchorStyles StopAanhor = AnchorStyles.None;
54         private void mStopAnhor()
55         {
56             if (this.Top <= 0 && this.Left <= 0)
57             {
58                 StopAanhor = AnchorStyles.None;
59             }
60             else if (this.Top <= 0)
61             {
62                 StopAanhor = AnchorStyles.Top;
63             }
64             else if (this.Left <= 0)
65             {
66                 StopAanhor = AnchorStyles.Left;
67             }
68             else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
69             {
70                 StopAanhor = AnchorStyles.Right;
71             }
72             else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)
73             {
74                 StopAanhor = AnchorStyles.Bottom;
75             }
76             else
77             {
78                 StopAanhor = AnchorStyles.None;
79             }
80         }
81 
82         private void hide_LocationChanged(object sender, EventArgs e)
83         {
84             this.mStopAnhor();
85         }
86 
87 }

 【转载】

posted on 2013-11-27 16:27  MasonChi  阅读(236)  评论(0编辑  收藏  举报

导航