如何在.NET WinForms应用程序中控制字体DPI

我为一家小型企业创建了一个应用程序。办公室的某些员工无法正确看到该表格。原因是它们的DPI设置设置为96dpi以上。有人知道控制此的方法吗?

对于所有具有winforms应用程序经验的人,如何控制表单布局,以使DPI不会影响应用程序的外观?


慕丝7291255
浏览 605回答 3
3回答

慕桂英3389331

假定您不尝试遵循用户的UI字体选择(SystemFonts.IconTitleFont),并且仅将表单硬编码为一种字体大小(例如Tahoma 8pt,Microsoft Sans Serif 8.25pt),则可以将表单设置AutoScaleMode为ScaleMode.Dpi。这将通过调用来按比例缩放表单及其大部分子控件的大小,从而依次对自身和所有子控件递归调用protected 方法。将根据新缩放因子的需要增加控件的位置,大小,字体等。CurrentDpiSetting / 96Form.Scale()ScaleControl()ScaleControl警告:并非所有控件都能正确缩放自己。例如,列表视图的列不会随着字体变大而变宽。为了处理该问题,您必须根据需要手动执行其他缩放。我通过重写受保护的ScaleControl()方法并手动缩放listview列来做到这一点:public class MyForm : Form{&nbsp; &nbsp;protected override void ScaleControl(SizeF factor, BoundsSpecified specified)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; base.ScaleControl(factor, specified);&nbsp; &nbsp; &nbsp; Toolkit.ScaleListViewColumns(listView1, factor);&nbsp; &nbsp;}}public class Toolkit&nbsp;&nbsp;{&nbsp; &nbsp;/// <summary>&nbsp; &nbsp;/// Scale the columns of a listview by the Width scale factor specified in factor&nbsp; &nbsp;/// </summary>&nbsp; &nbsp;/// <param name="listview"></param>&nbsp; &nbsp;/// <param name="factor"></param>&nbsp; &nbsp;/// <example>/*&nbsp; &nbsp;/// protected override void ScaleControl(SizeF factor, BoundsSpecified specified)&nbsp; &nbsp;/// {&nbsp; &nbsp;///&nbsp; &nbsp; base.ScaleControl(factor, specified);&nbsp; &nbsp;///&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;///&nbsp; &nbsp; //ListView columns are not automatically scaled with the ListView, so we&nbsp; &nbsp;///&nbsp; &nbsp; //must do it manually&nbsp; &nbsp;///&nbsp; &nbsp; Toolkit.ScaleListViewColumns(lvPermissions, factor);&nbsp; &nbsp;/// }&nbsp; &nbsp;///</example>&nbsp; &nbsp;public static void ScaleListViewColumns(ListView listview, SizeF factor)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; foreach (ColumnHeader column in listview.Columns)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; column.Width = (int)Math.Round(column.Width * factor.Width);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}如果您仅使用控件,那么这一切都很好。但是,如果您使用任何硬编码的像素大小,则需要通过表单的当前缩放比例来缩放像素的宽度和长度。可能具有硬编码像素大小的情况的一些示例:画一个25px高的矩形在表单上的位置(11,56)绘制图像将图标拉伸到48x48使用Microsoft Sans Serif 8.25pt绘制文本获取图标的32x32格式并将其填充到PictureBox中如果是这种情况,则需要通过“ 当前缩放系数 ” 缩放那些硬编码的值。不幸的是,没有提供“当前”比例因子,我们需要自己记录它。解决方案是假设初始比例因子为1.0,并且每次ScaleControl()调用该比例因子时,请通过新因子修改运行比例因子。public class MyForm : Form{&nbsp; &nbsp;private SizeF currentScaleFactor = new SizeF(1f, 1f);&nbsp; &nbsp;protected override void ScaleControl(SizeF factor, BoundsSpecified specified)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; base.ScaleControl(factor, specified);&nbsp; &nbsp; &nbsp; //Record the running scale factor used&nbsp; &nbsp; &nbsp; this.currentScaleFactor = new SizeF(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.currentScaleFactor.Width * factor.Width,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.currentScaleFactor.Height * factor.Height);&nbsp; &nbsp; &nbsp; Toolkit.ScaleListViewColumns(listView1, factor);&nbsp; &nbsp;}}最初,比例因子为1.0。如果form然后被缩放1.25,则缩放因子变为:1.00 * 1.25 = 1.25&nbsp; &nbsp; //scaling current factor by 125%如果该表单随后被缩放0.95,则新的缩放因子变为1.25 * 0.95 = 1.1875&nbsp; //scaling current factor by 95%使用a的原因SizeF(而不是单个浮点值)是因为缩放量在x和y方向上可以不同。如果将表单设置为ScaleMode.Font,则该表单将缩放为新的字体大小。字体可以具有不同的宽高比(例如, Segoe UI的字体比Tahoma的字体高)。这意味着您必须分别缩放x和y值。因此,如果您想将控件放置在location (11,56),则必须将定位代码从以下位置更改:Point pt = new Point(11, 56);control1.Location = pt;至Point pt = new Point(&nbsp; &nbsp; &nbsp; (int)Math.Round(11.0*this.scaleFactor.Width),&nbsp; &nbsp; &nbsp; (int)Math.Round(56.0*this.scaleFactor.Height));control1.Location = pt;如果要选择字体大小,则同样适用:Font f = new Font("Segoe UI", 8, GraphicsUnit.Point);必须成为:Font f = new Font("Segoe UI", 8.0*this.scaleFactor.Width, GraphicsUnit.Point);而将32x32图标提取到位图的方式将从以下内容更改:Image i = new Icon(someIcon, new Size(32, 32)).ToBitmap();至Image i = new Icon(someIcon, new Size(&nbsp; &nbsp; &nbsp;(int)Math.Round(32.0*this.scaleFactor.Width),&nbsp;&nbsp; &nbsp; &nbsp;(int)Math.Round(32.0*this.scaleFactor.Height))).ToBitmap();

有只小跳蛙

我知道有些麻烦,但是请考虑使用WPF重写您的应用程序。WPF应用程序在每个DPI设置上的外观都相同。
打开App,查看更多内容
随时随地看视频慕课网APP