if you work with another than the default Gui. That's why your approach will only work for Gui 1.
To change control's text color is different from showing tooltips on entering as well as leaving a Gui window. So I changed the concept to rely on window IDs (HWNDs) instead of titles:
Code: Select all
#NoEnv
; Variable which holds the ID (HWND) of the currently hovered window
PrevID := ""
; Associative array of Gui window IDs (HWNDs) and the associated control text
IDArray := []
Gui, 1: +AlwaysOnTop -Caption -Sysmenu -Border +HwndHGUI ; Hwnd option added
Gui, 1: Color, FF9900
Gui, 1: Font, cBlack
Gui, 1: Margin, 0, 0
Gui, 1: Add, Text, w80 h30 cBlack Center +0x0200 vWINAMP gClicked, WINAMP ; control name added
Gui, 1: Show, x100 y100, WINAMP
IDArray[HGUI] := "WINAMP" ; store the ID of the Gui and the associated control name in the array
Gui, 2: +AlwaysOnTop -Caption -Sysmenu -Border +HwndHGUI
Gui, 2: Color, 99CCFF
Gui, 2: Font, cBlack
Gui, 2: Margin, 0, 0
Gui, 2: Add, Text, w80 h30 cBlack Center +0x0200 vNOTEPAD gClicked, NOTEPAD
Gui, 2: Show, x100 y130, NOTEPAD
IDArray[HGUI] := "NOTEPAD"
Gui, 3: +AlwaysOnTop -Caption -Sysmenu -Border +HwndHGUI
Gui, 3: Color, 669966
Gui, 3: Font, cBlack
Gui, 3: Margin, 0, 0
Gui, 3: Add, Text, w80 h30 cBlack Center +0x0200 vPICTUS gClicked, PICTUS
Gui, 3: Show, x100 y160, PICTUS
IDArray[HGUI] := "PICTUS"
SetTimer, MouseOverGUI, 125
Return
MouseOverGUI:
MouseGetPos, , , WinID
If (WinID <> PrevID)
{
If IDArray.HasKey(PrevID) { ; the previous window is one of the GUI windows
Gui, %PrevID%: Font, cBlack
GuiControl, %PrevID%: Font, % IDArray[PrevID]
}
If IDArray.HasKey(WinID) { ; the new window is one of the GUI windows
Gui, %WinID%: Font, cWhite
GuiControl, %WinID%: Font, % IDArray[WinID]
}
PrevID := WinID
}
Return
Clicked:
ToolTip, You clicked on %A_GuiControl% so do what you want here!
SetTimer, KillTT, -1000
Return
KillTT:
ToolTip
Return
But you might be interested in another concept using buttons. All you need is the
. I added it to the example:
Code: Select all
#NoEnv
Gui, +AlwaysOnTop -Caption
Gui, Margin, 0, 0
Gui, Add, Button, xm w80 h30 vWINAMP gClicked +HwndHBTN, WINAMP
FCB_Create(HBTN, [0xFF9900], ["Black", "White"])
Gui, Add, Button, xm w80 h30 vNOTEPAD gClicked +HwndHBTN, NOTEPAD
FCB_Create(HBTN, [0x99CCFF], ["Black", "White"])
Gui, Add, Button, xm w80 h30 vPICTUS gClicked +HwndHBTN, PICTUS
FCB_Create(HBTN, [0x669966], ["Black", "White"])
Gui, Show, x100 y100
Return
GuiEscape:
ExitApp
Clicked:
ToolTip, You clicked on %A_GuiControl% so do what you want here!
SetTimer, KillTT, -1000
Return
KillTT:
ToolTip
Return
; ================================================================================================================================
; Function: Creates flat colored push buttons with up to 6 colors for the different button states.
; Syntax: FCB_Create(Hwnd, BkgColors[, TxtColors])
; Parameters:
; Hwnd - The handle (HWND) to the button.
; BkgColors - An array of up to 6 color values used to draw the background of the button.
; The first element is mandatory. Its value will be used for all other omitted or empty elements.
; TxtColors - An array of up to 6 color values used to draw the text of the button.
; If the first element is omitted it will be set to the default text color.
; The value of the first element will be used for all other omitted or empty elements.
; Default: "" -> use the default text color for all states.
; Return values:
; Returns True on success; otherwise False.
; Remarks:
; All color values can be passed either as AHK HTML color name (e.g. "Navy") or RGB integer value (e.g. 0xRRGGBB).
;
; MSDN defines 6 states (msdn.microsoft.com/en-us/library/windows/desktop/bb775975(v=vs.85).aspx):
; PBS_NORMAL = 1
; PBS_HOT = 2
; PBS_PRESSED = 3
; PBS_DISABLED = 4
; PBS_DEFAULTED = 5
; PBS_STYLUSHOT = 6
; On Win Vista and 7 the colors specified for states 5 and 6 are used to animate the current default button.
; ================================================================================================================================
FCB_Create(Hwnd, BkgColors, TxtColors := "") {
; System colors
Static DefTxtColor := DllCall("GetSysColor", "Int", 18, "UInt") ; default text color
, DisBkgColor := "" ; disabled background color
, DisTxtColor := DllCall("GetSysColor", "Int", 17, "UInt") ; disabled text color
; Stock objects
Static DCB := DllCall("GetStockObject", "Int", 18, "UPtr") ; DC_BRUSH
, DCP := DllCall("GetStockObject", "Int", 19, "UPtr") ; DC_PEN
; Messages
Static WM_GETFONT := 0x31
; Styles
Static BS_DEFPUSHBUTTON := 0x01, BS_LEFT := 0x0100, BS_RIGHT := 0x0200, BS_CENTER := 0x0300, BS_TOP := 0x0400
, BS_BOTTOM := 0x0800, BS_VCENTER := 0x0C00, BS_MULTILINE := 0x2000, WS_DISABLED := 0x08000000
, BS_CHECKBOX := 0x02, BS_RADIOBUTTON := 0x04, BS_GROUPBOX := 0x07, BS_AUTORADIOBUTTON := 0x09
, RCBUTTONS := BS_CHECKBOX | BS_RADIOBUTTON | BS_AUTORADIOBUTTON
; DrawText format flags
Static DT_LEFT := 0x00, DT_TOP := 0x00, DT_CENTER := 0x01, DT_RIGHT := 0x02, DT_VCENTER := 0x04
, DT_BOTTOM := 0x08, DT_WORDBREAK := 0x10, DT_SINGLELINE := 0x20, DT_NOCLIP := 0x0100, DT_CALCRECT := 0x0400
, DT_END_ELLIPSIS := 0x8000, DT_PATH_ELLIPSIS := 0x4000
; Image list flags
Static ILC_COLOR32 := 0x20
; =============================================================================================================================
; Get the backgound color for disabled buttons on first call
If (DisBkgColor = "") {
LGB := DllCall("GetStockObject", "Int", 1, "UPtr") ; LTGRAY_BRUSH
VarSetCapacity(LB, 16, 0) ; LOGBRUSH
DllCall("GetObject", "Ptr", LGB, "Int", A_PtrSize = 8 ? 16 : 12, "Ptr", &LB)
DisBkgColor := NumGet(LB, 4, "UInt")
DllCall("DeleteObject", "Ptr", LGB)
}
; -----------------------------------------------------------------------------------------------------------------------------
; Check the control parameter
If !DllCall("IsWindow", "Ptr", Hwnd, "UPtr")
Return False
; Get and check control's class and styles
WinGetClass, BtnClass, ahk_id %Hwnd%
ControlGet, BtnStyle, Style, , , ahk_id %Hwnd%
If (BtnClass != "Button") || ((BtnStyle & 0xF ^ BS_GROUPBOX) = 0) || ((BtnStyle & RCBUTTONS) > 1)
Return False
; -----------------------------------------------------------------------------------------------------------------------------
; Check and convert the color parameters
; Background colors
If !(IsObject(BkgColors)) || (BkgColors[1] = "")
Return False
If !(BkgArray := FCB_GetColors(BkgColors, 0))
Return False
; Text colors
If !(IsObject(TxtColors)) || (TxtColors.MaxIndex() = "")
TxtArray := [DefTxtColor, DefTxtColor, DefTxtColor, DefTxtColor, DefTxtColor, DefTxtColor]
Else If !(TxtArray := FCB_GetColors(TxtColors, DefTxtColor))
Return False
; -----------------------------------------------------------------------------------------------------------------------------
; Get the button's properties.
HFN := DllCall("SendMessage", "Ptr", Hwnd, "UInt", 0x31, "Ptr", 0, "Ptr", 0, "UPtr") ; WM_GETFONT
ControlGetText, BtnCaption, , ahk_id %Hwnd%
VarSetCapacity(CtlRect, 16, 0)
DllCall("GetClientRect", "Ptr", Hwnd, "Ptr", &CtlRect)
CtlW := NumGet(CtlRect, 8, "Int")
CtlH := NumGet(CtlRect, 12, "Int")
; -----------------------------------------------------------------------------------------------------------------------------
; Create a compatible memory DC and a compatible bitmap for drawing
HDC := DllCall("GetDC", "Ptr", Hwnd, "UPtr")
MDC := DllCall("CreateCompatibleDC", "Ptr", HDC, "UPtr")
HBM := DllCall("CreateCompatibleBitmap", "Ptr", HDC, "Int", CtlW, "Int", CtlH, "UPtr")
HBM := DllCall("CopyImage", "Ptr", HBM, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x2008, "UPtr")
DllCall("SelectObject", "Ptr", MDC, "Ptr", DCB)
DllCall("SelectObject", "Ptr", MDC, "Ptr", DCP)
DllCall("SelectObject", "Ptr", MDC, "Ptr", HFN)
DllCall("ReleaseDC", "Ptr", Hwnd, "Ptr", HDC)
; -----------------------------------------------------------------------------------------------------------------------------
; Calculate the text rectangle
CenterBtn := BtnStyle & BS_CENTER
DT_ALIGN := CenterBtn = BS_CENTER ? DT_CENTER
: CenterBtn = BS_RIGHT ? DT_RIGHT
: CenterBtn = BS_LEFT ? DT_LEFT
: DT_CENTER
DT_ALIGN |= (BtnStyle & BS_MULTILINE) ? DT_WORDBREAK : DT_END_ELLIPSIS
VC := BtnStyle & BS_VCENTER
If (VC = BS_VCENTER) || (VC = BS_BOTTOM) || (VC = 0) {
VarSetCapacity(TxtRect, 16, 0)
NumPut(NumGet(CtlRect, 0, "Int64"), TxtRect, 0, "Int64")
NumPut(NumGet(CtlRect, 8, "Int64"), TxtRect, 8, "Int64")
TxtT := NumGet(TxtRect, 4, "Int")
TxtB := NumGet(TxtRect, 12, "Int")
DllCall("DrawText", "Ptr", MDC, "Str", BtnCaption, "Int", -1, "Ptr", &TxtRect, "UInt", DT_ALIGN | DT_CALCRECT)
D := TxtB - NumGet(TxtRect, 12, "Int")
TxtT += (VC = BS_BOTTOM) ? D : D // 2
NumPut(TxtT, CtlRect, 4, "Int")
NumPut(TxtB, CtlRect, 12, "Int")
}
; -----------------------------------------------------------------------------------------------------------------------------
; Create the bitmaps for each button state and add them to an image list
HIL := DllCall("ImageList_Create", "UInt", CtlW, "UInt", CtlH, "UInt", ILC_COLOR32, "Int", 6, "Int", 0, "UPtr")
Loop, 6 {
OBM := DllCall("SelectObject", "Ptr", MDC, "Ptr", HBM)
BtnDisabled := BtnStyle & WS_DISABLED
BkgColor := BtnDisabled ? DisBkgColor : BkgArray[A_Index]
TxtColor := BtnDisabled ? DisTxtColor : TxtArray[A_Index]
DllCall("SetDCBrushColor", "Ptr", MDC, "UInt", BkgColor)
DllCall("SetDCPenColor", "Ptr", MDC, "UInt", BkgColor)
DllCall("Rectangle", "Ptr", MDC, "Int", 0, "Int", 0, "Int", CtlW, "Int", CtlH)
DllCall("SetBkMode", "Ptr", MDC, "Int", 1)
DllCall("SetTextColor", "Ptr", MDC, "UInt", TxtColor)
DllCall("DrawText", "Ptr", MDC, "Str", BtnCaption, "Int", -1, "Ptr", &CtlRect, "UInt", DT_ALIGN)
DllCall("SelectObject", "Ptr", MDC, "Ptr", OBM)
DllCall("ImageList_Add", "Ptr", HIL, "Ptr", HBM, "Ptr", 0)
If (BtnDisabled)
Break
}
; -----------------------------------------------------------------------------------------------------------------------------
; Create a BUTTON_IMAGELIST structure
VarSetCapacity(BIL, 20 + A_PtrSize, 0)
NumPut(HIL, BIL, 0, "Ptr")
Numput(4, BIL, A_PtrSize + 16, "UInt") ; BUTTON_IMAGELIST_ALIGN_CENTER
; Assign the ImageList to the button
Control, Style, +0x0080, , ahk_id %Hwnd% ; BS_BITMAP
DllCall("SendMessage", "Ptr", Hwnd, "UInt", 0x1602, "Ptr", 0, "Ptr", 0) ; BCM_SETIMAGELIST
DllCall("SendMessage", "Ptr", Hwnd, "UInt", 0x1602, "Ptr", 0, "Ptr", &BIL)
; -----------------------------------------------------------------------------------------------------------------------------
; Free the GDI objects
DllCall("DeleteDC", "Ptr", MDC)
DllCall("DeleteObject", "Ptr", HBM)
; -----------------------------------------------------------------------------------------------------------------------------
; All done
Return True
}
; ================================================================================================================================
; Internally called function to convert the colors to BGR
; ================================================================================================================================
FCB_GetColors(ColorArray, DefColor) {
; HTML colors (BGR)
Static HTML := {BLACK: 0x000000, GRAY: 0x808080, SILVER: 0xC0C0C0, WHITE: 0xFFFFFF, MAROON: 0x000080, PURPLE: 0x800080
, FUCHSIA: 0xFF00FF, RED: 0x0000FF, GREEN: 0x008000, OLIVE: 0x008080, YELLOW: 0x00FFFF, LIME: 0x00FF00
, NAVY: 0x800000, TEAL: 0x808000, AQUA: 0xFFFF00, BLUE: 0xFF0000}
Colors := []
Loop, 6 {
CurrColor := ColorArray[A_Index]
If (CurrColor = "")
CurrColor := A_Index = 1 ? DefColor : Colors[1]
Else If (HTML.HasKey(CurrColor))
CurrColor := HTML[CurrColor]
Else
CurrColor := ((CurrColor & 0xFF0000) >> 16) | (CurrColor & 0x00FF00) | ((CurrColor & 0x0000FF) << 16)
If (CurrColor = "")
Return False
Colors[A_Index] := CurrColor
}
Return Colors
}