代码库の自动粘合桌面边缘
- using System.Windows.Forms;
- using System;
- namespace **
- {
-
- public partial class ***: form
- {
- /* 【必要事件】:
- * No.1:窗体的 Move 事件。
- * No.2:窗体的 MouseEnter事件。
- * No.3:MouseLeave事件。在窗体的“deactivated”事件 + 窗体中[所有的控件]leave的事件中都加入该事件。
- * 将“**”更改为最终要使用的窗体的namespace。
- */
-
- int intBianYuanXiangSu = 7; //边缘像素。即当窗体缩回去的时候,露出来多少像素,来提示窗体位置。
-
- /// <summary>
- /// 自动粘合桌面边缘。
- /// </summary>
- /// <param name="intJingDu">自动粘合的精度,默认10像素。</param>
- /// <param name="LeftRight">是否启动在接近桌面左右边缘时也自动粘合。</param>
- private void myMethod_AutoAbsorb(int intJingDu , bool LeftAndRight)
- {
-
- //小于10像素.则使用默认10像素。
- if ( intJingDu <= 10 )
- intJingDu = 10;
-
- //只有在窗体!不在!桌面的边缘的时候,才开始判断是否开始吸附。
- if ( Left != SystemInformation.WorkingArea.Width - this.Size.Width && Left != 0 && Top != 0 )
- {
-
-
- //当窗体靠近桌面"上方"的时候,开始粘合。
- if ( Location.Y <= intJingDu && Location.Y > -this.Size.Height + intBianYuanXiangSu )
- Top = 0;
-
- if ( LeftAndRight == true )
- {
- //当窗体靠近桌面的"左边缘"时候,开始粘合。
- if ( this.Location.X <= intJingDu && Location.X > -Size.Width + intBianYuanXiangSu )
- Left = 0;
-
- //当窗体靠近桌面“右边缘”的时候,开始粘合。WorkingArea获得桌面的尺寸的属性。
- if ( Location.X >= ( SystemInformation.WorkingArea.Width - this.Size.Width - intJingDu )
- && Left < SystemInformation.WorkingArea.Width - intBianYuanXiangSu )
- Left = SystemInformation.WorkingArea.Width - Size.Width;
- }
- }
- }
-
-
- private void myMethod_AutoAbsorb_MouseEnter()
- {
-
- if ( Top == -this.Size.Height + intBianYuanXiangSu ) //上边缘
- this.Top = 0;
-
- if ( Left == -this.Size.Width + intBianYuanXiangSu ) //左边缘
- this.Left = 0;
-
- if ( Left == SystemInformation.WorkingArea.Width - intBianYuanXiangSu ) //右边缘
- this.Left = SystemInformation.WorkingArea.Width - this.Size.Width;
-
-
- }
-
- //在窗体的deactivated事件 + 窗体中所有的控leave件的事件中都加入以下事件。这样勉强达到效果。
- private void myMethod_AutoAbsorb_MouseLeave()
- {
-
- //只有鼠标不在窗体的内部范围的时候,才开始缩回去。
- if ( !( ( Control.MousePosition.X >= Left && Control.MousePosition.X <= Left + Width ) &&
- ( Control.MousePosition.Y >= Top && Control.MousePosition.Y <= Top + Height ) ) )
- {
- if ( Top == 0 ) //上边缘
- Top = -Size.Height + intBianYuanXiangSu;
-
- if ( Left == 0 ) //左边缘
- Left = -Size.Width + intBianYuanXiangSu;
-
- if ( Left == SystemInformation.WorkingArea.Width - Size.Width ) //右边缘
- Left = SystemInformation.WorkingArea.Width - intBianYuanXiangSu;
-
- }
-
-
- }
- }
- }