【发布时间】:2018-09-20 13:05:52
【问题描述】:
当我在视网膜 MacBook Pro 上的虚拟机中在 Windows 中运行我的 WinForm 应用程序时,窗体的大小在运行时缩小,而按钮同时向外移动。这可能会导致底部边缘的按钮滑到窗口边缘下方,看不见。由于它们是底部锚定的,因此它们完全无法访问。从 Windows 原生桌面运行时,应用程序通常表现良好。
这仅发生在表单上的 字体 或 DPI AutoScaleMode 设置中。使用 Inherit 或 None,表单及其内容非常庞大,但与我的设计方式成正比。
我已经用一个全新的模板 WinForm 应用程序复制了这个,除了调整表单大小并放入一个按钮之外什么都不做。如何在不改变尺寸的情况下让应用缩放?
这是designer.cs中的InitializeComponent()方法:
private void InitializeComponent()
{
this.sendButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// sendButton
//
this.sendButton.Location = new System.Drawing.Point(60, 856);
this.sendButton.Margin = new System.Windows.Forms.Padding(4);
this.sendButton.MinimumSize = new System.Drawing.Size(200, 60);
this.sendButton.Name = "sendButton";
this.sendButton.Size = new System.Drawing.Size(200, 62);
this.sendButton.TabIndex = 1004;
this.sendButton.Text = "Send";
this.sendButton.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 981);
this.Controls.Add(this.sendButton);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
这是设计器中的表单截图:
在运行时:
【问题讨论】:
-
使用锚点。或者一个 Dock-Filled TableLayoutPanel。
-
@LarsTech 我确实锚定到底部,但是当我这样做时,控件被卡在该位置超出范围
-
我没有看到您的设计器代码中使用了 Anchor 属性。如果您要在不同的 DPI 环境中进行设计,TableLayoutPanel 可以让事情变得更简单。
-
@LarsTech 我没有将它包含在我的示例中,因为它不会影响行为。布局并不是真正的问题——表单的行为不符合预期。我将尝试改写问题以使其更清楚。
-
嗯,确实如此。如果您在 Retina Mac 上的某个位置放置一个按钮,它可能是您戴尔上的不同位置。一个 Dock-Filled TableLayoutPanel 会有所帮助,因为它可以按百分比进行布局。您可以尝试使用表单的 AutoScaleMode。
标签: c# winforms visual-studio-2017