[C#] 窗口自动吸附桌面边缘 →→→→→进入此内容的聊天室

来自 , 2019-10-28, 写在 C#, 查看 126 次.
URL http://www.code666.cn/view/42fe8808
  1. using System.Windows.Forms;
  2. using System;
  3. namespace **
  4. {
  5.  
  6. public partial class ***: form
  7. {
  8.         /* 【必要事件】:
  9.          * No.1:窗体的 Move 事件。
  10.          * No.2:窗体的 MouseEnter事件。
  11.          * No.3:MouseLeave事件。在窗体的“deactivated”事件 + 窗体中[所有的控件]leave的事件中都加入该事件。
  12.          * 将“**”更改为最终要使用的窗体的namespace。
  13.          */
  14.  
  15.         int intBianYuanXiangSu = 7; //边缘像素。即当窗体缩回去的时候,露出来多少像素,来提示窗体位置。
  16.  
  17.         /// <summary>
  18.         /// 自动粘合桌面边缘。
  19.         /// </summary>
  20.         /// <param name="intJingDu">自动粘合的精度,默认10像素。</param>
  21.         /// <param name="LeftRight">是否启动在接近桌面左右边缘时也自动粘合。</param>
  22.         private void myMethod_AutoAbsorb ( int intJingDu , bool LeftAndRight )
  23.         {
  24.  
  25.                 //小于10像素.则使用默认10像素。
  26.                 if ( intJingDu <= 10 )
  27.                         intJingDu = 10;
  28.  
  29.                 //只有在窗体!不在!桌面的边缘的时候,才开始判断是否开始吸附。
  30.                 if ( Left != SystemInformation.WorkingArea.Width - this.Size.Width && Left != 0 && Top != 0 )
  31.                 {
  32.  
  33.  
  34.                         //当窗体靠近桌面"上方"的时候,开始粘合。
  35.                         if ( Location.Y <= intJingDu && Location.Y > -this.Size.Height + intBianYuanXiangSu )
  36.                                 Top = 0;
  37.  
  38.                         if ( LeftAndRight == true )
  39.                         {
  40.                                 //当窗体靠近桌面的"左边缘"时候,开始粘合。
  41.                                 if ( this.Location.X <= intJingDu && Location.X > -Size.Width + intBianYuanXiangSu )
  42.                                         Left = 0;
  43.  
  44.                                 //当窗体靠近桌面“右边缘”的时候,开始粘合。WorkingArea获得桌面的尺寸的属性。
  45.                                 if ( Location.X >= ( SystemInformation.WorkingArea.Width - this.Size.Width - intJingDu )
  46.                                         && Left < SystemInformation.WorkingArea.Width - intBianYuanXiangSu )
  47.                                         Left = SystemInformation.WorkingArea.Width - Size.Width;
  48.                         }
  49.                 }
  50.         }
  51.  
  52.  
  53.         private void myMethod_AutoAbsorb_MouseEnter()
  54.         {
  55.  
  56.                 if ( Top == -this.Size.Height + intBianYuanXiangSu ) //上边缘
  57.                         this.Top = 0;
  58.  
  59.                 if ( Left == -this.Size.Width + intBianYuanXiangSu ) //左边缘
  60.                         this.Left = 0;
  61.  
  62.                 if ( Left == SystemInformation.WorkingArea.Width - intBianYuanXiangSu ) //右边缘
  63.                         this.Left = SystemInformation.WorkingArea.Width - this.Size.Width;
  64.  
  65.  
  66.         }
  67.  
  68.         //在窗体的deactivated事件 + 窗体中所有的控leave件的事件中都加入以下事件。这样勉强达到效果。
  69.         private   void myMethod_AutoAbsorb_MouseLeave()
  70.         {
  71.  
  72.                 //只有鼠标不在窗体的内部范围的时候,才开始缩回去。
  73.                 if ( ! ( ( Control.MousePosition.X >= Left && Control.MousePosition.X <= Left + Width ) &&
  74.                          ( Control.MousePosition.Y >= Top && Control.MousePosition.Y <= Top + Height ) ) )
  75.                 {
  76.                         if ( Top == 0 ) //上边缘
  77.                                 Top = -Size.Height + intBianYuanXiangSu;
  78.  
  79.                         if ( Left == 0 ) //左边缘
  80.                                 Left = -Size.Width + intBianYuanXiangSu;
  81.  
  82.                         if ( Left == SystemInformation.WorkingArea.Width - Size.Width ) //右边缘
  83.                                 Left = SystemInformation.WorkingArea.Width - intBianYuanXiangSu;
  84.  
  85.                 }
  86.  
  87.  
  88.         }
  89. }
  90. }

回复 "窗口自动吸附桌面边缘"

这儿你可以回复上面这条便签

captcha