Code: Select all
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=30079
#NoEnv
#SingleInstance Force
hIcon := LoadPicture("shell32.dll", "Icon131", ImageType)
Gui, Color, Black
Gui, Font, s14 cWhite, Tahoma
Gui, Add, Text, HwndhText gReturn, Experiment with`ncustom fonts.
hTT := AddTooltip(hText)
AddTooltip(hText, "Tooltip with icon, title, and custom font")
AddTooltip("Title", "My Tooltip Title", hIcon)
AddToolTip("AutoPopDelay", 15)
SetCtrlFont(hTT, "s12", "Calibri")
Gui, Show
Return:
Return
SetCtrlFont(CtrlHwnd, FontOptions := "", FontName := "") {
static WM_SETFONT := 0x0030, WM_GETFONT := 0x0031
DefaultGui := A_DefaultGui
Gui, New
Gui, Font, % FontOptions, % FontName
Gui, Add, Text, hwndhText, Text
hFont := DllCall("SendMessage", "Ptr", hText, "UInt", WM_GETFONT, "Ptr", 0, "Ptr", 0, "Ptr")
Gui, Destroy
Gui, %DefaultGui%:Default
DllCall("SendMessage", "Ptr", CtrlHwnd, "UInt", WM_SETFONT, "Ptr", hFont, "Ptr", true)
Return hFont
}
GuiClose:
ExitApp
;------------------------------
;
; Function: AddTooltip v2.0
;
; Description:
;
; Add/Update tooltips to GUI controls.
;
; Parameters:
;
; p1 - Handle to a GUI control. Alternatively, set to "Activate" to enable
; the tooltip control, "AutoPopDelay" to set the autopop delay time,
; "Deactivate" to disable the tooltip control, or "Title" to set the
; tooltip title.
;
; p2 - If p1 contains the handle to a GUI control, this parameter should
; contain the tooltip text. Ex: "My tooltip". Set to null to delete the
; tooltip attached to the control. If p1="AutoPopDelay", set to the
; desired autopop delay time, in seconds. Ex: 10. Note: The maximum
; autopop delay time is ~32 seconds. If p1="Title", set to the title of
; the tooltip. Ex: "Bob's Tooltips". Set to null to remove the tooltip
; title. See the *Title & Icon* section for more information.
;
; p3 - Tooltip icon. See the *Title & Icon* section for more information.
;
; Returns:
;
; The handle to the tooltip control.
;
; Requirements:
;
; AutoHotkey v1.1+ (all versions).
;
; Title & Icon:
;
; To set the tooltip title, set the p1 parameter to "Title" and the p2
; parameter to the desired tooltip title. Ex: AddTooltip("Title","Bob's
; Tooltips"). To remove the tooltip title, set the p2 parameter to null. Ex:
; AddTooltip("Title","").
;
; The p3 parameter determines the icon to be displayed along with the title,
; if any. If not specified or if set to 0, no icon is shown. To show a
; standard icon, specify one of the standard icon identifiers. See the
; function's static variables for a list of possible values. Ex:
; AddTooltip("Title","My Title",4). To show a custom icon, specify a handle
; to an image (bitmap, cursor, or icon). When a custom icon is specified, a
; copy of the icon is created by the tooltip window so if needed, the original
; icon can be destroyed any time after the title and icon are set.
;
; Setting a tooltip title may not produce a desirable result in many cases.
; The title (and icon if specified) will be shown on every tooltip that is
; added by this function.
;
; Remarks:
;
; The tooltip control is enabled by default. There is no need to "Activate"
; the tooltip control unless it has been previously "Deactivated".
;
; This function returns the handle to the tooltip control so that, if needed,
; additional actions can be performed on the Tooltip control outside of this
; function. Once created, this function reuses the same tooltip control.
; If the tooltip control is destroyed outside of this function, subsequent
; calls to this function will fail.
;
; Credit and History:
;
; Original author: Superfraggle
; * Post: <http://www.autohotkey.com/board/topic/27670-add-tooltips-to-controls/>
;
; Updated to support Unicode: art
; * Post: <http://www.autohotkey.com/board/topic/27670-add-tooltips-to-controls/page-2#entry431059>
;
; Additional: jballi.
; Bug fixes. Added support for x64. Removed Modify parameter. Added
; additional functionality, constants, and documentation.
;
;-------------------------------------------------------------------------------
AddTooltip(p1,p2:="",p3:="")
{
Static hTT
;-- Misc. constants
,CW_USEDEFAULT:=0x80000000
,HWND_DESKTOP :=0
;-- Tooltip delay time constants
,TTDT_AUTOPOP:=2
;-- Set the amount of time a tooltip window remains visible if
; the pointer is stationary within a tool's bounding
; rectangle.
;-- Tooltip styles
,TTS_ALWAYSTIP:=0x1
;-- Indicates that the tooltip control appears when the cursor
; is on a tool, even if the tooltip control's owner window is
; inactive. Without this style, the tooltip appears only when
; the tool's owner window is active.
,TTS_NOPREFIX:=0x2
;-- Prevents the system from stripping ampersand characters from
; a string or terminating a string at a tab character.
; Without this style, the system automatically strips
; ampersand characters and terminates a string at the first
; tab character. This allows an application to use the same
; string as both a menu item and as text in a tooltip control.
;-- TOOLINFO uFlags
,TTF_IDISHWND:=0x1
;-- Indicates that the uId member is the window handle to the
; tool. If this flag is not set, uId is the identifier of the
; tool.
,TTF_SUBCLASS:=0x10
;-- Indicates that the tooltip control should subclass the
; window for the tool in order to intercept messages, such
; as WM_MOUSEMOVE. If this flag is not used, use the
; TTM_RELAYEVENT message to forward messages to the tooltip
; control. For a list of messages that a tooltip control
; processes, see TTM_RELAYEVENT.
;-- Tooltip icons
,TTI_NONE :=0
,TTI_INFO :=1
,TTI_WARNING :=2
,TTI_ERROR :=3
,TTI_INFO_LARGE :=4
,TTI_WARNING_LARGE:=5
,TTI_ERROR_LARGE :=6
;-- Extended styles
,WS_EX_TOPMOST:=0x8
;-- Messages
,TTM_ACTIVATE :=0x401 ;-- WM_USER + 1
,TTM_ADDTOOLA :=0x404 ;-- WM_USER + 4
,TTM_ADDTOOLW :=0x432 ;-- WM_USER + 50
,TTM_DELTOOLA :=0x405 ;-- WM_USER + 5
,TTM_DELTOOLW :=0x433 ;-- WM_USER + 51
,TTM_GETTOOLINFOA :=0x408 ;-- WM_USER + 8
,TTM_GETTOOLINFOW :=0x435 ;-- WM_USER + 53
,TTM_SETDELAYTIME :=0x403 ;-- WM_USER + 3
,TTM_SETMAXTIPWIDTH:=0x418 ;-- WM_USER + 24
,TTM_SETTITLEA :=0x420 ;-- WM_USER + 32
,TTM_SETTITLEW :=0x421 ;-- WM_USER + 33
,TTM_UPDATETIPTEXTA:=0x40C ;-- WM_USER + 12
,TTM_UPDATETIPTEXTW:=0x439 ;-- WM_USER + 57
;-- Save/Set DetectHiddenWindows
l_DetectHiddenWindows:=A_DetectHiddenWindows
DetectHiddenWindows On
;-- Tooltip control exists?
if not hTT
{
;-- Create Tooltip window
hTT:=DllCall("CreateWindowEx"
,"UInt",WS_EX_TOPMOST ;-- dwExStyle
,"Str","TOOLTIPS_CLASS32" ;-- lpClassName
,"Ptr",0 ;-- lpWindowName
,"UInt",TTS_ALWAYSTIP|TTS_NOPREFIX ;-- dwStyle
,"UInt",CW_USEDEFAULT ;-- x
,"UInt",CW_USEDEFAULT ;-- y
,"UInt",CW_USEDEFAULT ;-- nWidth
,"UInt",CW_USEDEFAULT ;-- nHeight
,"Ptr",HWND_DESKTOP ;-- hWndParent
,"Ptr",0 ;-- hMenu
,"Ptr",0 ;-- hInstance
,"Ptr",0 ;-- lpParam
,"Ptr") ;-- Return type
;-- Disable visual style
; Note: Uncomment the following to disable the visual style, i.e.
; remove the window theme, from the tooltip control. Since this
; function only uses one tooltip control, all tooltips created by this
; function will be affected.
;;;;; DllCall("uxtheme\SetWindowTheme","Ptr",hTT,"Ptr",0,"UIntP",0)
;-- Set the maximum width for the tooltip window
; Note: This message makes multi-line tooltips possible
SendMessage TTM_SETMAXTIPWIDTH,0,A_ScreenWidth*96//A_ScreenDPI,,ahk_id %hTT%
}
;-- Other commands
if p1 is not Integer
{
if (p1="Activate")
SendMessage TTM_ACTIVATE,True,0,,ahk_id %hTT%
if (p1="Deactivate")
SendMessage TTM_ACTIVATE,False,0,,ahk_id %hTT%
if (InStr(p1,"AutoPop")=1) ;-- Starts with "AutoPop"
SendMessage TTM_SETDELAYTIME,TTDT_AUTOPOP,p2*1000,,ahk_id %hTT%
if (p1="Title")
{
;-- If needed, truncate the title
if (StrLen(p2)>99)
p2:=SubStr(p2,1,99)
;-- Icon
if p3 is not Integer
p3:=TTI_NONE
;-- Set title
SendMessage A_IsUnicode ? TTM_SETTITLEW:TTM_SETTITLEA,p3,&p2,,ahk_id %hTT%
}
;-- Restore DetectHiddenWindows
DetectHiddenWindows %l_DetectHiddenWindows%
;-- Return the handle to the tooltip control
Return hTT
}
;-- Create/Populate the TOOLINFO structure
uFlags:=TTF_IDISHWND|TTF_SUBCLASS
cbSize:=VarSetCapacity(TOOLINFO,(A_PtrSize=8) ? 72:48,0)
NumPut(cbSize, TOOLINFO,0,"UInt") ;-- cbSize
NumPut(uFlags, TOOLINFO,4,"UInt") ;-- uFlags
NumPut(HWND_DESKTOP,TOOLINFO,8,"Ptr") ;-- hwnd
NumPut(p1, TOOLINFO,(A_PtrSize=8) ? 16:12,"Ptr")
;-- uId
;-- Check to see if tool has already been registered for the control
SendMessage
,A_IsUnicode ? TTM_GETTOOLINFOW:TTM_GETTOOLINFOA
,0
,&TOOLINFO
,,ahk_id %hTT%
l_RegisteredTool:=ErrorLevel
;-- Update the TOOLTIP structure
NumPut(&p2,TOOLINFO,(A_PtrSize=8) ? 48:36,"Ptr")
;-- lpszText
;-- Add, Update, or Delete tool
if l_RegisteredTool
{
if StrLen(p2)
SendMessage
,A_IsUnicode ? TTM_UPDATETIPTEXTW:TTM_UPDATETIPTEXTA
,0
,&TOOLINFO
,,ahk_id %hTT%
else
SendMessage
,A_IsUnicode ? TTM_DELTOOLW:TTM_DELTOOLA
,0
,&TOOLINFO
,,ahk_id %hTT%
}
else
if StrLen(p2)
SendMessage
,A_IsUnicode ? TTM_ADDTOOLW:TTM_ADDTOOLA
,0
,&TOOLINFO
,,ahk_id %hTT%
;-- Restore DetectHiddenWindows
DetectHiddenWindows %l_DetectHiddenWindows%
;-- Return the handle to the tooltip control
Return hTT
}