winforms - 如何在 Windows 窗体 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()
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
,则不会显示气球式工具提示。
推荐阅读
- php - LARAVEL - 我想从包含主类别 ID 的上部 _id 列的类别对象访问主类别
- ruby - 将数组字符串对象转换为哈希?
- grep - 如何使用 zgrep 在归档文件中查找与给定文件名模式匹配的内容?
- angular - 使用 Angular AOT 构建失败
- laravel - 如何在 Laravel Eloquent 中的 take() 之后 orderBy()
- python - 单击提交时,UpdateView 和 DeleteView 导致错误 405
- ios - SwiftUI / iOS | 在没有 NSNotification 的情况下获取键盘大小
- svg - 在 SVG rect 上渲染斜面边框
- android - 为 CameraX 设置目标分辨率不像文档中那样工作
- scala - 为列表的每个元素添加引号