当把软件窗口拖动到屏幕边缘时,让软件窗体隐藏,鼠标再次悬浮时再把它显示出来,
根据下面的代码给窗体添加一系列事件
具体代码如下:
using System.Runtime.InteropServices;namespace 仿QQ侧边隐藏{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static extern bool ReleaseCapture(); public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; //窗体的宽高 int formwidth; int formheight; private void Form1_Move(object sender, EventArgs e) { formwidth = this.Width; formheight = this.Height; int x = this.Location.X; int y = this.Location.Y; if (x <= 0) { this.Location = new System.Drawing.Point(0, y); this.Width = 0; } if (y <= 0) { this.Location = new System.Drawing.Point(x, 0); this.Height = 0; } } private void Form1_MouseEnter(object sender, EventArgs e) { this.Width = formwidth; this.Height = formheight; } private void Form1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } private void Form1_MouseLeave(object sender, EventArgs e) { int nowx = Cursor.Position.X; int nowy = Cursor.Position.Y; if (nowx >= (this.Location.X + this.Width) | nowx <= this.Location.X | nowy <= this.Location.Y | nowy >= (this.Location.Y + this.Height)) { if (this.Location.X == 0) { this.Width = 0; } if (this.Location.Y == 0) { this.Height = 0; } } } private void button1_Click(object sender, EventArgs e) { this.Close(); } }}
运行起来会发现效果是这样的:
把标题栏去掉
再次运行,调试程序,这个图不太好截,有需要的自己试一下吧。