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

问题描述 投票:0回答:1

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

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

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

winforms powershell textbox tooltip mousehover
1个回答
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,则将不显示气球样式的工具提示

© www.soinside.com 2019 - 2024. All rights reserved.