简体   繁体   English

缩放窗口窗体窗口

[英]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.可以准备 Windows 窗体窗口以根据窗口大小调整/重新定位所有元素,但我正在尝试做一些不同的事情。

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. Windows 窗体不提供任何功能来执行此操作。 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.这不是使 Windows 窗体分辨率独立的完整示例,但是,您可以从此处获取逻辑。 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.否则,您可以使用任何第三方控件,如DevExpress Tool。 There is LayoutControl which is providing same facility.有提供相同功能的LayoutControl you can show and hide any control at runtime without leaving blank space.您可以在运行时显示和隐藏任何控件而不留空白。

Your form has a Scale property.您的表单具有 Scale 属性。 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.把它放在你的调整大小事件中。

查看自 .NET 2.0 起可用的Control.Scale方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM