當窗體離屏幕四周一定距離時,改變窗體位置,引導窗體靠邊;靠邊后,當鼠標離開窗體時,改變窗體位置,窗體隱藏,凸出一點在屏幕內;隱藏后,當鼠標移到窗體時,窗體顯示。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FrmZoom { public partial class FrmMain : Form { // 窗體是否引導 bool isGuide = false; // 窗體是否隱藏 bool isShrink = false; // 窗體引導距離 const int GUIDE_DISTANCE = 15; // 窗體靠邊凸出部分距離 const int BULGE_DISTANCE = 3; public FrmMain() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 設置500毫秒檢查一次窗體是否需要縮放 this.timZoom.Interval = 500; // 啟動窗體縮放事件 this.timZoom.Enabled = true; } /// <summary> /// 引導窗體 /// </summary> private void GuideFrm() { /* 判斷窗體邊緣是否進入引導距離 * true 靠邊,“isGuide = false;”,窗體已引導 * false “isGuide = false;”,窗體未引導 */ if (this.Left < GUIDE_DISTANCE && this.Left > -GUIDE_DISTANCE) { this.Left = 0; isGuide = true; } else if (this.Top < GUIDE_DISTANCE && this.Top > -GUIDE_DISTANCE) { this.Top = 0; isGuide = true; } else if (this.Right < (Screen.GetWorkingArea(this).Width + GUIDE_DISTANCE) && this.Right > (Screen.GetWorkingArea(this).Width - GUIDE_DISTANCE)) { this.Left = Screen.GetWorkingArea(this).Width - this.Width; isGuide = true; } else { isGuide = false; } } /// <summary> /// 隱藏窗體 /// </summary> private void HideFrm() { /* 窗體是否靠邊 * true 窗體隱藏 * false */ if (this.Left == 0) { this.Left = -(this.Width - BULGE_DISTANCE); } else if (this.Top == 0) { this.Top = -(this.Height - BULGE_DISTANCE); } else if (this.Right == (Screen.GetWorkingArea(this).Width)) { this.Left = Screen.GetWorkingArea(this).Width - BULGE_DISTANCE; } // 窗體已隱藏 isShrink = true; // 窗體引導關閉 isGuide = false; // 窗體置頂 this.TopMost = true; // 在Windows任務欄中不顯示窗體 this.ShowInTaskbar = false; } /// <summary> /// 顯示窗體 /// </summary> private void ShowFrm() { // 窗體是否靠邊 if (this.Left == -(this.Width - BULGE_DISTANCE)) { this.Left = 0; } else if (this.Top == -(this.Height - BULGE_DISTANCE)) { this.Top = 0; } else if (this.Right == Screen.GetWorkingArea(this).Width + this.Width - BULGE_DISTANCE) { this.Left = Screen.GetWorkingArea(this).Width - this.Width; } // 窗體未隱藏 isShrink = false; // 窗口不置頂 this.TopMost = false; // 在Windows任務欄中顯示窗體 this.ShowInTaskbar = true; } /// <summary> /// 窗體縮放 /// </summary> private void ZoomFrm() { // 獲取鼠標位置 int mouseX = MousePosition.X; int mouseY = MousePosition.Y; // 獲取窗體位置 int frmX = this.Location.X; int frmY = this.Location.Y; /* 鼠標是否在窗體內 * true 顯示窗體 * false 隱藏窗體 */ if (mouseX > frmX - 1 && mouseX < frmX + this.Width + 1 && mouseY > frmY - 1 && mouseY < frmY + this.Height + 1) { // 窗體隱藏時才顯示窗體 if (isShrink) { ShowFrm(); } } else { // 窗體引導時才隱藏窗體 if (isGuide) { HideFrm(); } } } /// <summary> /// 移動窗體事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_Move(object sender, EventArgs e) { // 引導窗體 GuideFrm(); } /// <summary> /// 窗體縮放事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timZoom_Tick(object sender, EventArgs e) { // 縮放窗體 ZoomFrm(); } } }