如何在 Windows Forms PowerShell 中显示鼠标悬停文本框工具提示?

How to display a mouseover textbox tooltip in Windows Forms PowerShell?

谁能告诉我如何将鼠标悬停工具提示添加到 Powershell windows 表单上的文本框的示例?感谢您的帮助!

很抱歉没有在这里直接发布我的代码,但我每次提交前都会收到错误消息。

代码可以在这里找到 https://drive.google.com/file/d/1u7r_vaMh8sEsAWsXLcFtxfAXtYTcURj2/view?usp=sharing

扩展你的 关于同一个项目,我建议添加另一个辅助函数来处理两个文本框控件:

function Show-ToolTip {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [System.Windows.Forms.Control]$control,
        [string]$text = $null,
        [int]$duration = 1000
    )
    if ([string]::IsNullOrWhiteSpace($text)) { $text = $control.Tag }
    $pos = [System.Drawing.Point]::new($control.Right, $control.Top)
    $obj_tt.Show($text,$form, $pos, $duration)
}

我还建议在每个文本框的 Tag 属性 中存储工具提示的默认文本。您始终可以在 MouseEnter 事件中动态更改它:

$txt_one.Tag = "Testing my new tooltip on first textbox"
$txt_two.Tag = "Testing my new tooltip on second textbox"

接下来,为这些框添加事件处理程序:

# event handlers for the text boxes
$txt_one.Add_GotFocus({ Paint-FocusBorder $this })
$txt_one.Add_LostFocus({ Paint-FocusBorder $this })
$txt_one.Add_MouseEnter({ Show-ToolTip $this })   # you can play with the other parameters -text and -duration
$txt_one.Add_MouseLeave({ $obj_tt.Hide($form) })

$txt_two.Add_GotFocus({ Paint-FocusBorder $this })
$txt_two.Add_LostFocus({ Paint-FocusBorder $this })
$txt_two.Add_MouseEnter({ Show-ToolTip $this })
$txt_two.Add_MouseLeave({ $obj_tt.Hide($form) })

[void]$form.ShowDialog() 下方也处理工具提示对象:

# clean-up
$obj_tt.Dispose()
$form.Dispose()

P.S。在测试期间,我发现在显示工具提示时使用 MouseHover 事件会发生奇怪的事情。时不时地,箭头指向上方,远离文本框。更改为 MouseEnter 伴随着 MouseLeave 最适合我。


根据您的评论,我无法在 PS 7 中进行测试,但对我来说,在 PS 5.1 中它有效:


感谢 Paul Wasserman,他发现
下应该有注册表设置 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\
EnableBalloonTips.
的名字命名 它是一个 DWORD 值,需要设置为 1。如果您的计算机缺少该注册表值,或者它被设置为 0 将不会显示气球式工具提示