; = Info =========================================================================================== ; Title : Demo for GUICtrlGetBKColor ; -------------------------------------------------------------------------------------------------- ; ; Date : 2020-08-22 ; Author(s) : Professor Bernd (Demo) ; guinness, Bilgus (Code) ; ; Description : Demo to show different codes to determine the background color of controls. ; Unfortunately, some of the codes have weaknesses which are shown in this demo. ; ; Demo zum Zeigen verschiedener Codes zum Ermitteln der Hintergrund-Farbe von ; Controls. Leider haben einige Codes Schwächen, die im Demo aufgezeigt werden. ; ================================================================================================== #include #include #include #include #include Opt("MustDeclareVars", 1) Global $g_hGui = GUICreate("Demo for GUICtrlGetBKColor", 578, 323) ; Labels for titles. Global $g_Label1 = GUICtrlCreateLabel("WinAPI 1", 64, 32, 92, 17, $SS_CENTER) Global $g_Label2 = GUICtrlCreateLabel("WinAPI 2", 184, 32, 92, 17, $SS_CENTER) Global $g_Label3 = GUICtrlCreateLabel("guinness", 304, 32, 92, 17, $SS_CENTER) Global $g_Label4 = GUICtrlCreateLabel("Bilgus", 424, 32, 92, 17, $SS_CENTER) ; Test Buttons lime. Global $g_Btn_Lime_1 = GUICtrlCreateButton("Btn_Lime_1", 64, 64, 89, 33) GUICtrlSetBkColor(-1, 0x00FF00) Global $g_Btn_Lime_2 = GUICtrlCreateButton("Btn_Lime_2", 184, 64, 89, 33) GUICtrlSetBkColor(-1, 0x00FF00) Global $g_Btn_Lime_3 = GUICtrlCreateButton("Btn_Lime_3", 304, 64, 89, 33) GUICtrlSetBkColor(-1, 0x00FF00) Global $g_Btn_Lime_4 = GUICtrlCreateButton("Btn_Lime_4", 424, 64, 89, 33) GUICtrlSetBkColor(-1, 0x00FF00) ; Test Labels yellow. Global $g_Lbl_Yellow_1 = GUICtrlCreateLabel("Lbl_Yellow_1", 64, 112, 89, 33) GUICtrlSetBkColor(-1, 0xFFFF00) Global $g_Lbl_Yellow_2 = GUICtrlCreateLabel("Lbl_Yellow_2", 184, 112, 89, 33) GUICtrlSetBkColor(-1, 0xFFFF00) Global $g_Lbl_Yellow_3 = GUICtrlCreateLabel("Lbl_Yellow_3", 304, 112, 89, 33) GUICtrlSetBkColor(-1, 0xFFFF00) Global $g_Lbl_Yellow_4 = GUICtrlCreateLabel("Lbl_Yellow_4", 424, 112, 89, 33) GUICtrlSetBkColor(-1, 0xFFFF00) ; Buttons to set the new color. Global $g_BtnSetColr_1 = GUICtrlCreateButton("set blue", 64, 168, 89, 33) Global $g_BtnSetColr_2 = GUICtrlCreateButton("set blue", 184, 168, 89, 33) Global $g_BtnSetColr_3 = GUICtrlCreateButton("set blue", 304, 168, 89, 33) Global $g_BtnSetColr_4 = GUICtrlCreateButton("set blue", 424, 168, 89, 33) ; Buttons to reset to the old color. Global $g_BtnResetColr_1 = GUICtrlCreateButton("reset", 64, 216, 91, 33) Global $g_BtnResetColr_2 = GUICtrlCreateButton("reset", 184, 216, 91, 33) Global $g_BtnResetColr_3 = GUICtrlCreateButton("reset", 304, 216, 91, 33) Global $g_BtnResetColr_4 = GUICtrlCreateButton("reset", 424, 216, 91, 33) GUISetState(@SW_SHOW) ; Variables to store the old colors. Global $g_iBackColor_Old_Btn_1 = -1 Global $g_iBackColor_Old_Btn_2 = -1 Global $g_iBackColor_Old_Btn_3 = -1 Global $g_iBackColor_Old_Btn_4 = -1 Global $g_iBackColor_Old_Lbl_1 = -1 Global $g_iBackColor_Old_Lbl_2 = -1 Global $g_iBackColor_Old_Lbl_3 = -1 Global $g_iBackColor_Old_Lbl_4 = -1 Example() Func Example() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop ; ---- Column 1 (WinAPI 1) ---- Case $g_BtnSetColr_1 $g_iBackColor_Old_Btn_1 = GUICtrlGetBkColor_WinAPI_1($g_Btn_Lime_1) ; Lime $g_iBackColor_Old_Lbl_1 = GUICtrlGetBkColor_WinAPI_1($g_Lbl_Yellow_1) ; Yellow GUICtrlSetBkColor($g_Btn_Lime_1, 0xd7edff) ; light blue GUICtrlSetBkColor($g_Lbl_Yellow_1, 0xd7edff) ; light blue DebugPrintFor_SetColor(1) Case $g_BtnResetColr_1 If $g_iBackColor_Old_Btn_1 > -1 Then GUICtrlSetBkColor($g_Btn_Lime_1, $g_iBackColor_Old_Btn_1) If $g_iBackColor_Old_Lbl_1 > -1 Then GUICtrlSetBkColor($g_Lbl_Yellow_1, $g_iBackColor_Old_Lbl_1) DebugPrintFor_ResetColor(1) ; ---- Column 2 (WinAPI 2) ---- Case $g_BtnSetColr_2 $g_iBackColor_Old_Btn_2 = GUICtrlGetBkColor_WinAPI_2($g_Btn_Lime_2) ; Lime $g_iBackColor_Old_Lbl_2 = GUICtrlGetBkColor_WinAPI_2($g_Lbl_Yellow_2) ; Yellow GUICtrlSetBkColor($g_Btn_Lime_2, 0xd7edff) ; light blue GUICtrlSetBkColor($g_Lbl_Yellow_2, 0xd7edff) ; light blue DebugPrintFor_SetColor(2) Case $g_BtnResetColr_2 If $g_iBackColor_Old_Btn_2 > -1 Then GUICtrlSetBkColor($g_Btn_Lime_2, $g_iBackColor_Old_Btn_2) If $g_iBackColor_Old_Lbl_2 > -1 Then GUICtrlSetBkColor($g_Lbl_Yellow_2, $g_iBackColor_Old_Lbl_2) DebugPrintFor_ResetColor(2) ; ---- Column 3 (guinness) ---- ; Unfortunately, functions with GetPixel do not work reliably for controls with frames such as buttons. Case $g_BtnSetColr_3 $g_iBackColor_Old_Btn_3 = GUICtrlGetBkColor_guinness($g_Btn_Lime_3) ; Lime $g_iBackColor_Old_Lbl_3 = GUICtrlGetBkColor_guinness($g_Lbl_Yellow_3) ; Yellow GUICtrlSetBkColor($g_Btn_Lime_3, 0xd7edff) ; light blue GUICtrlSetBkColor($g_Lbl_Yellow_3, 0xd7edff) ; light blue DebugPrintFor_SetColor(3) Case $g_BtnResetColr_3 If $g_iBackColor_Old_Btn_3 > -1 Then GUICtrlSetBkColor($g_Btn_Lime_3, $g_iBackColor_Old_Btn_3) If $g_iBackColor_Old_Lbl_3 > -1 Then GUICtrlSetBkColor($g_Lbl_Yellow_3, $g_iBackColor_Old_Lbl_3) DebugPrintFor_ResetColor(3) ; ---- Column 4 (Bilgus) ---- ; Unfortunately the function does NOT work with transparent controls, e.g. labels. Case $g_BtnSetColr_4 $g_iBackColor_Old_Btn_4 = GUICtrlGetBkColor_Bilgus($g_Btn_Lime_4) ; Lime $g_iBackColor_Old_Lbl_4 = GUICtrlGetBkColor_Bilgus($g_Lbl_Yellow_4) ; Yellow GUICtrlSetBkColor($g_Btn_Lime_4, 0xd7edff) ; light blue GUICtrlSetBkColor($g_Lbl_Yellow_4, 0xd7edff) ; light blue DebugPrintFor_SetColor(4) Case $g_BtnResetColr_4 If $g_iBackColor_Old_Btn_4 > -1 Then GUICtrlSetBkColor($g_Btn_Lime_4, $g_iBackColor_Old_Btn_4) If $g_iBackColor_Old_Lbl_4 > -1 Then GUICtrlSetBkColor($g_Lbl_Yellow_4, $g_iBackColor_Old_Lbl_4) DebugPrintFor_ResetColor(4) EndSwitch WEnd EndFunc ; ==> Example ; ============================================================================== ; GUICtrlGetBkColor (WinAPI 1) ; ; Description: Retrieves the RGB value of the control background WITHOUT GetPixel. ; ---- But it does NOT work! ---- ; ; Author: Professor Bernd, 2020-08-22.. ; ------------------------------------------------------------------------------ Func GUICtrlGetBkColor_WinAPI_1($hWnd) If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) Local $hDC = _WinAPI_GetDC($hWnd) ; ---- Why does it not work WITHOUT _SendMessage() ---- <== Local $iColor = _WinAPI_GetBkColor($hDC) _WinAPI_ReleaseDC($hWnd, $hDC) Return $iColor EndFunc ; ==> GUICtrlGetBkColor (WinAPI 1) ; ============================================================================== ; GUICtrlGetBkColor (WinAPI 2) ; ; Description: Retrieves the RGB value of the control background WITHOUT GetPixel. ; ; Author: Bilgus, 2020-04-30. ; https://www.autoitscript.com/forum/topic/202574-get-color-of-a-solid-brush/?do=findComment&comment=1453833 ; Modified by: Professor Bernd, 2020-08-22. ; ------------------------------------------------------------------------------ Func GUICtrlGetBkColor_WinAPI_2($hWnd) If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) Local $hDC = _WinAPI_GetDC($hWnd) ; <== Get the DC before you send the message! _SendMessage(_WinAPI_GetParent($hWnd), $WM_CTLCOLORSTATIC, $hDC, $hWnd) Local $iColor = _WinAPI_GetBkColor($hDC) _WinAPI_ReleaseDC($hWnd, $hDC) Return $iColor EndFunc ; ==> GUICtrlGetBkColor (WinAPI 2) ; #FUNCTION# ==================================================================================================================== ; Name ..........: GUICtrlGetBkColor (guinness) ; Description ...: Retrieves the RGB value of the control background WITH GetPixel. ; Syntax ........: GUICtrlGetBkColor($hWnd) ; Parameters ....: $hWnd - Control ID/Handle to the control ; Return values .: Success - RGB value ; Failure - 0 ; Author ........: guinness, 2011-02-20. ; Example .......: Yes ; https://www.autoitscript.com/forum/topic/125684-guictrlgetbkcolor-get-the-background-color-of-a-control/ ; =============================================================================================================================== Func GUICtrlGetBkColor_guinness($hWnd) If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) Local $hDC = _WinAPI_GetDC($hWnd) Local $iColor = _WinAPI_GetPixel($hDC, 0, 0) _WinAPI_ReleaseDC($hWnd, $hDC) Return $iColor EndFunc ;==>GUICtrlGetBkColor ; ============================================================================== ; GUICtrlGetBkColor (Bilgus) ; ; Description: Retrieves the RGB value of the control background WITHOUT GetPixel. ; ---- Does NOT work e.g. with transparent labels. ---- ; ; Author: Bilgus, 2020-04-30. ; https://www.autoitscript.com/forum/topic/202574-get-color-of-a-solid-brush/?do=findComment&comment=1453833 ; Modified by: Professor Bernd, 2020-08-22. ; ------------------------------------------------------------------------------ Func GUICtrlGetBkColor_Bilgus($hWnd) If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) Local $hWnd_Main = _WinAPI_GetParent($hWnd) Local $hDC = _WinAPI_GetDC($hWnd) Local $hBrush = _SendMessage($hWnd_Main, $WM_CTLCOLORSTATIC, $hDC, $hWnd) Local $iColor = _GetBrushColor($hBrush) _WinAPI_ReleaseDC($hWnd, $hDC) Return $iColor EndFunc ; ==> GUICtrlGetBkColor (Bilgus) ; ; ============================================================================== ; _GetBrushColor (Bilgus) ; ; Description: Get the color of a SOLID brush. Does NOT work e.g. with transparent labels. ; ; Author: Bilgus, 2020-04-30. ; https://www.autoitscript.com/forum/topic/202574-get-color-of-a-solid-brush/ ; ------------------------------------------------------------------------------ Func _GetBrushColor($hBrush) ;https://devblogs.microsoft.com/oldnewthing/20190802-00/?p=102747 Local $iErr Local Const $BS_SOLID = 0x00000000 Local Const $tagLogBrush = "uint lbStyle; dword lbColor; ulong_ptr lbHatch" Local $tLogBrush = DllStructCreate($tagLogBrush) Local $iSzLogBrush = DllStructGetSize($tLogBrush) If _WinAPI_GetObject($hBrush, $iSzLogBrush, DllStructGetPtr($tLogBrush)) <> $iSzLogBrush Then $iErr = 0x1 ElseIf DllStructGetData($tLogBrush, "lbStyle") <> $BS_SOLID Then $iErr = 0x2 Else Return DllStructGetData($tLogBrush, "lbColor") EndIf Return SetError($iErr, 0, 0xFFFFFFFF) ;CLR_NONE EndFunc ; ==> _GetBrushColor ; ---- Helper functions -------------------------------------------------------- ; Write to the console for the buttons that will set the new color. Func DebugPrintFor_SetColor($_iColmNum) ConsoleWrite(" $g_iBackColor_Old_Btn_" & $_iColmNum & ": " & Execute("$g_iBackColor_Old_Btn_" & $_iColmNum) _ & " _WinAPI_GetBkMode($g_Btn_Lime_" & $_iColmNum & "): " _ & _WinAPI_GetBkMode(_WinAPI_GetDC(WinGetHandle(Execute("$g_Btn_Lime_" & $_iColmNum))))& @CRLF) ConsoleWrite(" $g_iBackColor_Old_Lbl_" & $_iColmNum & ": " & Execute("$g_iBackColor_Old_Lbl_" & $_iColmNum) _ & " _WinAPI_GetBkMode($g_Lbl_Yellow_" & $_iColmNum & "): " _ & _WinAPI_GetBkMode(_WinAPI_GetDC(WinGetHandle(Execute("$g_Lbl_Yellow_" & $_iColmNum)))) & @CRLF) EndFunc ; ==> DebugPrintFor_SetColor ; Write to the console for the buttons that reset the color to the old value. Func DebugPrintFor_ResetColor($_iColmNum) ConsoleWrite(" $g_iBackColor_Old_Btn_" & $_iColmNum & ": " & Execute("$g_iBackColor_Old_Btn_" & $_iColmNum) & @CRLF) ConsoleWrite(" $g_iBackColor_Old_Lbl_" & $_iColmNum & ": " & Execute("$g_iBackColor_Old_Lbl_" & $_iColmNum) & @CRLF) EndFunc ; ==> DebugPrintFor_ResetColor