如何在调整窗体应用程序大小时保持纵横比?

4
我需要一些关于窗体调整大小的帮助,Form1默认以800x600的大小打开,但它会使用编程方式更改大小。下面的代码可以保持窗体的纵横比,但是当重新调整窗口大小时,纵横比会失效,我无法解决这个问题。
举个例子,当前窗口大小为宽度800,高度600,如果重新调整大小,应该保持纵横比。比如说,如果窗口被调整为宽度850,那么我期望高度是650。对吗?
    public Form1()
    {
        InitializeComponent();

        // Default Window Size
        ClientSize = new Size(800, 600);
        chromeWidth = Width - ClientSize.Width;
        chromeHeight = Height - ClientSize.Height;


    }

    // Window form Size changes programatically from a size handler, updating "ClientSize".


    //////////////////////////// Resize /////////////////////////////////////

    #region Resizer
    private float constantWidth = 1;//16;
    private float constantHeight = 1;//9;

    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 System.Windows.Forms.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

3
800x600 > 850x650不是相同的宽高比。我猜你的意思是你想让它们都以相同的像素值改变? - musefan
4个回答

4
长宽比是一个比例,也就是说,通过 height / width 运算所得到的结果应该是相同的值,才能保持长宽比不变。简单地等量改变长或宽并不能保持长宽比,除非长和宽的值相同(如正方形)。
因此,您需要先获取长宽比(即 height / width),然后将该值乘以高度(或宽度)来获取宽度(或高度)。请注意,保留原长宽比对于it技术相关内容至关重要。
const double Ratio = height / width;

...

height = height + 50;  // This works the same if you change the 'width'
width = height * Ratio;

1
对于850来说,它是637.5(四舍五入为638)。因为600是800的75%。 所以 height = width * 0.75 这将为您保持纵横比。

1
如果Windows分辨率为800 x 600,而您想将其宽度增加到850,则需要进行以下计算。
Int32 newWidth = 850;
Int32 newHeight = (this.Height * newWidth) / this.Width;
this.Size = new Size(newWidth, newHeight);

//Basic Calculation
//newHeight = (oldHeight x newWidth) / oldWidth
//638 = (600 * 850) / 800

1
或者更有用的通用版本:Int32 newHeight = (oldHeight * newWidth) / oldWidth; - DonBoitnott

0

您可以尝试以下方法:

public void ResizeWidth(int newWidth)
{
   this.Height = (int)((double)newWidth / (double)((double)this.Width / (double)this.Height));
   this.Width = newWidth;
}

public void ResizeHeight(int newHeight)
{
   this.Width = (int)((double)newHeight * (double)((double)this.Width / (double)this.Height));
   this.Height = newHeight;
}

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