移动窗口无边框
如何移动没有边框的窗口。应用程序上没有空白空间,所有可用的只是网络浏览器和菜单条。我希望用户能够通过拖动菜单条来移动窗口。我该如何编码?我尝试了一些在网上找到的代码块,但没有一个起作用。
How do I move a window that does not have a border. There is no empty space on the application, all that is available is a webbrowser and a menustrip. I would like the users to be able to move the window by dragging the menu strip. How do I code this? I have tried a few code blocks I have found online, but none of them worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这篇代码项目文章应该可以帮助您完成此任务。我自己用过这个,没有任何问题。这就是它的要点:
这基本上会“欺骗”窗口管理器,使其认为它正在抓取 winform 的标题栏。
要将其应用到您的项目中,只需使用 MenuStrip 中的 MouseDown 事件即可。
This Code Project article should help you accomplish this. I've used this myself with no problems. This is the jist of it:
This will basically "trick" the window manager into thinking that it is grabbing the title bar of the winform.
To apply it to your project, just use the MouseDown event from the MenuStrip.
这就是 .Net 方式
。
Here is the .Net Way
that's it.
只需将起点放入二维数组中,如下所示:
Just put the start point into an 2D Array like this:
您可以伪造菜单条,例如使用带有标签的面板。然后您可以手动处理此问题:当用户单击标签时,将打开一个弹出菜单,当用户拖动标签时,窗口将移动。但我建议不要使用这种解决方法,因为它不是标准的 GUI 行为,并且您可能会让用户感到困惑。
You can fake your menustrip, for example using a panel with a label instead. And then you can handle this manually: when the user clicks the label, a popup menu will open, and when the user drags the label, the window will move. But I would advise against such workarounds, because it's not a standard GUI behavior, and you might get your users confused.
我还没有尝试过,但是如果您可以处理菜单栏上的“OnMouseDown”和“onMouseUp”事件:
I haven't tried it, but if you can handle the "OnMouseDown" and "onMouseUp" events on the menu bar:
如果您使用的是面板,则必须将其添加到
YourForm.Designer.cs
中
中,并将其添加到YourForm.cs
If you are using a Panel you have to add this in the
YourForm.Designer.cs
and this in the
YourForm.cs
Mbithi Kioko 走在正确的轨道上,但我会这样做。
Mbithi Kioko is on the right track but i would do it this way.
我必须使用 System.Runtime.InteropServices.DllImportAttribute - 只是想我会发表评论并让大家知道。
I had to use
System.Runtime.InteropServices.DllImportAttribute
- just thought I would comment and let you all know.