Form re-sizing at runtime


Here I am trying to present you how to re-size the form at runtime with re-size of all controls. Once you place a control on the form, you can adjust its position, size, and other design-time properties. There are situations, when you have to allow a user of your application to re-size the form.Then controls never get bigger and smaller when you re-size the form. Here I am trying to understand how to do this!!

In C#.Net

For re-sizing the Form at runtime with all controls get increased or decreased according to user re-sizing the form.
For this, We have to declare these

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



Use the namespace

 
using System.Runtime.InteropServices;



Form have got two events ResizeBegin, ResizeEnd

My Form's name is frmReceivedFCRegister

Write these code in form_ ResizeBegin

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


Then Write these code in form_ ResizeEnd

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




For that We have to write a function

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




And set form Borderstyle is 'sizable'

Just re-size the form n enjoy !!


Comments

Author: Ashok Babu Kandula01 Dec 2011 Member Level: Gold   Points : 0

Nice Article Brother.......

Very useful

Author: nuwan rathnayake15 Dec 2011 Member Level: Silver   Points : 0

Nice article Dear, But I have to problem. I want to create if form maximizing do that thing. please give me a Idea.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: