PowerShell和Windows窗体工具提示自定义颜色

问题描述

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

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

[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()

解决方法

我会回答我自己的问题:

疲惫的头脑中有两个明显的错误。首先:

$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()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...