在 C# 中缩放窗口窗体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zoom for a windows form in C#
提问by WaterBoy
Is there an easy way to set the zoom level for a windows form in C#? In VBA there was a zoom property of the form.
有没有一种简单的方法可以在 C# 中为 Windows 窗体设置缩放级别?在 VBA 中,表单有一个缩放属性。
采纳答案by WaterBoy
There is no way (that I know of) to do what you ask with typical WinForms.
没有办法(据我所知)使用典型的 WinForms 来完成您的要求。
If you're doing custom painting/drawing, you can zoom that by using a zoom transform, but so far as I know there is no "Zoom" property for the form in the entire world of .NET and native Windows/C++ APIs combined.
如果您正在进行自定义绘画/绘图,则可以使用缩放变换对其进行缩放,但据我所知,在 .NET 和本机 Windows/C++ API 组合的整个世界中,表单没有“缩放”属性.
You could probably rig something yourself such that you scale controls by a constant factor. And you can probably find 3rd-party controls/surfaces which support this. And who knows what is possible with WPF. But in a typical WinForms world, no.
您可能可以自己装配一些东西,以便按恒定系数缩放控件。您可能会找到支持此功能的第 3 方控件/界面。谁知道 WPF 有什么可能。但在典型的 WinForms 世界中,没有。
回答by WaterBoy
I had the same problem and I solved it this way in c#. Code goes on Form load
我遇到了同样的问题,我在 c# 中以这种方式解决了它。代码继续表单加载
float scaleX = ((float)Screen.PrimaryScreen.WorkingArea.Width / 1024);
float scaleY = ((float)Screen.PrimaryScreen.WorkingArea.Height / 768);
SizeF aSf = new SizeF(scaleX, scaleY);
this.Scale(aSf);
This "more or less" scales form and all children. Loops forever in 800x600 (?) You have to set the following Form properties:
这种“或多或少”的尺度形式和所有的孩子。以 800x600 (?) 永远循环您必须设置以下表单属性:
AutoscaleMode = Font
AutoSize = False
回答by Yuriy Grinevich
You can get some kind of zoom by assigning different Fontto the Form, all the controls will be zoomed accordingly if AutoScaleModeset to Font. Also settings AutoSizeto False will keep form size intact, the controls will grow to the center of the form. You need to set up all Anchors correctly and test the look, since its just "kind of zoom".
您可以通过为 Form分配不同的Font来获得某种缩放,如果AutoScaleMode设置为 Font ,所有控件都将相应地缩放。此外,将AutoSize设置为 False 将保持表单大小不变,控件将增长到表单的中心。您需要正确设置所有锚点并测试外观,因为它只是“某种缩放”。
So basically here is sample constructor:
所以基本上这里是示例构造函数:
public Form1()
{
InitializeComponent();
AutoSize = false;
AutoScaleMode = AutoScaleMode.Font;
Font = new Font("Trebuchet MS",
10.0f,
FontStyle.Regular,
GraphicsUnit.Point,
((byte)(204))
);
}
After form has been shown assigning new Font will mess up all the controls and this trick will not work.
显示表单后,分配新字体会弄乱所有控件,此技巧将不起作用。