简体   繁体   English

如何在C#中停靠窗体?

[英]How to dock a windows form in C#?

I just would like to know if it is possible to dock a windows form on top of the user screen? 我只是想知道是否可以将窗体停靠在用户屏幕的顶部? I have been trying to do this by manually setting the position of my form to the coordinates I want. 我一直试图通过手动将表单的位置设置为我想要的坐标来做到这一点。 But using this method, however, allows the user to change the position of the form just by dragging it. 但是,使用此方法,用户只需拖动即可更改表单的位置。 I want to make the form docked to the upper portion of the screen since this window form will server as a menu for the project I am making. 我想使表单停靠在屏幕的上半部分,因为这个窗口表单将作为我正在制作的项目的菜单服务器。

Thanks a lot. 非常感谢。 :) :)

I would consider using the Control.Dock property along with one of the DockStyle enumeration values. 我会考虑使用Control.Dock属性以及其中一个DockStyle枚举值。

You might need to play with the Layout too, so that you may layout your form's controls differently depending on the DockStyle selected. 您可能还需要使用Layout ,以便根据所选的DockStyle以不同方式布置窗体的控件。

You will need, in my point of view, to consider the Control.Location property so that you get to know which DockStyle value to dock your form with. 在我看来,您需要考虑Control.Location属性,以便了解停靠表单的DockStyle值。

EDIT #1 编辑#1

Your Windows Form has a Dock property as it inherits from Control . 您的Windows窗体具有Dock属性,因为它继承自Control

Let's consider the following : 我们考虑以下几点:

  1. Each time your form comes closer to your right-side of the screen, for example, or of the MDI container, you want to dock right, right ? 例如,每当您的表单靠近屏幕右侧或MDI容器时,您想要正确对接,对吗? (Little word play here... =P) So, you have to subscribe to the Control.LocationChanged event. (这里的小字游戏...... = P)所以,你必须订阅Control.LocationChanged事件。

     private void myForm_LocationChanged(object sender, EventArgs e) { if (this.Location.X > 900) then this.Dock = DockStyle.Right; else if (this.Location.X < 150) then this.Dock = DockStyle.Left; else if (this.Location.Y > 600) then this.Dock = DockStyle.Bottom; else if (this.Location.Y < 150) then this.Dock = DockStyle.Top; else this.Dock = DockStyle.None; } 

Indeed, instead of constant values, you should use the current desktop resolution and calculate a ratio from it where you want your docking to occur. 实际上,您应该使用当前的桌面分辨率而不是常量值,并计算您希望进行对接的比率。

***Disclaimer:****This code is provided as-is and has not been tested. ***免责声明:****此代码按原样提供,尚未经过测试。 This algorithm is hopefully enough to guide you through the docking process as you need it. 希望这个算法足以指导您完成对接过程。 Further assistance may be brought upon request.* =) 可根据要求提供进一步的帮助。* =)

It seems the Form.DesktopLocation property is the righter tool for the job as for your main window, meaning your MDI container, for instance. 看起来Form.DesktopLocation属性是作为主窗口的作业的更好的工具,例如,意味着您的MDI容器。 As for the other windows, I would go along with something that looks like the code sample provided. 至于其他窗口,我会选择看起来像提供的代码示例的东西。

Does this help? 这有帮助吗?

EDIT #2 编辑#2

If you want to prevent Form's overlapping, perhaps the Control.BringToFront() method could do it before or after your call to the Control.Show() method, depending on what works best for you. 如果要防止Form重叠,可能Control.BringToFront()方法可以在调用Control.Show()方法之前或之后执行此操作,具体取决于哪种方法最适合您。

So after some tweaks I finally was able to get this code working. 经过一些调整后,我终于能够使这段代码正常工作了。

this.DesktopLocation = new Point((Screen.PrimaryScreen.Bounds.Width / 2 - 420), 0);

I placed that line below the InitializeComponent() and it docks my form to the center of the screen with whatever resolution values. 我将该行放在InitializeComponent()下面,它将我的表单停靠在屏幕中心,无论分辨率值是多少。

By setting the FormBorderStyle of your form to None , you take the drag handle away from the user so they cannot move it via the mouse. 通过将窗体的FormBorderStyle设置为None ,可以将拖动手柄从用户移开,这样他们就无法通过鼠标移动它。

Then you just need to place it where you want. 然后你只需要把它放在你想要的地方。

If you really want to take away the users options you can also set the ShowInTaskbar property to false 如果您确实想要ShowInTaskbar用户选项,还可以将ShowInTaskbar属性设置为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM