winforms - 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()
推荐阅读
- dataframe - 从 MongoEngine 结果创建 Pandas DataFrame 的正确方法是什么?
- c# - SOAP API System.Net.WebException 响应流在错误响应中给出 BOM 字符
- angular - 升级 Angular 版本 10 后,离子原生网络无法按预期工作
- angular - 使用 Angular observable 定期查询 web api 并订阅
- symfony - Swagger 返回需要身份验证
- telegram - 如何验证 webhook 的请求来自 Telegram?
- gradle - 如何从 settings.gradle 访问 build.gradle 中定义的 ext 变量?
- r - 带有ifelse的forloop,合并两个数据集
- python - 如何使用 Termux 解决 $pip install web3 错误?
- python - 如何在背景减法中计算前景区域的像素数