Resizing a Form with Ratio

5 posts, 0 answers
  1. Scott Gross
    Scott Gross  avatar
    15 posts
    Member since:
    Feb 2009

    Posted 01 May 2007 Link to this post

    What is the proper way to proportionally resize a form?

    Currently I just have:
    private void Form1_Resize(object sender, EventArgs e)  
    {  
        this.Height = this.Width;  

    This correctly resizes the height when it's horizontally resized, but it flickers between the initial size and the "dragging" size.

    Any idea on how to fix this?

    I need this to work vertically as well.  Ideally I'd like to have the ratio calculated from the initial size.

    Hope someone can help.

    Thank you
  2. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 02 May 2007 Link to this post

    Hello,

    You can try the following solution for resizing a form proportionally.
    Place the following code in your form:
    protected override void WndProc(ref Message m) 
        const int WM_EXITSIZEMOVE = 0x232; 
        const int WM_SYSCOMMAND = 0x112; 
     
        switch (m.Msg) 
        { 
            case WM_EXITSIZEMOVE: // Form is moved or resized 
     
                base.WndProc(ref m); 
                FormResized(); 
                break
     
            case WM_SYSCOMMAND: // System command (maximize, minimize, restore, etc.) 
     
                base.WndProc(ref m); 
     
                const int SC_MAXIMIZE = 0xF030; 
                const int SC_MINIMIZE = 0xF020; 
                const int SC_RESTORE = 0xF120; 
     
                switch (m.WParam.ToInt32()) 
                { 
                    case SC_MAXIMIZE: // Form is maximized by control 
                    case SC_MAXIMIZE + 2: // Form is maximized by title bar 
                        FormResized(); 
                        break
     
                    case SC_MINIMIZE: // Form is minimized by control 
                        FormResized(); 
                        break
     
                    case SC_RESTORE: // Form is restored by control 
                    case SC_RESTORE + 2: // Form is restored by title bar 
                        FormResized(); 
                        break
     
                    default
                        break
                } 
                break
     
            default
     
                base.WndProc(ref m); 
                break
        } 
     
    private int lastWidth; 
    private int lastHeight; 
     
    void FormResized() 
     
        if (this.Width != this.lastWidth) 
        { 
            this.Height = this.Width; 
        } 
        else 
            if (this.Height != this.lastHeight) 
            { 
                this.Width = this.Height; 
            } 
     
     
        this.lastWidth = this.Width; 
        this.lastHeight = this.Height; 
     
     

    The actual resizing is executed in the FormResize method.
    I hope this works for you.

    All the best,
    Ray
    the telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for WinForms is Visual Studio 2017 Ready
  4. MattLynch
    MattLynch avatar
    20 posts
    Member since:
    Jan 2007

    Posted 02 May 2007 Link to this post

    You can also try this:

    this.SuspendLayout();

    // perform any special resizing...

    this.ResumeLayout();

  5. Scott Gross
    Scott Gross  avatar
    15 posts
    Member since:
    Feb 2009

    Posted 02 May 2007 Link to this post

    this is close. I need it to resize dynamically while I'm dragging though.  Is there a way to make it dynamic?
  6. MattLynch
    MattLynch avatar
    20 posts
    Member since:
    Jan 2007

    Posted 02 May 2007 Link to this post

    Your requirements are specialized, so your code needs to be specialized too. 

    Try this: Take Telerik's sample, and modify the FormResized method.

    void FormResized() 
         this.SuspendLayout();
        if (this.Width != this.lastWidth) 
        { 
            this.Height = this.Width; 
        } 
        else 
            if (this.Height != this.lastHeight) 
            { 
                this.Width = this.Height; 
            } 
     
     
        this.lastWidth = this.Width; 
        this.lastHeight = this.Height; 
         this.ResumeLayout();

    Have you thought about what happens on a wide screen monitor?  In a maximized state, your controls will not be proportional, because the screen size is not a square.  To maintain the correct proportions, you would need to disable maximizing (2 cases - via the title bar + via the control box).

    Have fun...
Back to Top
UI for WinForms is Visual Studio 2017 Ready