How to attach a Windows form in C #?

I just wanted to know if it is possible to attach a window shape over the user's screen? I tried to do this by manually setting the position of my form to the desired coordinates. However, using this method allows the user to reposition the form by simply dragging it. I want to make the form attached to the top of the screen, as this window form will be the server as a menu for the project I am creating.

Many thanks.:)

+6
c # winforms window dock form-control
source share
3 answers

So, after some tweaks, I was finally able to run this code.

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

I put this line below InitializeComponent () and it binds my form to the center of the screen with any resolution values.

+1
source share

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

You may need to play with Layout so that you can layout control forms in different ways depending on the DockStyle selected.

You, in my opinion, will need to consider the Control.Location property so that you know what DockStyle value to attach your form with.

EDIT NO. 1

Your Windows form has a Dock property because it inherits from Control .

Consider the following:

  • Every time your form approaches your right side of the screen, for example, or the MDI container, you want to dock to the right, right? (A small word plays here ... = P) So, you should 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; } 

In fact, instead of constant values, you should use the current desktop resolution and calculate the coefficient from it where you want your docking to occur.

*** Disclaimer: **** This code is provided as is and has not been tested. This algorithm, we hope, is enough to guide you through the docking process when you need it. Additional assistance is available upon request. * =)

It seems that the Form.DesktopLocation property is a more convenient tool for setting, as for your main window, which means your MDI container, for example, As for other windows, I would agree with something that looks like the provided code sample.

Does it help?

EDIT No. 2

If you want to prevent the form from matching, perhaps the Control.BringToFront() method could do this before or after your call to Control.Show() , depending on what suits you best.

+3
source share

By setting the FormBorderStyle your form to None , you take the drag handle from the user so that he cannot move it with the mouse.

Then you just need to place it where you want.

If you really want to remove user settings, you can also set the ShowInTaskbar property to false

0
source share

All Articles