如何按比例调整窗体大小?C#

如何按比例调整窗体大小?C#,c#,winforms,resize,scale,C#,Winforms,Resize,Scale,我需要在我的表格中宽度是高度的两倍(1:2)当我调整大小时 我怎么能做到? 感谢您的帮助,对不起我的英语:)注册调整大小活动,并按字面意思执行以下操作: this.ClientSize.Width = this.ClientSize.Height * 2; 或用于完整表单大小(包括边框) 签出 您可以使用onLoad、onClick等事件在特定条件下调整大小。所以基本上由你决定 有一些标准表单属性“高度”和“宽度”,因此可以调整这些属性 例如: private void frmMain_Loa

我需要在我的表格中宽度是高度的两倍(1:2)当我调整大小时 我怎么能做到?
感谢您的帮助,对不起我的英语:)

注册调整大小活动,并按字面意思执行以下操作:

this.ClientSize.Width = this.ClientSize.Height * 2;
或用于完整表单大小(包括边框)

签出


您可以使用onLoad、onClick等事件在特定条件下调整大小。所以基本上由你决定

有一些标准表单属性“高度”和“宽度”,因此可以调整这些属性

例如:

private void frmMain_Load(object sender, EventArgs e)
    {
        int height = 500;
        frmMain.ActiveForm.Height = height;
        frmMain.ActiveForm.Width = height / 2;
    }
查看此帖子:

关键是响应消息,它允许您更改窗口矩形

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();

        // Necessary to take the window frame width/height into account
        this.chromeWidth = this.Width - this.ClientSize.Width;
        this.chromeHeight = this.Height - this.ClientSize.Height;
        this.ClientSize = new System.Drawing.Size(400, 200);
    }

    // ...

    #region Resizer
    private float constantWidth = 2;
    private float constantHeight = 1;

    private int chromeWidth;
    private int chromeHeight;

    // From Windows SDK
    private const int WM_SIZING = 0x214;

    private const int WMSZ_LEFT = 1;
    private const int WMSZ_RIGHT = 2;
    private const int WMSZ_TOP = 3;
    private const int WMSZ_BOTTOM = 6;

    struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SIZING)
        {
            RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));

            int w = rc.Right - rc.Left - chromeWidth;
            int h = rc.Bottom - rc.Top - chromeHeight;

            switch (m.WParam.ToInt32()) // Resize handle
            {
                case WMSZ_LEFT:
                case WMSZ_RIGHT:
                    // Left or right handles, adjust height
                    rc.Bottom = rc.Top + chromeHeight + (int)(constantHeight * w / constantWidth);
                    break;

                case WMSZ_TOP:
                case WMSZ_BOTTOM:
                    // Top or bottom handles, adjust width
                    rc.Right = rc.Left + chromeWidth + (int)(constantWidth * h / constantHeight);
                    break;

                case WMSZ_LEFT + WMSZ_TOP:
                case WMSZ_LEFT + WMSZ_BOTTOM:
                    // Top-left or bottom-left handles, adjust width
                    rc.Left = rc.Right - chromeWidth - (int)(constantWidth * h / constantHeight);
                    break;

                case WMSZ_RIGHT + WMSZ_TOP:
                    // Top-right handle, adjust height
                    rc.Top = rc.Bottom - chromeHeight - (int)(constantHeight * w / constantWidth);
                    break;

                case WMSZ_RIGHT + WMSZ_BOTTOM:
                    // Bottom-right handle, adjust height
                    rc.Bottom = rc.Top + chromeHeight + (int)(constantHeight * w / constantWidth);
                    break;
            }

            Marshal.StructureToPtr(rc, m.LParam, true);
        }

        base.WndProc(ref m);
    }
    #endregion
}

使用TableLayoutControl托管其他控件。这类似于HTML表

将控件添加到表中的单元格中,然后可以添加列样式和行样式,在其中可以设置单元格的宽度和高度。测量值可以指定为自动、绝对或百分比。百分比是您在这里真正需要的百分比

然后将此TableLayoutControl的dock设置为随窗口调整大小,单元格将根据先前设置的百分比按比例调整大小

要调整实际组件的大小,您必须设置其停靠锚定属性。任何指定控件应如何相对于其所在单元格调整大小的配置都将起作用,例如,您可能希望通过将控件的“锚定”属性设置为,使控件仅在单元格中垂直调整大小,并且仍然保持相同的宽度

AnchorStyle.Top | AnchorStyle.Bottom
简言之:

  • 设置控件相对于其单元格的大小调整方式
  • 设置相对于TableLayoutControl调整单元格大小的方式(使用ColumnStyles和RowStyles)
  • 设置TableLayoutControl相对于表单调整大小的方式

更多信息可以在这里找到:

要调整大小,下面的表格更好


WPF、WinForms或Web表单?这里需要注意的重要一点是,当用户拖动窗口手柄时,会发生这种情况,这比响应调整大小(随后发生)提供了更好的体验,因此,如果我只调整宽度,它总是会被重置。如果我只调整高度的另一方面,它将工作。作为一个用户,我会发现这非常令人困惑。最好检查两个答案中是否有一个没有更改,然后根据更改后的答案设置。请注意,不鼓励使用仅链接的答案,因此答案应该是搜索解决方案的终点(而不是另一个参考的中途停留,随着时间的推移,它往往会过时)。请考虑在这里添加一个独立的概要,将链接作为参考。
public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();

        // Necessary to take the window frame width/height into account
        this.chromeWidth = this.Width - this.ClientSize.Width;
        this.chromeHeight = this.Height - this.ClientSize.Height;
        this.ClientSize = new System.Drawing.Size(400, 200);
    }

    // ...

    #region Resizer
    private float constantWidth = 2;
    private float constantHeight = 1;

    private int chromeWidth;
    private int chromeHeight;

    // From Windows SDK
    private const int WM_SIZING = 0x214;

    private const int WMSZ_LEFT = 1;
    private const int WMSZ_RIGHT = 2;
    private const int WMSZ_TOP = 3;
    private const int WMSZ_BOTTOM = 6;

    struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SIZING)
        {
            RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));

            int w = rc.Right - rc.Left - chromeWidth;
            int h = rc.Bottom - rc.Top - chromeHeight;

            switch (m.WParam.ToInt32()) // Resize handle
            {
                case WMSZ_LEFT:
                case WMSZ_RIGHT:
                    // Left or right handles, adjust height
                    rc.Bottom = rc.Top + chromeHeight + (int)(constantHeight * w / constantWidth);
                    break;

                case WMSZ_TOP:
                case WMSZ_BOTTOM:
                    // Top or bottom handles, adjust width
                    rc.Right = rc.Left + chromeWidth + (int)(constantWidth * h / constantHeight);
                    break;

                case WMSZ_LEFT + WMSZ_TOP:
                case WMSZ_LEFT + WMSZ_BOTTOM:
                    // Top-left or bottom-left handles, adjust width
                    rc.Left = rc.Right - chromeWidth - (int)(constantWidth * h / constantHeight);
                    break;

                case WMSZ_RIGHT + WMSZ_TOP:
                    // Top-right handle, adjust height
                    rc.Top = rc.Bottom - chromeHeight - (int)(constantHeight * w / constantWidth);
                    break;

                case WMSZ_RIGHT + WMSZ_BOTTOM:
                    // Bottom-right handle, adjust height
                    rc.Bottom = rc.Top + chromeHeight + (int)(constantHeight * w / constantWidth);
                    break;
            }

            Marshal.StructureToPtr(rc, m.LParam, true);
        }

        base.WndProc(ref m);
    }
    #endregion
}
AnchorStyle.Top | AnchorStyle.Bottom