如何按比例调整C#表单大小?

3

我希望在我的表单中,宽度是高度的两倍(1:2),并且当我调整大小时也保持这个比例。我该怎么做?谢谢您的帮助,对我的英语表示抱歉 :)


6
WPF、WinForms还是Web表单? - undefined
6个回答

8

请查看此篇文章:保持宽高比的窗体缩放

关键在于响应WM_SIZING消息,这可以让你更改窗口矩形。

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
}

4
这里需要注意的重要一点是,这将在用户拖动窗口手柄时发生,这比响应“调整大小”(稍后发生)要好得多,能提供更好的体验。 - undefined

1
使用 TableLayoutControl 来托管其他控件。这类似于 HTML 表格。
您可以将控件添加到表格中的单元格中,然后可以添加 ColumnStyles 和 RowStyles,在其中设置单元格的宽度和高度。测量值可以指定为自动、绝对或百分比。在这里,您真正需要的是百分比。
然后,将此 TableLayoutControl 的停靠设置为随窗口调整大小,单元格将按照您之前设置的百分比进行 比例 调整大小。
要使实际组件也调整大小,必须设置它们的 DockAnchor 属性之一。任何指定控件应如何相对于其所在的单元格调整大小的配置都可以工作,例如,您可能希望仅在单元格中垂直调整控件的大小,并通过将控件的 Anchor 属性设置为来保持相同的宽度。
AnchorStyle.Top | AnchorStyle.Bottom

简而言之:
  • 设置控件相对于其单元格的调整方式
  • 使用ColumnStyles和RowStyles设置单元格相对于TableLayoutControl的调整方式
  • 设置TableLayoutControl相对于表单的调整方式

更多信息请参见:http://msdn.microsoft.com/en-us/vstudio/bb798032.aspx


0

1
注意,不鼓励只提供链接作为答案,Stack Overflow(SO)的答案应该是对解决方案搜索的终点(而不是另一个容易过时的参考站点)。请考虑在此处添加一个独立的概要,将链接作为参考。 - undefined

0

注册 resize 事件并直接执行以下操作:

this.ClientSize.Width = this.ClientSize.Height * 2;

或者对于完整的表单尺寸(包括边框)

this.Size.Width = this.Size.Height * 2;

0
检查 Control.Resize 事件 -- 还有
private void Form1_Resize(object sender, System.EventArgs e)
{   
    Control control = (Control)sender;
    control.Width = control.Height * 2; 
}

2
所以,如果我只调整宽度,它总是被重置。另一方面,如果我只调整高度,它会正常工作。作为用户,我会觉得这非常令人困惑。最好的做法是检查两者中是否有一个保持不变,然后根据发生改变的那个来设置另一个。 - undefined

0

你可以使用像onLoad、onClick等事件,在特定条件下进行调整大小。所以基本上这取决于你。

还有一些标准的表单属性,比如高度和宽度,你可以调整这些属性。

例如:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接