【问题标题】:PowerShell and Windows Forms Tooltip custom colourPowerShell 和 Windows 窗体工具提示自定义颜色
【发布时间】:2020-10-09 15:17:32
【问题描述】:

我正在尝试自定义一个带有黑色背景(如果可能,还有黑色边框)和白色文本的简单工具提示。我有以下代码,但目前它很不稳定,有时有效,有时无效。

有人可以建议如何使这更可靠吗?谢谢。

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Windows.Forms.Application]::EnableVisualStyles()

Add-Type -AssemblyName System.Drawing

#create form
$form = New-Object System.Windows.Forms.Form

$shutdownBtn = New-Object System.Windows.Forms.Button
$shutdownBtn.Size = New-Object System.Drawing.Size(200, 40)
$shutdownBtn.Text = "Shut down"

$form.Controls.Add($shutdownBtn)

$tooltip2 = New-Object System.Windows.Forms.ToolTip
$tooltip2.SetToolTip($shutdownBtn, "Shut down.")

$tooltip2.OwnerDraw = $true 
$tooltip2.Add_Draw($tooltip2_Draw)
$tooltip2_Draw=[System.Windows.Forms.DrawToolTipEventHandler]{
        $fontstyle = New-Object System.Drawing.Font("Segoe UI", 9, [System.Drawing.FontStyle]::Normal)
        $format = [System.Drawing.StringFormat]::GenericTypographic
        $myBrush1 = new-object Drawing.SolidBrush White
        $_.Graphics.DrawString($_.ToolTipText, $fontstyle, $myBrush1, $_.Bounds.X, $_.Bounds.Y, $format)
        $myBrush2 = new-object Drawing.SolidBrush Black
        $_.Graphics.FillRectangle($myBrush2, $_.Bounds)
        $_.DrawBackground()
        $_.DrawBorder()
        $_.DrawText()
 }

$form_cleanup =
{
    $tooltip2.Remove_Draw($tooltip2_Draw)
    $form.remove_FormClosed($form_cleanup)
}

$form.add_FormClosed($form_cleanup)

[void]$form.ShowDialog()
$form.Dispose()

【问题讨论】:

    标签: winforms powershell powershell-4.0


    【解决方案1】:

    我会回答我自己的问题:

    疲倦的头脑中出现了几个明显的错误。首先:

    $tooltip2.Add_Draw($tooltip2_Draw) 
    

    应该在我声明后调用

    $tooltip2_Draw 
    

    其次,我需要打电话

    FillRectangle
    

    在我打电话之前

    DrawString
    

    最后,因为我设置 OwnerDraw = $true 我不需要调用:

    $_.DrawBackground()
    $_.DrawBorder()
    $_.DrawText()
    

    所以这里是解决方案:

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    Add-Type -AssemblyName System.Drawing
    
    #create form
    $form = New-Object System.Windows.Forms.Form
    
    $shutdownBtn = New-Object System.Windows.Forms.Button
    $shutdownBtn.Size = New-Object System.Drawing.Size(200, 40)
    $shutdownBtn.Text = "Shut down"
    
    $form.Controls.Add($shutdownBtn)
    
    $tooltip2 = New-Object System.Windows.Forms.ToolTip
    $tooltip2.SetToolTip($shutdownBtn, "Shut down now.")
    
    $tooltip2.OwnerDraw = $true 
    $tooltip2_Draw=[System.Windows.Forms.DrawToolTipEventHandler]{
            $fontstyle = new-object System.Drawing.Font('Microsoft Sans Serif', 16, [System.Drawing.FontStyle]::Regular)
            $format = [System.Drawing.StringFormat]::GenericTypographic
            $format.LineAlignment = [System.Drawing.StringAlignment]::Center;
            $format.Alignment = [System.Drawing.StringAlignment]::Center;
            $whiteBrush = new-object Drawing.SolidBrush White
            $blackBrush = new-object Drawing.SolidBrush Black
            $_.Graphics.FillRectangle($blackBrush, $_.Bounds)
            $_.Graphics.DrawString($_.ToolTipText, $fontstyle, $whiteBrush, ($_.Bounds.X + ($_.Bounds.Width/2)), ($_.Bounds.Y + ($_.Bounds.Height/2)), $format)
           
     }
    $tooltip2.Add_Draw($tooltip2_Draw)
    
    $form_cleanup =
    {
        $tooltip2.Remove_Draw($tooltip2_Draw)
        $form.remove_FormClosed($form_cleanup)
    }
    
    $form.add_FormClosed($form_cleanup)
    
    [void]$form.ShowDialog()
    $form.Dispose()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 2010-09-29
      相关资源
      最近更新 更多