最近客户提出一个需求,要实现类似QQ收缩窗体的功能。具体来说就是拖动窗体标题栏到屏幕顶部,松开鼠标,鼠标移出窗体区域后,窗体会自动收缩到屏幕边上。
主体功能实现起来相对比较容易,因为我在网上能找到现成的代码,是参考了这篇文章C#中winform窗口做成QQ窗口那样能收缩在电脑屏幕的上方-优快云博客。但是实际使用过程中又遇到一些坑:
1、windows系统有个默认操作,就是拖动窗体到屏幕顶上,自动变成最大化。处理起来相对简单,就是禁止窗口最大化。
this.MaximizeBox = false;
2、收缩后的窗体,虽然有露出一点在界面上,但是比较难找到。处理起来就麻烦点,参照QQ的做法,在桌面右下角增加系统托盘,双击托盘也可以展开窗体。
全部项目代码如下:
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 WindowsFormsApp2
{
public partial class Form1 : Form
{
System.Windows.Forms.Timer StopRectTimer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "仿QQ收缩窗体";
//实现类似QQ一样的面板收缩到屏幕边上
this.MaximizeBox = false;//禁止最大化,以免拖动标题栏到顶部的时候自动最大化
StopRectTimer = new System.Windows.Forms.Timer();
StopRectTimer.Tick += new EventHandler(timer1_Tick);
StopRectTimer.Interval = 50;
StopRectTimer.Enabled = true;
this.TopMost = true;
StopRectTimer.Start();
NotifyIcon MyNotifyIcon = new NotifyIcon();//实例化
MyNotifyIcon.Visible = true;//可见性
MyNotifyIcon.Text = this.Text;//鼠标放在托盘时显示的文字
MyNotifyIcon.BalloonTipText = this.Text;//气泡显示的文字
MyNotifyIcon.ShowBalloonTip(1000);//托盘气泡的显现时间
MyNotifyIcon.Icon = this.Icon;//托盘的外观(系统)
ContextMenu cm = new ContextMenu();
MenuItem mi = new MenuItem("退出");
mi.Click += Mi_Click;
cm.MenuItems.Add(mi);//添加托盘菜单项
MyNotifyIcon.ContextMenu = cm;//托盘菜单项在图标右键时显示
MyNotifyIcon.MouseDoubleClick += MyNotifyIcon_MouseDoubleClick;//托盘的鼠标窗机时间注册方法
}
private void Mi_Click(object sender, EventArgs e)
{
this.Close();
}
private void MyNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
StopRectTimer.Enabled = false;
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
}
MethodInvoker mi = new MethodInvoker(async () =>
{
// 延迟5秒
await Task.Delay(5000);
StopRectTimer.Enabled=true;
});
this.BeginInvoke(mi);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Bounds.Contains(Cursor.Position))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
}
}
else
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, (this.Height - 8) * (-1));
break;
case AnchorStyles.Left:
this.Location = new Point((-1) * (this.Width - 8), this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 8, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 8));
break;
}
}
}
internal AnchorStyles StopAanhor = AnchorStyles.None;
private void mStopAnhor()
{
if (this.Top <= 0 && this.Left <= 0)
{
StopAanhor = AnchorStyles.None;
}
else if (this.Top <= 0)
{
StopAanhor = AnchorStyles.Top;
}
else if (this.Left <= 0)
{
StopAanhor = AnchorStyles.Left;
}
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
{
StopAanhor = AnchorStyles.Right;
}
else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)
{
StopAanhor = AnchorStyles.Bottom;
}
else
{
StopAanhor = AnchorStyles.None;
}
}
private void Form1_LocationChanged(object sender, EventArgs e)
{
this.mStopAnhor();
}
}
}