繁体   English   中英

根据屏幕分辨率调整表单控件的大小

[英]To Resize form controls According To Screen Resolutions

我正在设计一个分辨率为1366x768的dotnet Window应用程序,当屏幕的分辨率低于给定的屏幕分辨率时,我无法调整表单控件的大小,是否有任何解决方案可以调整表单控件的大小以降低分辨率也。 到目前为止,我已经尝试了以下代码。 当分辨率高于给定分辨率时,它会很好地工作。

 private void masterform_Resize(object sender, EventArgs e)
        {

            double RW = (this.Width - CW) / CW;
            double RH = (this.Height - CH) / CH;
            foreach (Control Ctrl in Controls)
            {
                Ctrl.Width += Convert.ToInt32(Ctrl.Width * RW);
                Ctrl.Height += Convert.ToInt32(Ctrl.Height * RH);
                Ctrl.Left += Convert.ToInt32(Ctrl.Left * RW);
                Ctrl.Top += Convert.ToInt32(Ctrl.Top * RH);
            }
            CW = this.Width;
            CH = this.Height;
        }


  private void masterform_Load(object sender, EventArgs e)
        {
            IW = this.Width;
            IH = this.Height;`enter code here`

        }   

让我知道是否有解决方案。

您可以使用“ Table Layout Panel + Anchor让表单处理每个分辨率中的控件大小。

为此,您可以按照以下说明进行操作:

创建一个表格,放置表格布局面板,根据需要设置行和列, 不要忘记将列宽度设置为“ Percent然后将控件放入单元格中(或先将面板放入单元格中,然后将控件置于面板上) ,将锚点设置为left + right即可。

看下面的图片:

PIC1

PIC2

尝试这个

private Size oldSize;
private void Form1_Load(System.Object sender, System.EventArgs e)
{
    oldSize = base.Size;
}
protected override void OnResize(System.EventArgs e)
{
    base.OnResize(e);
    foreach (Control cnt in this.Controls) {
        ResizeAll(cnt, base.Size);
    }
    oldSize = base.Size;
}
private void ResizeAll(Control cnt, Size newSize)
{
    int iWidth = newSize.Width - oldSize.Width;
    cnt.Left += (cnt.Left * iWidth) / oldSize.Width;
    cnt.Width += (cnt.Width * iWidth) / oldSize.Width;

    int iHeight = newSize.Height - oldSize.Height;
    cnt.Top += (cnt.Top * iHeight) / oldSize.Height;
    cnt.Height += (cnt.Height * iHeight) / oldSize.Height;
}

这里的描述

暂无
暂无

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

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