Jump to content

_WinAPI_GetBkColor() vs $CLR_DEFAULT


Recommended Posts

Hi everybody :)
I discovered 2 different ways for reverting to a background color. Here is a script that will show these 2 ways :

#include <ColorConstants.au3>
#include <GuiButton.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>

Global $aButton_Caption[5] = _
    ["Click me : Label will get a green background", _
     "Click me : Reverting background color (_WinAPI_GetBkColor)", _
     "Click me : Label will get a green background... again", _
     "Click me : Reverting background color ($CLR_DEFAULT)", _

$hGUI = GUICreate("2 ways to revert back color ?", 324, 134, -1, -1, _
    -1, $WS_EX_TOPMOST)
$idButton = GUICtrlCreateButton($aButton_Caption[0], _
    8, 91, 307, 25)
$idLabel = GUICtrlCreateLabel("Just a test label placed here", _
    93, 40, 136, 17, $SS_CENTER)


; ++++++++++++
; Keep the GUI background color in a variable.
; Gui MUST be visible (not hidden or minimized) for this function to work properly
$hDC = _WinAPI_GetDC($hGUI)
$iGuiBackColor = _WinAPI_GetBkColor($hDC)
MsgBox($MB_TOPMOST, "RGB Gui background color", _
    $iGuiBackColor & " (Hex " & Hex($iGuiBackColor) & ")")
_WinAPI_ReleaseDC($hGUI, $hDC)
; ++++++++++++

$iCount = 0
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE

        Case $idButton
            $iCount += 1
            Switch $iCount
                Case 1
                    GUICtrlSetBkColor($idLabel, 0xDDFFDD) ; light green
                Case 2
                    GUICtrlSetBkColor($idLabel, $iGuiBackColor) ; 0xE0DFE3 grey on my PC
                Case 3
                    GUICtrlSetBkColor($idLabel, 0xDDFFDD) ; light green... again
                Case 4
                    GUICtrlSetBkColor($idLabel, $CLR_DEFAULT) ; 0xFF000000
                Case 5
            _GUICtrlButton_SetText($idButton, $aButton_Caption[$iCount])


Until a couple of days, I used theĀ  _WinAPI_GetBkColor() way, based on one of Guinness script.
Then I found recently that using $CLR_DEFAULT could do exactly the same, as you can notice in the preceding script, also in this script, where $CLR_DEFAULT is used with good results during the WM_NOTIFY function.

So the question is : can we simply rely only on $CLR_DEFAULT and forget the _WinAPI_GetBkColor() way, which requires a bit more code and include files ?

In case you launch the preceding script, you probably won't get the same $iGuiBackColor as mine, which is 0xE0DFE3 meaning a grey background. Depending on your windows background color, you'll get another value in MsgBox but it doesn't matter, as long as your background color is correctly reverted twice.

Edited by pixelsearch
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...