C# winform 动态控件大小调整 [英] C# winform dynamic control sizing

查看:165
本文介绍了C# winform 动态控件大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论用户使用何种屏幕分辨率,我都希望我的 winform 应用控件位于特定位置和特定大小.

I want my winform app controls to be at a certain location and a certain size no matter what screen resolution the user is using.

谁能给我一个简单的例子,说明如何将面板塑造成从屏幕向下 60% 开始到屏幕向下 100% 并成为屏幕的全长?

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?

我希望这是有道理的,所以基本上无论屏幕分辨率是多少,面板都将占据 winform 的 40%,从应用程序上的 60% 开始.

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.

谢谢!

无论分辨率是 800X600 还是 1024X760,我都希望相同的百分比.

I want the same % to be no matter if the resolution is 800X600 or 1024X760.

推荐答案

您可以使用 SplitContainer,将表单分为两个区域.如果将其方向更改为水平方向,则会得到 2 个区域,一个从屏幕顶部开始,第二个在另一侧.现在您可以定义每个面板的最小尺寸:

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);

这使得顶部面板占据了客户区的 60%,底部占据了它的 40%.它会自动在表单上开始 60%.然后您可以将任何其他您想要的控件放入面板并使用 Control.Dock = DockStyle.(something)

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)

另一种改变任何控件位置的方法是基本上计算它的位置和大小.您可以将客户区的宽度设为 Form.ClientSize.Width 以及 应用程序向下 60% 的 X、Y 开始位置为:

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);

因此,如果您想在 Form1 中强制 例如 label1 表现得像您在帖子中写的那样,一种可能性是这个:

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));
}

这篇关于C# winform 动态控件大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆