Click here to Skip to main content
Licence CPOL
First Posted 8 Sep 2009
Views 16,857
Downloads 1,036
Bookmarked 21 times

Relative resizing and control positioning in forms

By | 8 Sep 2009 | Article
Dynamicly resize and position controls relative to the size of the form.

Introduction

I looked for a code that resizes and positions all controls of a form relative to the form's real size. I found no code for the same on the internet, and decided to share my code with you. This code uses the resize_begin and resize_end values of the form to get the width and height of the form, and after the form resize, it resizes all controls and their child controls in a recursive fashion. Also, I added some code to avoid flickering and to refresh the form (will take some time).

Using the code

First, we will look at the variables to add to the form for the x, y position and the width, height of the form before it is changed. Use DllImport of SendMessage to stop the window message REDRAW.

private int xBefore = 0;
private int yBefore = 0;
private int widthBefore = 0;
private int heightBefore = 0;
private const int WM_SETREDRAW = 0xB;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, 
                         int wParam, int lParam);

In the ResizeBegin event that will occur when the form changes its size, add the following code:

private void frmDynamicResize_ResizeBegin(object sender, EventArgs e)
{
    xBefore = this.Left;
    yBefore = this.Top;
    heightBefore = this.Height;
    widthBefore = this.Width;
}

Here is the ResizeEnd event that will occur when the form ends the change in size:

private void frmDynamicResize_ResizeEnd(object sender, EventArgs e)
{
       float WidthPerscpective = (float)Width / widthBefore;
       float HeightPerscpective = (float)Height / heightBefore;
       ResizeAllControls(this, WidthPerscpective, HeightPerscpective);
}

A recursive function is used to resize each control and its child controls. It uses SuspendLayout / ResumeLayout to stop/start the form drawing.

We use the SendMessage WinAPI to stop Windows from sending redraw Windows messages to the form to protect the form from flickering. It updates each control separately.

private void ResizeAllControls(Control recussiveControl, 
             float WidthPerscpective, float HeightPerscpective)
{            
    SuspendLayout();
    foreach (Control control in recussiveControl.Controls)
    {
        if (control.Controls.Count != 0)
        {
            SendMessage(this.Handle, WM_SETREDRAW, 0, 0);
            ResizeAllControls(control, WidthPerscpective, HeightPerscpective);
            SendMessage(this.Handle, WM_SETREDRAW, 1, 0);
            Invalidate(control.Region);
            control.Update();
        }
        control.Left = (int)(control.Left * WidthPerscpective);
        control.Top = (int)(control.Top * HeightPerscpective);
        control.Width = (int)(control.Width * WidthPerscpective);
        control.Height = (int)(control.Height * HeightPerscpective);
        Invalidate(control.Region);
        control.Update();
    }
    ResumeLayout();
}

Points of Interest

I didn't put any code for maximizing or normalizing the form. That is pretty simple. If there is request for it, I will add it to the source.

History

  • 06/09/09: Uploaded to CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Liav Hurvitz


www.novabind.com
Israel Israel

Member



Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberSardkajai20:19 28 Apr '11  
GeneralResizing and Maximize or normalize the form Pinmemberzaki_82790252:25 22 Feb '11  
GeneralMy vote of 3 PinmemberToli Cuturicu11:58 24 Aug '10  
QuestionWhy you dont use a simple TableLayoutPanel? Pinmemberscosta_FST1:01 17 Sep '09  
AnswerRe: Why you dont use a simple TableLayoutPanel? PinmemberLiav Hurvitz6:20 21 Sep '09  
GeneralRe: Why you dont use a simple TableLayoutPanel? Pinmemberscosta_FST20:10 21 Sep '09  
GeneralIsn't this already handled PinmemberRoger Willcocks10:44 14 Sep '09  
GeneralRe: Isn't this already handled PinmemberLiav Hurvitz23:07 14 Sep '09  
GeneralRe: Isn't this already handled PinmemberRoger Willcocks11:44 15 Sep '09  
GeneralUse cases Pinmembersotona21:33 8 Sep '09  
GeneralRe: Use cases PinmemberLiav Hurvitz9:13 11 Sep '09  
GeneralRe: Use cases Pinmemberdxlee3:35 15 Sep '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 8 Sep 2009
Article Copyright 2009 by Liav Hurvitz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid