gpt4 book ai didi

winforms - 显示窗体和控件的缩放比例和大小

转载 作者:行者123 更新时间:2023-12-05 01:19:00 24 4
gpt4 key购买 nike

在我的桌面上,我的默认显示比例因子为 100%。当我创建一个表单大小为 400x200 且具有 3 个大小为 75x23 的图片框的简单应用程序时,它会按预期显示(图 1)。

Image 1

当我在显示比例为 125% 的笔记本电脑上运行该程序时,它没有按预期显示(图 2)。

Image 2

表格不合比例。现在是 530x244,宽度是 1.33x400,高度是 1.22x200。

红色的图片框也是不合比例的,是100x28。横向尺寸为1.33x75,纵向尺寸为1.25x23。

绿色和蓝色图片框的大小在代码中设置。

public Form1()
{
InitializeComponent();

double factor = CreateGraphics().DpiX / 96f;
pictureBox2.Size = new Size((int)(75 * factor), (int)(23 * factor));

pictureBox3.Size = new Size(75, 23);


label1.Text = String.Format("Form:{0}-{1} DpiX:{2} DpiY:{3}",
this.Size.Width,
this.Size.Height,
CreateGraphics().DpiX,
CreateGraphics().DpiY
);

label2.Text = String.Format("{0}-{1}", pictureBox1.Size.Width, pictureBox1.Size.Height);
label3.Text = String.Format("{0}-{1}", pictureBox2.Size.Width, pictureBox2.Size.Height);
label4.Text = String.Format("{0}-{1}", pictureBox3.Size.Width, pictureBox3.Size.Height);
}

窗体和图片框的初始大小在 InitializeComponent 函数中。

          private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
this.pictureBox1.Location = new System.Drawing.Point(14, 32);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(75, 23);
this.pictureBox1.TabIndex = 8;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.pictureBox2.Location = new System.Drawing.Point(14, 61);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(75, 23);
this.pictureBox2.TabIndex = 11;
this.pictureBox2.TabStop = false;
//
// pictureBox3
//
this.pictureBox3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
this.pictureBox3.Location = new System.Drawing.Point(14, 90);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(75, 23);
this.pictureBox3.TabIndex = 15;
this.pictureBox3.TabStop = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(96, 41);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 16;
this.label2.Text = "label2";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(96, 71);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(35, 13);
this.label3.TabIndex = 17;
this.label3.Text = "label3";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(96, 100);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(35, 13);
this.label4.TabIndex = 18;
this.label4.Text = "label4";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(384, 162);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;

}

当缩放设置为 100% 时,DpiX 将为 96f。当设置为 125% 时,DpiX 将为 120f。绿色图片框使用因子进行缩放,大小为 93x28,符合预期(75*120/96=93 和 23*120/96=28)。作为引用,蓝色图片框显示了 75x23 的原始尺寸。

谁能帮我解释一下为什么表格和红色图片框不符合设计比例?因素1.33和1.22如何解释?

最佳答案

 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

这是一个明智的选择。请注意 AutoScaleMode 属性的替代选择,您也可以选择 AutoScaleMode.Dpi。这是您希望会发生的事情。

但它没有,您为 Form 和 pictureBox1 获得的自动缩放是基于 AutoScaleMode.Font 的。很难看出,但如果仔细观察,您会注意到,您在第二张屏幕截图中没有得到相同的字体。默认设置是“Microsoft Sans Serif”,它在较新的 Windows 版本中已停用,您会得到一个替代品。应该是塞格。 Windows 中的部分机制使旧的 UI 设计在较新的操作系统版本上看起来焕然一新。请注意它的平均字符宽度要高得多。它弥补了这一点,这就是为什么你得到 1.33 而不是 1.25

你可以选择 AutoScaleMode.Dpi 然后你会得到你喜欢的数字。但冒着文本不再适合控件的风险。考虑使用在两台机器上都可用的字体(如 Tahoma)使其更易于预测。

您得到 1.22 是因为表单自动缩放其 ClientSize 属性,而不是 Size。边框大小相同,因此整体大小增加小于 1.25。如果用户修改他的主题以显示更大的标题文本和按钮,则可能会有更大的不同。只有 ClientSize 很重要,这是确保窗口内容适合的重要属性。

请注意您对 CreateGraphics() 的使用,它有太多的副作用。此方法只能通过首先创建 native 窗口来工作。这会导致自动缩放生效,比正常情况早得多。因此,您正在修改已经重新缩放的尺寸,这大大增加了困惑。此处更安全的版本是 Graphics.FromHwnd(IntPtr.Zero),现在您可以使用原始设计指标修改大小,至少在 Load 事件触发之前是这样。

关于winforms - 显示窗体和控件的缩放比例和大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42481619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com