Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale windows forms window

It is possible to prepare the windows forms window to resize/reposition all elements depending on the window size, but I am trying to do something different.

Is it possible in some way to actually scale the window along with all elements inside regardless of their positions, properties, etc?

Basically the way you would scale a picture in some graphics editor - you can just stretch or shrink it, but it doesn't matter what is on that picture.

So, is it possible to do something similar with the form? Being able to scale its size regardless of what's inside the form.

like image 449
NewProger Avatar asked Apr 01 '14 08:04

NewProger


1 Answers

Windows form does not provide any feature to do this. But, you can write your own code and make your form resolution independent.

This is not a complete example to make windows form resolution independent but, you can get logic from here. The following code creates problem when you resize the window quickly.

CODE:

private Size oldSize;
private void Form1_Load(object sender, EventArgs e) => oldSize = base.Size;

protected override void OnResize(System.EventArgs e)
{
    base.OnResize(e);

    foreach (Control cnt in this.Controls) 
        ResizeAll(cnt, base.Size);

    oldSize = base.Size;
}
private void ResizeAll(Control control, Size newSize)
{
    int width      = newSize.Width - oldSize.Width;
    control.Left  += (control.Left  * width) / oldSize.Width;
    control.Width += (control.Width * width) / oldSize.Width;

    int height = newSize.Height - oldSize.Height;
    control.Top    += (control.Top    * height) / oldSize.Height;
    control.Height += (control.Height * height) / oldSize.Height;
}

Otherwise you can use any third party control like DevExpress Tool. There is LayoutControl which is providing same facility. you can show and hide any control at runtime without leaving blank space.

like image 105
Shell Avatar answered Sep 28 '22 01:09

Shell