English 中文(简体)
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. :)

最佳回答

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.

问题回答

I would consider using the Control.Dock property along with one of the DockStyle enumeration values.

You might need to play with the Layout too, so that you may layout your form s controls differently depending on the DockStyle selected.

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.

EDIT #1

Your Windows Form has a Dock property as it inherits from 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 ? (Little word play here... =P) So, you have to subscribe to the Control.LocationChanged event.

    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. As for the other windows, I would go along with something that looks like the code sample provided.

Does this help?

EDIT #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.

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.

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





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签