[AHK2] 常用的Tooltip
开始
在实际使用ahk时,常常要使用tooltip提示程序的运行结果。
比如:
- 增加屏幕亮度后显示当前亮度;
- 锁定鼠标后提示鼠标已锁定;
- 提示Run的运行结果;
- ……
在最后需要使用SetTimer
指定几秒种后使用Tooltip ,,, [weight]
关闭tooltip。
这部分工作包括显示tooltip显然是可以封装的,以遍后续的维护和修改。
脚本
于是便有下面的代码,封装了实际使用tooltip的常用方法,包括:
- ShowTip -> 显示指定文本并在指定时间后移除。
- SetTimerRemoveToolTip -> 指定时间后移除指定tooltip。
- RemoveToolTip -> 移除指定tooltip,而不使用SetTimer.
SetTimerRemoveToolTip()
使用防抖避免连续触发。
使用
; 显示tooltip并在4秒后移除,
Tip.ShowTip('SomeText', 200, 100, 2, 4000)
; 5秒后移除weight为5的tooltip
Tip.SetTimerRemoveToolTip(5, 5000)
; 立即移除weight为1的tooltip
Tip.RemoveToolTip(1)
代码
class Tip {
; class-desc: tooltip manager
; debounce
static timer := false
; display tooltip and clear after the specified seconds
static ShowTip(raw, x := 100, y := 50, weight := 1, duration := 4000) {
ToolTip raw, x, y, weight
Tip.SetTimerRemoveToolTip(weight, duration)
}
; remove the specified weight after some time
static SetTimerRemoveToolTip(weight := 1, time := 4000) {
if Tip.timer {
; clear prior timer
SetTimer Tip.timer, 0
}
; bind the timer
Tip.timer := ObjBindMethod(this, 'RemoveToolTip', weight)
; start new timer
SetTimer Tip.timer, -time
}
static RemoveToolTip(weight)
{
ToolTip , , , weight
}
}