- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Windows 窗体(包括我正在使用的用于 Compact Framwork 的 Windows 窗体)有一个 AutoScale feature .通过设置 AutoScaleMode
property到 AutoScaleMode.Dpi
,例如,为 320x200 设计的应用程序会自动缩放到更大的显示器,例如 VGA 设备。
这很好用,但我有一些自制的自定义控件可以执行它们自己的 OnPaint
操作,我希望它们也可以扩展。不幸的是,我没有找到关于如何执行此操作的良好文档或示例。
目前,我正在这样做:
protected SizeF zoom = new SizeF(1.0, 1.0);
protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
{
base.ScaleControl(factor, specified);
zoom = factor; // remember the zoom factor
}
protected override void OnPaint(PaintEventArgs e)
{
// scale everything by zoom.Width and zoom.Height
...
e.Graphics.DrawImage(...);
...
}
它有效,但我想知道这是否是“正确的方法”。由于(根据 ILSpy)其他 CF 控件都没有内部字段来存储比例因子,我想知道是否有更简单或更好的方法来执行此操作。
最佳答案
我们通常在控件和表单的 OnResize
中处理所有缩放。我们还必须支持许多具有疯狂尺寸和 DPI 的不同设备(一些平台甚至不报告正确的 DPI!)。我们发现在 AutoScaleMode
关闭的情况下,您可以按比例使用这样的帮助程序在 OnResize
中缩放窗体的子项。您只需在构造函数中添加一个 Size _initalSize
成员设置为表单大小。然而,我通常发现在大多数表单上我必须编写自定义布局代码以适当处理纵向和横向显示。
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
// Scale the control
ScaleChildren(this, ref _initialFormSize);
}
public static void ScaleChildren(Control control, ref Size initialSize, float fontFactor)
{
if (control == null || control.Size == initialSize)
return;
SizeF scaleFactor = new SizeF((float)control.Width / (float)initialSize.Width, (float)control.Height / (float)initialSize.Height);
initialSize = control.Size;
if (!float.IsInfinity(scaleFactor.Width) || !float.IsInfinity(scaleFactor.Height))
{
foreach (Control child in control.Controls)
{
child.Scale(scaleFactor);
if (child is Panel)
continue;
try
{
// scale the font
float scaledFontSize = (float)(int)(child.Font.Size * scaleFactor.Height * fontFactor + 0.5f);
System.Diagnostics.Debug.WriteLine(
string.Format("ScaleChildren(): scaleFactor = ({0}, {1}); fontFactor = {2}; scaledFontSize = {3}; \"{4}\"",
scaleFactor.Width, scaleFactor.Height, fontFactor, scaledFontSize, child.Text));
child.Font = new Font(child.Font.Name, scaledFontSize, child.Font.Style);
}
catch { }
}
}
}
关于c# - 如何使紧凑的框架自定义控件支持 AutoScale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153853/
问题:用任何语言编写一个程序,给定一个字符串,生成一个与输入字符串的任何字谜匹配的正则表达式。对于所有大于某个长度 N 的正则表达式,正则表达式必须短于列出所有可能的字谜的“蛮力”解决方案,其中列出了
我正在尝试使用 html 和 css 生成一个简单的甘特图,我需要弄清楚如何使它紧凑,这意味着如果有可用的空白,该条应尝试适合前一行,同时保持相同的 x 距离。 上下文: X 轴是以月为单位的时间轴
我收到以下错误 (1/1) ErrorException compact(): Undefined variable: operator 这是我的代码行 $postsCat = Post::w
我正在使用 java 读取紧凑/截断的 PDF417 条形码,并且我有标准 PDF417 条形码的开始和结束模式的矩阵: Start Pattern = 81111113 (ending wit
我正在使用的当前 ListView 在文本上方和下方有额外的空间。我如何使它成为一个真正紧凑的? http://jsfiddle.net/qW6sP/8/
是否有办法“压缩”SQL 查询的结果? 结果如下: +----+-----------+----------+-----------+-------------+--------+ | ID | Fi
我目前在我的 Java 项目中使用 JSON(通过 gzip 压缩),其中我需要在磁盘上存储大量对象(数亿)。我每行有一个 JSON 对象,并且不允许在 JSON 对象中换行。这样我就可以逐行从磁盘流
R具有qr()函数,该函数使用LINPACK或LAPACK执行QR分解(以我的经验,后者快5%)。返回的主要对象是一个矩阵“ qr”,该矩阵包含在上三角矩阵R中(即R=qr[upper.tri(qr)
我希望开始 SQL 开发,因此决定用 Java 和 C# 实现一个使用基于 SQL 的数据库的应用程序,特别是它将是一个桌面应用程序,用于存储某些类型的数据通过数据库访问用户机器。 阅读此处发布的答案
我目前正在尝试解决一个奇怪的问题。我找不到太多相关信息。 简而言之: 当反序列化一个 xml 序列化对象时,我得到了一个正确对象类型的实例,但我只得到了对象内部的默认值。 长话短说: 序列化和反序列化
假设我需要一个包含许多重复元素的数组,如下所示: [3,3,3,3,3,8,8,8,8,5,5,5,5,5,5](所以这是五个 3,四个 8,和六个 5) 在 python 中,你可以像这样非常优雅地
这个问题在这里已经有了答案: Reverse the ordering of words in a string (48 个回答) 关闭 7 年前。 所以我正在尝试针对提示提出一个好的 C++ 解决
我是一名优秀的程序员,十分优秀!