C#中让窗体自动靠边隐藏

 
  
1:  private void Yincangtimer_Tick(object sender, EventArgs e)//窗体隐藏事件
   2:          {
   3:              int a = Control.MousePosition.Y;//光标的在屏幕中的 Y 坐标
   4:              int b = Control.MousePosition.X;//光标的在屏幕中的 X 坐标
   5:              int height = Screen.PrimaryScreen.WorkingArea.Height;//屏幕的高
   6:              int width = Screen.PrimaryScreen.WorkingArea.Width;//屏幕的宽
   7:              int x = this.Left;//窗体的在屏幕中的X坐标
   8:              int y = this.Top;//窗体的在屏幕中的Y坐标
   9:              //判断光标是否在窗体内
  10:              if (b >= x && b <= (this.Width + x) && a >= y && a <= (this.Top + this.Height))
  11:              {
  12:                  return;
  13:              }
  14:              else
  15:              {//隐藏窗体
  16:                  if ((x + this.Width) >= width)
  17:                  {
  18:                      this.Top = this.Top;
  19:                      this.Left = width - 5;
  20:                  }
  21:                  else if (x <= 0)
  22:                  {
  23:                      this.Top = this.Top;
  24:                      this.Left = 5 - this.Width;
  25:                  }
  26:                  else if (y <= 0)
  27:                  {
  28:                      this.Left = this.Left;
  29:                      this.Top = 5 - this.Height;
  30:   
  31:                  }
  32:                  else
  33:                  {
  34:   
  35:                      return;
  36:                  }
  37:              }
  38:          }
  39:   
  40:  //光标离开窗体
  41:          private void MainForm_MouseLeave(object sender, EventArgs e)
  42:          {
  43:              Yincangtimer.Start();
  44:          }
  45:          //光标进入窗体
  46:          private void MainForm_MouseEnter(object sender, EventArgs e)
  47:          {
  48:              int height = Screen.PrimaryScreen.WorkingArea.Height;//屏幕的高
  49:              int width = Screen.PrimaryScreen.WorkingArea.Width;//屏幕的宽
  50:         
  51:              if (this.Left < 0)
  52:              {
  53:                  this.Left = 0;
  54:                  this.Top = this.Top;
  55:                  Yincangtimer.Stop();
  56:              }
  57:              else if (this.Left > width - this.Width)
  58:              {
  59:                  this.Left = width - this.Width;
  60:                  this.Top = this.Top;
  61:                  Yincangtimer.Stop();
  62:              }
  63:              else if (this.Top <= 0)
  64:              {
  65:                  this.Left = this.Left;
  66:                  this.Top = 0;
  67:                  Yincangtimer.Stop();
  68:              }
  69:   
  70:              else
  71:              { return; }
  72:          }

转载于:https://www.cnblogs.com/sczw-maqing/p/3259158.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值