首页 > 解决方案 > 如何在 Windows 窗体 PowerShell 中显示鼠标悬停文本框工具提示?

问题描述

有人可以向我展示如何将鼠标悬停工具提示添加到 Powershell Windows 窗体上的文本框的示例吗?谢谢你的帮助!

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

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

标签: winformspowershelltextboxtooltipmousehover

解决方案


扩展您之前关于同一个项目的问题,我建议添加另一个帮助函数来处理两个文本框控件:

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

PS在测试过程中,我发现MouseHover在显示工具提示时使用事件会做一些奇怪的事情。箭头不时指向上方,远离文本框。更改为MouseEnter陪伴MouseLeave对我来说效果最好。


根据您的评论,我无法在 PS 7 中进行测试,但对我而言,在 PS 5.1 中它可以工作:

在此处输入图像描述


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


推荐阅读