winform 实现窗口程序像QQ一样靠近桌面边缘自动隐藏窗口

实现原理:

步骤如下:

    1、判断窗体程序是否靠近桌面边缘;

    2、获取桌面屏幕大小与窗体程序大小;

    3、把窗体程序显示在桌面以外隐藏起来,预留部分窗体方便用户拉出程序;

    4、判断鼠标是否在窗体程序上,在就靠边显示整个窗体程序,不在就隐藏显示,并调为半透明转态。

代码部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int SH;
        int SW;
        int self_SH;
        int self_SW;
        int star_win_flag = 1;//窗口初始化位置标志位,防止隐藏窗口后定时器重新跑窗口函数再次在初始化位置打开
        private void Form1_Load(object sender, EventArgs e)
        {
            //获取显示器屏幕的大小,不包括任务栏、停靠窗口
            SH = Screen.PrimaryScreen.WorkingArea.Height;
            SW = Screen.PrimaryScreen.WorkingArea.Width;
            //获取当前活动窗口高度跟宽度
            self_SH = this.Size.Height;
            self_SW = this.Size.Width;
            if(star_win_flag==1)
            {
                //设置窗口打开的位置为下方居中
                SetDesktopLocation((SW - self_SW) / 2, SH - self_SH);
                star_win_flag = 0;
            }
            //============添加窗体所在位置定时检测=================
            TopMost = true; //把窗体设置为最顶层
            System.Windows.Forms.Timer MyTimer = new System.Windows.Forms.Timer();
            MyTimer.Tick += new EventHandler(StopRectTimer_Tick);
            MyTimer.Interval = 100;
            MyTimer.Enabled = true;
 
        }

  

1
2
3
4
5
6
7
8
9
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
//=================隐藏窗体&显示部分==================
        int check_flag = 0; //窗体隐藏标志位,0为不开启隐藏功能,初始默认0
        int win = 0;
        int frmX;
        int frmY;
        private void StopRectTimer_Tick(object sender, EventArgs e)
        {
            // 获取窗体位置
            frmX = this.Location.X;
            frmY = this.Location.Y;
 
            if (check_flag == 1)
            {
                //获取窗口的边沿与桌面的间距,判断窗口是否靠近边沿里面-1个位置
                if (this.Left <= 0) //获取控件左边沿与桌面左边沿的间距,窗口靠近左边桌面边沿        
                    win = 1;
                else if (this.Top <= 0 && this.Left > 0 && this.Right < SW - 1)////获取控件上边沿与桌面上边沿的间距,窗口靠近顶端桌面边沿
                    win = 2;
                else if (this.Right >= SW) ////获取控件右边沿与桌面左边沿的间距,窗口靠近右边桌面边沿 
                    win = 3;
                else //窗体没有靠近边沿
                    win = 0;
 
                /* Cursor.Position获取当前鼠标的位置
                 * Bounds.Contains(Cursor.Position)获取鼠标位置是否在窗口边界里面,在返回ture
                 *如果鼠标在窗体上,则根据停靠位置显示整个窗体
                 *窗体边沿计算是以左边沿为主*/
 
                if (Bounds.Contains(Cursor.Position))
                {
                    switch (win)
                    {
                        case 1:
                            this.Opacity = 1.0f;    //窗口恢复不透明状态
                                                    //窗体靠近左沿时,鼠标在窗体显示完整窗体
                            SetDesktopLocation(0, frmY);
                            break;
                        case 2:
                            this.Opacity = 1.0f;    //窗口恢复不透明状态
                                                    //窗体靠近顶部时,鼠标在窗体显示完整窗体 
                            SetDesktopLocation(frmX, 0);
                            break;
                        case 3:
                            this.Opacity = 1.0f;    //窗口恢复不透明状态
                                                    //窗体靠近右沿时,鼠标在窗体显示完整窗体
                            SetDesktopLocation(SW - self_SW, frmY);
                            break;
                    }
                }
 
                //如果鼠标离开窗体,则根据停靠位置隐藏窗体(即把窗体显示出桌面以外),但须留出部分窗体边缘以便鼠标选中窗体,这里留7个位置
                else
                {
                    switch (win)
                    {
                        case 1:
                            this.Opacity = 0.2f; //窗口百分之80透明
                                                 //窗体靠近左沿时,鼠标不在窗体时隐藏
                            SetDesktopLocation(20 - self_SW, frmY);
                            break;
                        case 2:
                            this.Opacity = 0.2f; //窗体靠近顶部时,鼠标不在窗体时隐藏 
                            SetDesktopLocation(frmX, 20 - self_SH);
                            break;
                        case 3:
                            this.Opacity = 0.2f; //窗体靠近右沿时,鼠标不在窗体时隐藏 
                            SetDesktopLocation(SW - 20, frmY);
                            break;
                    }
                }
            }
        }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*==========窗体边沿隐藏功能开启选择框===========*/
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.CheckState == CheckState.Checked) //判断复选框选中
 
            {
                check_flag = 1;
                //if(win==0)//判断框功能选中时,判断窗口不在边沿时自动收到上边沿中间隐藏
                {
                    this.Opacity = 0.2f; //窗口半透明                  
                    SetDesktopLocation((SW - self_SW) / 2, 20-SH );
                }
                 
                //MessageBox.Show("checkbox1 is checked\n" + checkBox1.Text);
 
            }
 
            else if (checkBox1.CheckState == CheckState.Unchecked) //判断复选框没选中
 
            {
                check_flag = 0;
                //MessageBox.Show("checkbox1 is Unchecked\n" + checkBox1.Text);
 
            }
        }

 隐藏结果:

 

 

 转发地址:https://www.cnblogs.com/xingboy/p/10110443.html

posted @   fulllove  阅读(624)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示