C#实现类似QQ的窗体停靠

直接上代码:

 1         [DllImport("User32.dll")]
2 public static extern bool PtInRect(ref Rectangle Rects, Point lpPoint);
3
4 private void timerShowHide_Tick(object sender, EventArgs e)
5 {
6 if (this.WindowState == FormWindowState.Normal)
7 {
8 System.Drawing.Point cursorPoint = new Point(Cursor.Position.X, Cursor.Position.Y);//获取鼠标在屏幕的坐标点
9 Rectangle Rects = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);//存储当前窗体在屏幕的所在区域
10 bool prInRect = PtInRect(ref Rects, cursorPoint);
11 if (prInRect)
12 {//当鼠标在当前窗体内
13 if (this.Top < 0)//窗体的Top属性小于0
14 this.Top = 0;
15 else if (this.Left < 0)//窗体的Left属性小于0
16 this.Left = 0;
17 else if (this.Right > Screen.PrimaryScreen.WorkingArea.Width)//窗体的Right属性大于屏幕宽度
18 this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
19 }
20 else
21 {
22 if (this.Top < 5) //当窗体的上边框与屏幕的顶端的距离小于5时
23 this.Top = 5 - this.Height; //将窗体隐藏到屏幕的顶端
24 else if (this.Left < 5) //当窗体的左边框与屏幕的左端的距离小于5时
25 this.Left = 5 - this.Width; //将窗体隐藏到屏幕的左端
26 else if (this.Right > Screen.PrimaryScreen.WorkingArea.Width - 5)//当窗体的右边框与屏幕的右端的距离小于5时
27 this.Left = Screen.PrimaryScreen.WorkingArea.Width - 5;//将窗体隐藏到屏幕的右端
28 }
29 }
30 }

转载于:https://my.oschina.net/weisenz/blog/200641

猜你喜欢

转载自blog.csdn.net/weixin_33739541/article/details/91920874