台式电脑

电脑桌面在侧面怎么样设(当窗体处于屏幕边缘时隐藏窗口的方法)

当把软件窗口拖动到屏幕边缘时,让软件窗体隐藏,鼠标再次悬浮时再把它显示出来,

根据下面的代码给窗体添加一系列事件

当窗体处于屏幕边缘时隐藏窗口的方法

具体代码如下:

电脑桌面在侧面怎么样设(当窗体处于屏幕边缘时隐藏窗口的方法)

usingSystem.Runtime.InteropServices;namespace仿QQ侧边隐藏{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}[DllImport("user32.dll")]publicstaticexternboolReleaseCapture();[DllImport("user32.dll")]publicstaticexternboolSendMessage(IntPtrhwnd,intwMsg,intwParam,intlParam);publicconstintWM_SYSCOMMAND=0x0112;publicconstintSC_MOVE=0xF010;publicconstintHTCAPTION=0x0002;//窗体的宽高intformwidth;intformheight;privatevoidForm1_Move(objectsender,EventArgse){formwidth=this.Width;formheight=this.Height;intx=this.Location.X;inty=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;}}}privatevoidbutton1_Click(objectsender,EventArgse){this.Close();}}}

运行起来会发现效果是这样的:

当窗体处于屏幕边缘时隐藏窗口的方法

把标题栏去掉

当窗体处于屏幕边缘时隐藏窗口的方法

再次运行,调试程序,这个图不太好截,有需要的自己试一下吧。

相关新闻

返回顶部