Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MyTrayTip := new CustomToolTip({ text: "Hello, World!", title: "traytip", trayTip: true, icon: 1, backColor: 0xFFC141
- , textColor: 0x50669F, fontName: "Calibri", fontOptions: "s18", timeout: 3000 })
- Sleep, 4000
- ExitApp
- class CustomToolTip
- {
- /*
- Options Description
- text, title
- x, y if not specified, near the cursor
- icon 1 — Info, 2 — Warning, 3 — Error, greater than 3 — HICON
- margin array [left, top, right, bottom]
- transparent, closeButton true/false
- textColor, backColor both colors must be in the RGB numeric format
- fontName, fontOptions like in GUI
- trayTip, isBallon true/false
- timeout ms
- maxWidth default value 600
- */
- __New( Options )
- {
- static ttStyles := (TTS_NOPREFIX := 2) | (TTS_ALWAYSTIP := 1), TTS_BALLOON := 0x40, TTS_CLOSE := 0x80
- , TTF_TRACK := 0x20, TTF_ABSOLUTE := 0x80
- , TTM_SETMAXTIPWIDTH := 0x418, TTM_TRACKACTIVATE := 0x411, TTM_TRACKPOSITION := 0x412
- , TTM_SETTIPBKCOLOR := 0x413, TTM_SETTIPTEXTCOLOR := 0x414, TTM_SETMARGIN := 0x41A
- , TTM_ADDTOOL := A_IsUnicode ? 0x432 : 0x404
- , TTM_SETTITLE := A_IsUnicode ? 0x421 : 0x420
- , TTM_UPDATETIPTEXT := A_IsUnicode ? 0x439 : 0x40C
- , WM_SETFONT := 0x30, WM_GETFONT := 0x31
- , WS_EX_TRANSPARENT := 0x00000020
- , exStyles := (WS_EX_TOPMOST := 0x00000008) | (WS_EX_COMPOSITED := 0x2000000) | (WS_EX_LAYERED := 0x00080000)
- dhwPrev := A_DetectHiddenWindows, O := Options
- DetectHiddenWindows, On
- defGuiPrev := A_DefaultGui, lastFoundPrev := WinExist()
- (o.trayTip && o.isBallon := true)
- this.hWnd := hWnd := DllCall("CreateWindowEx", "UInt", exStyles | WS_EX_TRANSPARENT * !!o.transparent, "Str", "tooltips_class32", "Str", ""
- , "UInt", ttStyles | TTS_CLOSE * !!o.closeButton | TTS_BALLOON * !!o.isBallon
- , "Int", 0, "Int", 0, "Int", 0, "Int", 0, "Ptr", 0, "Ptr", 0, "Ptr", 0, "Ptr", 0, "Ptr")
- WinExist("ahk_id" . hWnd)
- if (o.textColor != "" || o.backColor != "") {
- (o.backColor = "" && o.backColor := 0xFFFFFF)
- DllCall("UxTheme\SetWindowTheme", "Ptr", hWnd, "Ptr", 0, "Str", "")
- ByteSwap := Func("DllCall").Bind("msvcr100\_byteswap_ulong", "UInt")
- SendMessage, TTM_SETTIPBKCOLOR , ByteSwap.Call(o.backColor << 8)
- SendMessage, TTM_SETTIPTEXTCOLOR, ByteSwap.Call(o.textColor << 8)
- }
- if (o.fontName || o.fontOptions) {
- Gui, New
- Gui, Font, % o.fontOptions, % o.fontName
- Gui, Add, Text, hwndhText
- SendMessage, WM_GETFONT,,,, ahk_id %hText%
- SendMessage, WM_SETFONT, ErrorLevel
- Gui, Destroy
- Gui, %defGuiPrev%: Default
- }
- if o.trayTip {
- try Loc := this.TrackTrayIcon.GetTrayIconLocation()
- o.x := Loc.x + Loc.w//2
- o.y := Loc.y + 4
- }
- if (o.x = "" || o.y = "")
- DllCall("GetCursorPos", "Int64P", pt)
- (o.x = "" && o.x := (pt & 0xFFFFFFFF) + 15), (o.y = "" && o.y := (pt >> 32) + 15)
- if IsObject(o.margin) {
- VarSetCapacity(RECT, 16, 0)
- for k, v in o.margin
- NumPut(v, RECT, 4*(A_Index - 1), "UInt")
- SendMessage, TTM_SETMARGIN,, &RECT
- }
- VarSetCapacity(TOOLINFO, sz := 24 + A_PtrSize*6, 0)
- NumPut(sz, TOOLINFO)
- NumPut(TTF_TRACK | TTF_ABSOLUTE * !o.isBallon, TOOLINFO, 4)
- NumPut(ObjGetAddress(Options, "text"), TOOLINFO, 24 + A_PtrSize*3)
- SendMessage, TTM_SETTITLE , o.icon, ObjGetAddress(Options, "title")
- SendMessage, TTM_TRACKPOSITION , , o.x | (o.y << 16)
- SendMessage, TTM_SETMAXTIPWIDTH, , o.maxWidth ? o.maxWidth : 600
- SendMessage, TTM_ADDTOOL , , &TOOLINFO
- SendMessage, TTM_UPDATETIPTEXT , , &TOOLINFO
- SendMessage, TTM_TRACKACTIVATE , true , &TOOLINFO
- if o.trayTip
- TrackingTray := new this.TrackTrayIcon(hWnd)
- if o.timeout {
- this.timeoutTimer := Timer := Func("DllCall").Bind("DestroyWindow", "Ptr", hWnd)
- SetTimer, % Timer, % "-" . o.timeout
- }
- WinExist("ahk_id" . lastFoundPrev)
- DetectHiddenWindows, % dhwPrev
- }
- UpdateText(newText) {
- static TTM_UPDATETIPTEXT := A_IsUnicode ? 0x439 : 0x40C
- VarSetCapacity(TOOLINFO, sz := 24 + A_PtrSize*6, 0)
- NumPut(sz, TOOLINFO)
- NumPut(&newText, TOOLINFO, 24 + A_PtrSize*3)
- SendMessage, TTM_UPDATETIPTEXT,, &TOOLINFO,, % "ahk_id" . this.hWnd
- }
- class TrackTrayIcon {
- __New(hToolTip) {
- Timer := ObjBindMethod(this, "TrackingTray", hToolTip)
- SetTimer, % Timer, 100
- }
- TrackingTray(hToolTip) {
- static x_prev := 0, y_prev := 0
- if !WinExist("ahk_id " . hToolTip) {
- SetTimer,, Delete
- Return
- }
- try Loc := this.GetTrayIconLocation()
- x := Loc.x + Loc.w//2
- y := Loc.y + 4
- if (x != x_prev || y != y_prev) {
- SendMessage, TTM_TRACKPOSITION := 0x412,, x | (y << 16)
- x_prev := x, y_prev := y
- }
- }
- GetTrayIconLocation(hWnd := 0, uID := 0x404) {
- (!hWnd && hWnd := A_ScriptHwnd)
- VarSetCapacity(NOTIFYICONIDENTIFIER, sz := A_PtrSize*3 + 16, 0)
- NumPut( sz , NOTIFYICONIDENTIFIER)
- NumPut( hWnd , NOTIFYICONIDENTIFIER, A_PtrSize)
- NumPut( uID , NOTIFYICONIDENTIFIER, A_PtrSize*2)
- VarSetCapacity(RECT, 16, 0)
- hr := DllCall("Shell32\Shell_NotifyIconGetRect", "Ptr", &NOTIFYICONIDENTIFIER, "Ptr", &RECT)
- if (hr != 0)
- throw "Failed to get tray icon location"
- else {
- x := NumGet(RECT, "UInt")
- y := NumGet(RECT, 4, "UInt")
- w := NumGet(RECT, 8, "UInt") - x
- h := NumGet(RECT, 12, "UInt") - y
- Loc := {x: x, y: y, w: w, h: h}
- }
- Return Loc
- }
- }
- __Delete() {
- DllCall("DestroyWindow", "Ptr", this.hWnd)
- if this.timeoutTimer {
- Timer := this.timeoutTimer
- try SetTimer, % Timer, Delete
- }
- }
- }
Add Comment
Please, Sign In to add comment