簡體   English   中英

調整Window Forms應用程序大小時如何保持長寬比?

[英]How to maintain the aspect ratio when resizing a Window Forms application?

我需要一些有關調整窗口表單大小的幫助,默認情況下,此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

寬高比是一個比率,也就是說, height / width的結果應與要保持的寬高比相同。 除非高度和寬度為相同的值(即正方形),否則僅改變相同的數量就不會保持外觀。

因此,您需要通過首先獲取長寬比(即height / width ),然后將該值乘以高度以獲取寬度(反之亦然)來更改高度(或寬度)。

const double Ratio = height / width;

...

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

如果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

對於850,它是637.5(四舍五入為638)。 因為600是800的75%。所以height = width * 0.75

那將為您保留長寬比。

您可以嘗試以下方法:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM