当前位置: 首页 > 编程语言 > C#

在客户端桌面区域内移动窗体分享

时间:2023-04-11 11:27:05 C#

在客户端桌面区域内移动窗体我的目标是防止用户使用鼠标移动时部分或全部隐藏。比如我的桌面分辨率是1024×768,form位置属性的坐标有min/max值的“x/y”:0,1024-form.width,0,768-form.height。我正在使用一些本机APIi来完成所有这些并且一切正常,但我经常注意到我的表单刷新(闪烁)(我正在管理form_move事件)。似乎SuspendLayout方法无法正常工作,否则表单移动事件不会触发每个像素更改,但可能不止一个(请参阅下面的代码了解我的意思)。我的代码如下所示:privatevoidForm1_Move(objectsender,EventArgse){this.SuspendLayout();//桌面分辨率(API实用程序)intx=Desktop.GetXResolution();inty=Desktop.GetYResolution();//任务栏信息(API实用程序)任务栏tb=newTaskbar();intminX=0;intmaxX=x-这个宽度;intminY=0;intmaxY=y-this.Height;if(!tb.AutoHide){if(tb.Position!=TaskbarPosition.Unknown&&!tb.Size.IsEmpty){if(tb.Position==TaskbarPosition.Top)minY+=tb.Size.Height;switch(tb.Position){caseTaskbarPosition.Top:minY=tb.Size.Height;休息;caseTaskbarPosition.Bottom:maxY-=tb.Size.Height;休息;caseTaskbarPosition.Left:minX=tb.Size.Width;休息;caseTaskbarPosition.Right:maxX-=tb.Size.Width;休息;}}}//恢复X位置if(this.Location.XmaxX)this.Location=newPoint(maxX,this.Location.Y);//恢复Y坐标if(this.Location.YmaxY)this.Location=newPoint(this.Location.X,maxY);这个.Resume布局(假);正如我已经说过的,通常当我恢复Location属性时,我的winForm会闪烁任何建议将不胜感激。移动完成后移动。表格被移动,然后你把它移回去。无论如何它都会闪烁。相反,阻止它首先移动:privateconstintWM_MOVING=0x216;privatevoidWriteTheRect(IntPtrdest,Rectanglerect){System.Runtime.InteropServices.Marshal.WriteInt32(dest,0,rect.Left);System.Runtime.InteropServices.Marshal.WriteInt32(dest,4,rect.Top);System.Runtime.InteropServices.Marshal.WriteInt32(dest,8,rect.Right);System.Runtime.InteropServices.Marshal.WriteInt32(dest,12,rect.Bottom);}protectedoverridevoidWndProc(refMessagem){if(m.Msg==WM_MOVING){//lParam指向的RECT结构:left,top,right,bottomRectangler=Rectangle.FromLTRB(System.Runtime.InteropServices.Marshal.ReadInt32(m.LParam,0),System.Runtime.InteropServices.Marshal.ReadInt32(m.LParam,4),System.Runtime.InteropServices.Marshal.ReadInt32(m.LParam,8),System.Runtime.InteropServices.Marshal.ReadInt32(m.LParam,12));允许的矩形=Rectangle.FromLTRB(0,0,1600,900);如果(r.Left=allowed.Right||r.Bottom>=allowed.Bottom){intoffset_x=r.左允许。对吗?(allowed.Right-r.Right):(0));intoffset_y=r.Topallowed.Bottom?(allowed.Bottom-r.Bottom):(0));r.Offset(offset_x,offset_y);WriteTheRect(m.LParam,r);}}base.WndProc(refm);}显然,将constant替换成你想要的实际bounds如果对你有用,需要详细了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢