PowerShell 表单按钮文本未正确对齐且模糊

问题描述 投票:0回答:4

字体与MessageBox和自定义表单按钮不同。

$size = New-Object System.Drawing.Size(350, 154);

$form = New-Object System.Windows.Forms.Form;
$form.BackColor = [System.Drawing.SystemColors]::Window;
$form.StartPosition = "CenterScreen";
$form.MinimumSize = $size;
$form.ShowIcon = $false;
$form.Topmost = $true;
$form.Text = $Title;
$form.Size = $size;

$ok = New-Object System.Windows.Forms.Button;
$ok.Size = New-Object System.Drawing.Size(75, 23);
$ok.UseVisualStyleBackColor = $true;
$ok.DialogResult = "OK";
$ok.Text = "OK";
$ok.Anchor = 8;

$form.Controls.Add($ok);

$form.ShowDialog();

字体未在中心垂直对齐,并且更加模糊。

winforms powershell button fonts
4个回答
0
投票

我能够进行一些快速测试。看起来

Forms.Button
Windows.MessageBox
- 或
Forms.MessageBox

之间确实存在外观差异

消息框:

致电:

[System.Windows.MessageBox]::Show("Test", $null ,'OkCancel')

结果:

表单按钮:

致电:

$form = New-Object System.Windows.Forms.Form
$testButton = New-Object System.Windows.Forms.Button
$testButton.Text = "OK"
$form.Controls.Add($testButton)
$form.ShowDialog() | Out-Null

结果:

我的第一次发现是,

System.Windows
命名空间和
Windows.Forms
命名空间之间可能存在差异。

然后我运行了另一个测试来调用

MessageBox
命名空间的
Windows.Forms
类。令人惊讶的是,这个测试显示了与
System.Windows.MessageBox

相同的字体和对齐方式

Windows.Forms.MessageBox:

致电:

[System.Windows.Forms.MessageBox]::Show("Test")

结果:

我的假设是

Windows.Forms.Button
中的默认文本/对齐方式与
Windows.MessageBox
Forms.MessageBox
不同。我所能得出的结论是,一定有不同的 MS 开发人员在
.MessageBox
类上工作,然后在
Forms.Button
类上工作。


0
投票

看起来

System.Windows.Forms.Button
的文本对齐方式默认为“MiddleCenter”,而 MessageBox 使用“BottomCenter”。

针对

$testButton.TextAlign
设置尝试不同的 TextAlign 可能性。

在我的 Windows 7 机器上添加以下代码

$form = New-Object System.Windows.Forms.Form
$testButton = New-Object System.Windows.Forms.Button
$testButton.Text = "OK"
$testButton.TextAlign = [System.Drawing.ContentAlignment]::BottomCenter
$form.Controls.Add($testButton)
$form.ShowDialog() | Out-Null

$form.Dispose()

产生这个

附注您可以通过输入枚举名称(如

$testButton.TextAlign = 'BottomCenter'
)来缩短行,但使用完整的
[System.Drawing.ContentAlignment]::
将为您提供智能感知。

您还可以在此处

找到不同的枚举值

0
投票

.TextAlign =“居中”


-1
投票

发现禁用默认文本渲染:

[Windows.Forms.Application]::SetCompatibleTextRenderingDefault($false);
© www.soinside.com 2019 - 2024. All rights reserved.