2

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

4

6 回答 6

7

查看这篇文章:在保持纵横比的同时调整表单大小

关键是响应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
}
于 2012-04-19T15:14:57.457 回答
1

使用TableLayoutControl来承载您的其他控件。这与 HTML 表的工作方式类似。

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

然后,您将此TableLayoutControl的停靠栏设置为与您的窗口一起调整大小,并且单元格将根据您之前设置的百分比按比例调整大小。

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

AnchorStyle.Top | AnchorStyle.Bottom

简而言之:

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

更多信息可以在这里找到:http: //msdn.microsoft.com/en-us/vstudio/bb798032.aspx

于 2012-11-22T10:34:05.663 回答
0

调整下面的表格是更好的一个

http://niravdaraniya.blogspot.in/2013/07/how-to-resize-form-in-cnet.html

于 2013-07-23T03:51:15.657 回答
0

注册到 resize 事件并按字面意思执行此操作:

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

或完整的表格尺寸(包括边框)

this.Size.Width = this.Size.Height * 2;
于 2012-04-19T15:03:17.107 回答
0

检查Control.Resize 事件——也

private void Form1_Resize(object sender, System.EventArgs e)
{   
    Control control = (Control)sender;
    control.Width = control.Height * 2; 
}
于 2012-04-19T15:04:18.647 回答
0

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

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

例如:

private void frmMain_Load(object sender, EventArgs e)
    {
        int height = 500;
        frmMain.ActiveForm.Height = height;
        frmMain.ActiveForm.Width = height / 2;
    }
于 2012-04-19T15:11:29.923 回答