Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

Your form has a Scale property. You can directly set this property and it will simultaneously affect every control on the form.

float scaleX = ((float)formNewWidth / formBaseWidth);
float scaleY = ((float)formNewHeight / formBaseHeight);
this.Scale(new SizeF(scaleX, scaleY));

put this in your resize event.

Check out the Control.Scale method available since .NET 2.0.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top