当窗体离屏幕四周一定距离时,改变窗体位置,引导窗体靠边;靠边后,当鼠标离开窗体时,改变窗体位置,窗体隐藏,凸出一点在屏幕内;隐藏后,当鼠标移到窗体时,窗体显示。
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(); } } }
示例项目:http://download.csdn.net/detail/u011689438/8823845
时间: 2024-10-13 01:01:11