C# winform dynamic control sizing
I wa开发者_运维知识库nt my winform app controls to be at a certain location and a certain size no matter what screen resolution the user is using.
Could someone give me a quick example of how to shape a panel to start at 60% down on the screen to 100% down on the screen, and to be the full length of the screen?
I hope that made sense, so basically no matter what the screen resolution is, the panel will take up 40% of the winform starting 60% down on the app.
Thanks!
I want the same % to be no matter if the resolution is 800X600 or 1024X760.
You can use SplitContainer, which divides the form into two areas. If you change it's orientation to horizontal, you get 2 areas, one which starts on the top of the screen and the second on the other side. Now you can define minimal sizes of each of the panel:
SplitContainer.Panel1MinSize = Convert.ToInt32(0.6 * Form.ActiveForm.ClientSize.Height);
SplitContainer.Panel2MinSize = Convert.ToInt32(0.4 * Form.ActiveForm.ClientSize.Height);
This makes the top panel take 60% of the client area and the bottom 40% of it. It'll automatically start 60% down on the form. Then you can put any other control you want into the panel and dock it to the panel using Control.Dock = DockStyle.(something)
The other way how to change the position of any control is to basically compute it's position and size. You can get the width of the client area as Form.ClientSize.Width
and the beginning X, Y position of the 60% down on the app as:
int X = 0; // Leftmost
int Y = Convert.ToInt32(0.6 * Form.ClientSize.Height); // 60% from topmost point
Now you can set the control's size and position as:
Control.Size = new Size(Form.ClientSize.Width, Convert.ToInt32(0.4 * Form.ClientSize.Height));
Control.Location = new Point(0, Convert.ToInt32(0.6 * Form.ClientSize.Height);
So if you'd like to force for example label1
in Form1
to act like you wrote in your post, one possibility would be this:
private void Form1_Resize(object sender, EventArgs e)
{
label1.Size = new Size(this.ClientSize.Width, Convert.ToInt32(0.4 * this.ClientSize.Height));
label1.Location = new Point(0, Convert.ToInt32(0.6 * this.ClientSize.Height));
}
To keep the form the same size (in pixels) as you designed it, set the AutoSize property to false, the AutoScaleMode property to None, and use one of the 'Fixed' options in the FormBorderStyle. This should suppress all attempts by the system or by the user to chnage the size of the form. That means it'll be the samesize (in pixels) but this may seem bigger or smaller on different screen resolutions.
If you want the form to stay the "same size" relative to the screen, then you need to leave it resizable and read the Screen size on startup and set the Location and Size properties of the form so that it uses the relative area of the screen that you desire.
You can use a static class named Screen
that is in the System.Windows.Forms
.
Just set your panel's width and height as follows:
panel.Height = Screen.PrimaryScreen.WorkingArea.Height;
panel.Width = Screen.PrimaryScreen.WorkingArea.Width * 40 / 100;
and you can set the top of your panel by using this object properties. But I have to notice that if your panel is in another form, the Top & Left properties of it are relative to containing form obviously.
精彩评论