Scale windows forms window

那年仲夏 提交于 2019-12-01 09:08:02

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(System.Object sender, System.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 cnt, Size newSize)
{
    int iWidth = newSize.Width - oldSize.Width;
    cnt.Left += (cnt.Left * iWidth) / oldSize.Width;
    cnt.Width += (cnt.Width * iWidth) / oldSize.Width;

    int iHeight = newSize.Height - oldSize.Height;
    cnt.Top += (cnt.Top * iHeight) / oldSize.Height;
    cnt.Height += (cnt.Height * iHeight) / 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 / formBaseWidth);
this.Scale(new SizeF(scaleX, scaleY);

put this in your resize event.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!