【问题标题】:The PowerShell Form button text is not correctly aligned and blurryPowerShell 表单按钮文本未正确对齐且模糊
【发布时间】:2020-01-15 12:31:10
【问题描述】:

字体不同于 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


    【解决方案1】:

    发现禁用默认文本渲染:

    [Windows.Forms.Application]::SetCompatibleTextRenderingDefault($false);
    

    【讨论】:

      【解决方案2】:

      看起来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]:: 会为您提供智能感知。

      您还可以找到不同的枚举值here

      【讨论】:

      • 但与 MessageBox 文本相比仍然模糊。
      • @Marian 文本模糊不是 PowerShell 造成的,而是 Windows 10 中的常见问题。尝试在谷歌上搜索对您的系统有帮助的解决方案。一个很好的起点是herethere
      【解决方案3】:

      我能够进行一些快速测试。看来Forms.ButtonWindows.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.MessageBoxForms.MessageBox 不同。我所能得出的结论是肯定有另一个 MS 开发人员在从事 .MessageBox 类的工作,然后是在 Forms.Buttonclass 工作。

      【讨论】:

        猜你喜欢
        • 2022-12-04
        • 1970-01-01
        • 2017-01-27
        • 2016-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        相关资源
        最近更新 更多