Scale windows forms window

前端 未结 3 1092
说谎
说谎 2021-01-13 22:32

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 p

3条回答
  •  借酒劲吻你
    2021-01-13 22:49

    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.

提交回复
热议问题