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

无边框移动窗口分享

时间:2023-04-10 18:52:51 C#

无边框移动窗口如何移动无边框窗口。应用程序上没有空白区域,所有可用内容都是网络浏览器和菜单条。我希望用户能够通过拖动菜单栏来移动窗口。我应该如何编码?我尝试了一些在网上找到的代码块,但没有一个有效。这篇代码项目文章应该可以帮助您做到这一点。我自己没有用过这个问题。这是它的要点:publicconstintWM_NCLBUTTONDOWN=0xA1;publicconstintHT_CAPTION=0x2;[DllImportAttribute("user32.dll")]publicstaticexternintSendMessage(IntPtrhWnd,intMsg,intwParam,intlParam);[DllImportAttribute("user32.dll")]publicstaticexternboolReleaseCapture();privatevoidForm1_MouseDown(objectsender,System.Windows.Forms.MouseEventArgse){if(e.Button==MouseButtons.Left){ReleaseCapture();SendMessage(句柄,WM_NCLBUTTONDOWN,HT_CAPTION,0);}}这基本上会“欺骗”窗口管理器,使其认为它正在获取winform的标题栏。要将它应用于您的项目,只需使用MenuStrip中的MouseDown事件。这是.Net方式privatebooldragging=false;私人点dragCursorPoint;私有点dragFormPoint;privatevoidForm1_MouseDown(objectsender,MouseEventArgse){dragging=true;dragCursorPoint=Cursor.Position;dragFormPoint=this.Location;(objectsender,MouseEventArgse){if(dragging){Pointdif=Point.Subtract(Cursor.Position,newSize(dragCursorPoint));this.Location=Point.Add(dragFormPoint,newSize(dif));}}privatevoidForm1_MouseUp(objectsender,MouseEventArgse){dragging=false;}就这样。您可以伪造菜单条,例如使用带有选项卡的面板。然后你可以手动处理:当用户点击一个标签时,弹出菜单会打开,当用户拖动标签时,窗口会移动。但我建议不要使用这些解决方法,因为它不是标准的GUI行为,您可能会使用户感到困惑。我没试过,但如果你可以处理菜单栏上的“OnMouseDown”和“onMouseUp”事件:如果你使用的是Panel,你必须在this.panel1.MouseDown+=newSystem.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);这是在YourForm.cspublicconstintWM_NCLBUTTONDOWN=0xA1;publicconstintHT_CAPTION=0x2;[DllImportAttribute("user32.dll")]publicstaticexternintSendMessage(IntPtrhWnd,intMsg,intwParam,intlParam);[DllImportAttribute("user32.dll")]publicstaticexternboolReleaseCapture();privatevoidpanel1_MouseDown(objectsender,System.Windows.Forms.MouseEventArgse){if(e.Button==MouseButtons.Left){ReleaseCapture();SendMessage(句柄,WM_NCLBUTTONDOWN,HT_CAPTION,0);}}MbithiKioko走在正确的轨道上,但我会的。booldragging=false;intxOffset=0;intyOffset=0;privatevoidForm1_MouseDown(objectsender,MouseEventArgse){dragging=true;xOffset=Cursor.Position.X-this.Location.X;yOffset=Cursor.Position。Y-这个.Location.Y;}privatevoidForm1_MouseMove(objectsender,MouseEventArgse){if(dragging){this.Location=newPoint(Cursor.Position.X-xOffset,Cursor.Position.Y-yOffset);这个.Update();}}privatevoidForm1_MouseUp(objectsender,MouseEventArgse){dragging=false;我不得不使用System.Runtime.InteropServices.DllImportAttribute-只是想我会发表评论并让大家知道。把起点放到一个二维数组中即可,如下图:以上就是C#学习教程:无边框移动窗口的全部内容。如果对大家有用,需要详细了解C#学习教程,希望大家多多关注——publicpartialclassmainForm:Form{//移动无边框窗体的全局变量privatebooldragging=false;私人点startPoint=新点(0,0);publicmainForm(){InitializeComponent();}privatevoidmainForm_MouseDown(objectsender,MouseEventArgse){dragging=true;起点=新点(eX,eY);}privatevoidmainForm_MouseUp(objectsender,MouseEventArgse){dragging=false;}privatevoidmainForm_MouseMove(objectsender,MouseEventArgse){if(dragging){Pointp=PointToScreen(e.Location);Location=newPoint(pX-this.startPoint.X,pY-this.startPoint.Y);}}}本文收集自网络,不代表立场。如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: