C# 实现winform自动悬浮

2021-01-20 22:14

阅读:589

标签:prim   component   names   inter   poi   nal   nts   eth   计时   

winform自动悬浮,主要代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 
  9 namespace SupremeWindowsForms
 10 {
 11     public partial class Form : System.Windows.Forms.Form
 12     {
 13         #region Fields and Properties
 14         /// 
 15         /// 锚定位置
 16         /// 
 17         internal AnchorStyles StopAanhor = AnchorStyles.None;
 18 
 19         /// 
 20         /// 计时器
 21         /// 
 22         public Timer TimerAnchor { get; set; }
 23 
 24         #endregion
 25 
 26         #region Methods
 27         public Form()
 28         {
 29             InitializeComponent();
 30         }
 31 
 32         /// 
 33         /// 控制窗体隐藏与复现
 34         /// 
 35         /// 
 36         /// 
 37         private void Time_Tick(object sender, EventArgs e)
 38         {
 39             if (this.Bounds.Contains(Cursor.Position))
 40             {
 41                 switch (this.StopAanhor)
 42                 {
 43                     case AnchorStyles.Top:
 44                         //窗体在最上方隐藏时,鼠标接触自动出现
 45                         this.Location = new Point(this.Location.X, 0);
 46                         break;
 47                     //窗体在最左方隐藏时,鼠标接触自动出现
 48                     case AnchorStyles.Left:
 49                         this.Location = new Point(0, this.Location.Y);
 50                         break;
 51                     //窗体在最右方隐藏时,鼠标接触自动出现
 52                     case AnchorStyles.Right:
 53                         this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
 54                         break;
 55                 }
 56             }
 57             else
 58             {
 59                 //窗体隐藏时在靠近边界的一侧边会出现2像素原因:感应鼠标,同时2像素不会影响用户视线
 60                 switch (this.StopAanhor)
 61                 {
 62                     //窗体在顶部时时,隐藏在顶部,底部边界出现2像素
 63                     case AnchorStyles.Top:
 64                         this.Location = new Point(this.Location.X, (this.Height - 2) * (-1));
 65                         break;
 66                     //窗体在最左边时时,隐藏在左边,右边边界出现2像素
 67                     case AnchorStyles.Left:
 68                         this.Location = new Point((-1) * (this.Width - 2), this.Location.Y);
 69                         break;
 70                     //窗体在最右边时时,隐藏在右边,左边边界出现2像素
 71                     case AnchorStyles.Right:
 72                         this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);
 73                         break;
 74                 }
 75             }
 76         }
 77 
 78         /// 
 79         /// 设置窗体位置的类型
 80         /// 
 81         private void SetAnchor()
 82         {
 83             if (this.Top 0)
 84             {
 85                 StopAanhor = AnchorStyles.Top;
 86             }
 87             else if (this.Left 0)
 88             {
 89                 StopAanhor = AnchorStyles.Left;
 90             }
 91             else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
 92             {
 93                 StopAanhor = AnchorStyles.Right;
 94             }
 95             else
 96             {
 97                 StopAanhor = AnchorStyles.None;
 98             }
 99         }
100         #endregion
101 
102         #region Events
103         private void Form_LocationChanged(object sender, EventArgs e)
104         {
105             SetAnchor();
106         }
107 
108         private void Form_Load(object sender, EventArgs e)
109         {
110             TimerAnchor = new Timer { Interval = 100 };
111             TimerAnchor.Tick += Time_Tick;
112             //timer1.Tick += Timer1_Tick;
113             TimerAnchor.Start();
114         }
115         #endregion
116    }
117 }

 

C# 实现winform自动悬浮

标签:prim   component   names   inter   poi   nal   nts   eth   计时   

原文地址:https://www.cnblogs.com/1175429393wljblog/p/12119315.html


评论


亲,登录后才可以留言!