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

时间:2020-04-28 09:51:22

标签: winforms powershell textbox tooltip mousehover

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

很抱歉无法直接在此处发布我的代码,但是每次提交前我都会不断收到错误消息。

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

1 个答案:

答案 0 :(得分:1)

在您的previous question上扩展有关同一项目的内容,我建议添加另一个帮助器功能来处理两个文本框控件:

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中可以运行:

enter image description here


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

相关问题