c# - 如何按比例调整表格大小? C#

标签 c# winforms resize scale

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

最佳答案

查看这篇文章:Resizing forms while keeping aspect ratio .

关键是回应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
}

关于c# - 如何按比例调整表格大小? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10230920/

相关文章:

C#相当于java的Graphics2D

c# - 每三次迭代 C#

c# - ASP.NET Core 3.1 InProcess 托管应用程序在启动异常后未重新启动

c# - 如何捕捉结束调整窗口?

javascript - 调整 iframe Internet Explorer 9 的大小

windows - 哪种屏幕尺寸更适合最终用户

c# - 全屏 WPF 应用程序的分辨率

c# - 事件处理程序中的 "reentrant call to SetCurrentCellAddressCore"- 仅当单元格行索引和列索引相等时

c# - 在运行时调整控件的大小

c# - 如何修复 ListView 中的控件重影问题?