Тема: AHK v2: Альтернатива стандартному ToolTip
Конвертация версии для AHK v1.
Возможности альтернативного Tooltip:
Включение прозрачности, в том числе прозрачность для кликов мышью;
Автоматическое пропадание после указанного времени;
Пропадание при нажатии;
Изменение параметров шрифта;
Изменение цвета фона;
Уменьшенное мерцание при частом обновлении текста;
Работа с несколькими мониторами.
class Tool_Tip
{
; #requires AutoHotkey v2.0.19
; Tool_Tip by Alectric
; https://forum.script-coding.com/
; Метод Show(Text:="",X:="",Y:="",Delay:=0,Trans:="",BackColor:="",Font:="",FontName:="",WW:=0,HH:=0)
; Text - Текст который необходимо отобразить
; X, Y - Координаты появления относительно экрана
; Delay - задержка пропадания после вызова в миллисекундах
; Trans - Прозрачность в формате AHK Gui (но от 2 до 255), false - не прозрачно, true - прозрачность=180; (также становится прозрачен для кликов мыши)
; BackColor - Цвет фона
; Font - Параметры шрифта в формате Gui (цвет, размер, наклон, толщина, подчеркнутый, зачеркнутый)
; FontName - Название шрифта
; WW, HH - Фиксированные ширина и высота (если оба значения не 0, то отрисовка в ~5 раз быстрее)
;
; Метод Hide() - Скрывает Tooltip
;
; Свойство Pos - Возвращает объект с параметрами окна {X:X,Y:Y,W:W,H:H}.
static DPICoef:=Tool_Tip.GetDPICoef()
__Delete()
{
ObjAddRef(ObjPtr(this))
SetTimer this.obm,0
this.TTW.Destroy()
this.obm:=unset
; tooltip "Деструктор вызван"
; sleep 1000
; tooltip
}
__New()
{
this.Visible:=false
this.X:=0
this.Y:=0
this.W:=0
this.H:=0
this.BackColor:="0xFFFFFF"
this.Trans:=255
this.Text:=""
this.Font:="s9"
this.FontName:="Arial"
this.TTW:=Gui("+ToolWindow -sysmenu -Resize +AlwaysOnTop -Caption +Border +E0x2080000")
this.TTW.SetFont(this.Font,this.FontName)
this.TTW.BackColor:=this.BackColor
this.TTW.MarginX:=2
this.TTW.MarginY:=2
this.TTT:=this.TTW.Add("Text")
this.obm:=ObjBindMethod(this,"Hide")
ObjRelease(ObjPtr(this))
this.TTW.OnEvent("ContextMenu",this.obm)
this.TTT.OnEvent("Click",this.obm)
}
Pos
{
get
{
this.TTW.GetPos(&X,&Y,&W,&H)
return {X:X,Y:Y,W:W,H:H}
}
}
Hide(*)
{
this.TTW.Hide()
this.Visible:=false
}
Show(Text:="",X:="",Y:="",Delay:=0,Trans:="",BackColor:="",Font:="",FontName:="",WW:=0,HH:=0)
{
SaveCoordMode:=A_CoordModeMouse
CoordMode "Mouse","Screen"
MouseGetPos(&mX,&mY,&mW,&mC,2)
CoordMode "Mouse",SaveCoordMode
if (X="")
X:=mX+10
if (Y="")
Y:=mY+20
if ((Font!="" and Font!=this.Font) or (FontName!="" and FontName!=this.FontName))
{
this.Font:=Font
this.FontName:=FontName
this.TTT.SetFont(this.Font,this.FontName)
}
W:=this.W
H:=this.H
if (Text!="" and Text!=this.Text)
{
this.Text:=Text
if (!WW or !HH)
{
ControlSetStyle("-1",this.TTT)
Size:=this.Measure(Text)
}
else
{
ControlSetStyle("+1",this.TTT)
Size:={X:X,Y:Y,W:WW,H:HH}
}
this.TTT.Move(,,Size.W,Size.H)
this.TTT.Value:=Text
W:=Size.W+8
H:=Size.H+8
}
B:=this.Bound(X,Y,W,H)
W:=Round(W/Tool_Tip.DPICoef)
H:=Round(H/Tool_Tip.DPICoef)
X:=B.X
Y:=B.Y
if (X!=this.X or Y!=this.Y or W!=this.W or H!=this.H or BackColor!=this.BackColor or !this.Visible)
{
this.X:=X
this.Y:=Y
this.W:=W
this.H:=H
this.Visible:=true
if (BackColor!="" and BackColor!=this.BackColor)
{
this.BackColor:=BackColor
this.TTW.BackColor:=BackColor
}
this.TTW.show("x" X " y" Y " w" W " h" H " NoActivate")
}
if (Trans!="" and Trans!=this.Trans)
{
this.Trans:=Trans
if Trans
{
WinSetTransparent(Trans=1?180:Trans,this.TTW)
WinSetExStyle("+0x20",this.TTW)
}
else
{
WinSetTransparent(255,this.TTW)
WinSetExStyle("-0x20",this.TTW)
}
}
SetTimer this.obm,-abs(Delay)
}
Measure(Text)
{
G:=Gui()
G.SetFont(this.Font,this.FontName)
G.MarginX:=2
G.MarginY:=2
T:=G.Add("Text",,Text)
Controlgetpos(&X,&Y,&W,&H,T)
G.Destroy()
return {X:X,Y:Y,W:W,H:H}
}
Bound(X,Y,W:=0,H:=0)
{
this.last:=1
Loop MonitorGetCount()
{
MonitorGet(A_Index,&Left,&Top,&Right,&Bottom)
if (X>=Left and X<=Right and Y>=Top and Y<=Bottom)
{
MonitorGetWorkArea(A_Index,&Left,&Top,&Right,&Bottom)
this.last:=a_index
return {X:(X+W>Right?Right-W:X)
,Y:(Y+H>Bottom?Bottom-H:Y)
,B:a_index}
}
}
MonitorGetWorkArea(this.last,&Left,&Top,&Right,&Bottom)
return {X:(X+W>Right?Right-W:X<Left?Left:X)
,Y:(Y+H>Bottom?Bottom-H:Y<Top?Top:Y)
,B:this.last}
}
static GetDPICoef()
{
G:=Gui()
T:=G.Add("Text","x0 y0 w1000 h1000")
Controlgetpos(,,&W,,T)
G.Destroy()
return W/1000
}
}

