提问人:Sajjad Abdullah 提问时间:3/1/2023 更新时间:3/1/2023 访问量:25
Windows 窗体:在“组框”中缩放子控件时出现的问题
Windows Forms: Issues in scaling child controls inside Group Box
问:
我在 Windows 窗体应用程序中缩放组框的子控件时遇到问题。我笔记本电脑上的分辨率是 1366x768,这意味着 1.78 的比率。而我客户的 PC 上的分辨率是 2560x1600(1.6 比例)。我的初始形式如图 1 所示。
所有控件(底部按钮除外)都包含在名为 的组框中。 包含一个名为 的 TableLayoutPanel 控件。我已将所有控件放置在控件单元内,以确保对齐和正确缩放。它在我的显示屏上显示良好。但是,客户端希望在不同分辨率的PC上使用此应用程序。为此,他希望他应该根据显示屏选择增加/减少所有控件的字体大小。我使用了以下代码捕获 CTRL+PLUS 和 CTRL+MINUS 键来调用该函数:-groupBox1
groupBox1
tableLayoutPanel1
tableLayoutPanel1
SizeAdjust
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.Oemplus) || keyData == (Keys.Control | Keys.Add))
{
SizeAdjust(Zoom.In);
return true;
}
else if (keyData == (Keys.Control | Keys.OemMinus) || keyData == (Keys.Control | Keys.Subtract))
{
SizeAdjust(Zoom.Out);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
而功能如下:-SizeAdjust
private void SizeAdjust(Zoom zoomType)
{
//aspect ratio percentage
float arp = (Screen.PrimaryScreen.Bounds.Width / Screen.PrimaryScreen.Bounds.Height) / 100.0f;
//float arp = (groupBox1.Width / groupBox1.Height) / 100.0f;
float nw = 0, nh = 0; //new width & new height
if (zoomType == Zoom.In)
{
nw = groupBox1.Width + (groupBox1.Width * arp);
nh = groupBox1.Height + (groupBox1.Height * arp);
}
else if (zoomType == Zoom.Out)
{
nw = groupBox1.Width - (groupBox1.Width * arp);
nh = groupBox1.Height - (groupBox1.Height * arp);
}
float w = nw / groupBox1.Width;
float h = nh / groupBox1.Height;
SizeF scale = new SizeF(w, h);
groupBox1.Scale(scale);
ScaleFont(this, w, h);
}
private void ScaleFont(Control control, float w, float h)
{
foreach (Control cntrl in control.Controls)
{
if (cntrl.Controls.Count > 0)
ScaleFont(cntrl, w, h);
else
{
if (cntrl.GetType() == typeof(TextBoxV2) || cntrl.GetType() == typeof(NumberBox)
|| cntrl.GetType() == typeof(DateTimePickerV2))
{
//cntrl.Font = new Font("Microsoft Sans Serif", cntrl.Font.SizeInPoints * h * w);
cntrl.Font = new Font(cntrl.Font.FontFamily, cntrl.Font.SizeInPoints * h * w);
}
else if (cntrl.GetType() == typeof(Label))
ScaleLabel((Label)cntrl, w, h);
}
}
}
private void ScaleLabel(Label lbl, float w, float h)
{
Font newFont = new Font("Microsoft Sans Serif", lbl.Font.SizeInPoints * h * w);
if (TextRenderer.MeasureText(lbl.Text, newFont).Width < lbl.Width)
lbl.Font = newFont;
}
现在,当我运行应用程序并尝试增加子控件的字体大小(缩放)时。它确实执行缩放,但没有进行微调,如图 2 所示:-groupBox1
然而,如果不自己进行缩放,而只是转到Windows,从选项中选择,那么它可以进行很好的缩放,如下图所示:-Display Settings
125%
Scale and Layout
我的问题是,我可以像 Windows 一样进行精细缩放吗,或者,我可以利用 Windows 缩放功能来缩放我的控件吗?groupBox1
我花了很多时间搜索已经发布的问题,但无法从中获得最好的结果。我也不想使用WPF。
答: 暂无答案
评论