Jump to content

Get GUI Background color?


Recommended Posts

I have searched the Help File

I have searched the Forums

I have searched the Color.au3 and functions

I can't seem to find a function to return the current GUI background color of a GUI I created.

PixelGetColor() wont work 80% of the time for me.

thx

Valuater

8)

Oh... and I searched Autoit Wrappers too! ...lol

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Hi, I'm probly way of track..

_WinAPI_SetBkColor() returns the previous color..

Can you Lock the gui from painting and _WinAPI_SetBkColor() to get the returned color?

I've not tried it and I'm only stabbing in the dark.

Cheers

Edit: yep it works, and no need to lock the gui from painting.

But don't forget the return from _WinAPI_SetBkColor() is BGR and not RGB.

Edited by smashly
Link to comment
Share on other sites

Hi, I'm probly way of track..

_WinAPI_SetBkColor() returns the previous color..

Can you Lock the gui from painting and _WinAPI_SetBkColor() to get the returned color?

I've not tried it and I'm only stabbing in the dark.

Cheers

Thats a thought, I will give that a go if nothin better turns up!

Thanks for the idea

8)

NEWHeader1.png

Link to comment
Share on other sites

#include <WinAPI.au3>

$hGUI = GUICreate("")
GUISetBkColor(0xFF0000, $hGUI)
GUISetState(@SW_SHOW, $hGUI)

$hDC = _WinAPI_GetDC($hGUI)
$BkClr = _WinAPI_SetBkColor($hDC, 0)

; Crude trans from BGR to RGB
$BkClr = StringRight(Hex($BkClr, 6), 2) & StringLeft(StringRight(Hex($BkClr, 6), 4), 2) & StringLeft(Hex($BkClr, 6), 2)

ConsoleWrite("Previous RGB Color: 0x" & $BkClr & @LF)

Do
Until GUIGetMsg() = -3

Edit: It would be handier if GUISetBkColor() returned the previous color instead of 1 or 0.

Edited by smashly
Link to comment
Share on other sites

With a little help from my friend,,, Autoit Wrappers too

; Switch BGR to RGB and vice versa
; Author - RazerM

ConsoleWrite(0xFF0000 = SwitchColor(0x0000FF))
ConsoleWrite(@CRLF)

Func SwitchColor ($iColor)
    Local $iMask
    $iMask = BitXOR(BitAND($iColor, 0xFF) , ($iColor / 0x10000))
    Return BitXOR($iColor, ($iMask * 0x10001))
EndFunc   ;==>SwitchColor

Thanks again... :)

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

With a little help from my friend,,, Autoit Wrappers too

; Switch BGR to RGB and vice versa
; Author - RazerM

ConsoleWrite(0xFF0000 = SwitchColor(0x0000FF))
ConsoleWrite(@CRLF)

Func SwitchColor ($iColor)
    Local $iMask
    $iMask = BitXOR(BitAND($iColor, 0xFF) , ($iColor / 0x10000))
    Return BitXOR($iColor, ($iMask * 0x10001))
EndFunc   ;==>SwitchColor

Thanks again... :)

8)

That SwitchColor() function should go into the standard UDF's. color.au3 is a bit short on functions anyway. There are a few other color functions drifting around too. I think that I will create a new Colorx. au3 file and upload it to my site but not tonight.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Hi, I'm probly way of track..

_WinAPI_SetBkColor() returns the previous color..

Can you Lock the gui from painting and _WinAPI_SetBkColor() to get the returned color?

I've not tried it and I'm only stabbing in the dark.

Cheers

Edit: yep it works, and no need to lock the gui from painting.

But don't forget the return from _WinAPI_SetBkColor() is BGR and not RGB.

The same DLL that is used for _WinAPI_SetBkColor() also has a "GetBkColor" function. Note that it returns the color in 24bit BGR (not RGB) format, so I included a translator. I don't know why it's not already included in WinAPI.au3.
#include <GuiConstants.au3>
#include <WinAPI.au3>

Global $avColors[3] = [0xFF0000, 0xFF00, 0xFF], $iColor = 0
Global $hGUI = GUICreate("Test", 500, 200)
GUISetBkColor($avColors[$iColor])
Global $ctrlButton1 = GUICtrlCreateButton("GetBkColor", 200, 100, 100, 30)
Global $ctrlButton2 = GUICtrlCreateButton("SetBkColor", 200, 150, 100, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ctrlButton1
            $hGuiDC = _WinAPI_GetDC($hGUI)
            $iGetColor = _WinAPI_GetBkColor($hGuiDC)
            _WinAPI_ReleaseDC($hGUI, $hGuiDC)
            WinSetTitle($hGUI, "", "GetBkColor (BGR format) = 0x" & Hex($iGetColor, 6) & _
                    " (RGB) = 0x" & Hex(_RGB2BGR($iGetColor), 6))
        Case $ctrlButton2
            $iColor += 1
            If $iColor > UBound($avColors) - 1 Then $iColor = 0
            GUISetBkColor($avColors[$iColor])
            WinSetTitle($hGUI, "", "SetBkColor (BGR format) = 0x" & Hex(_RGB2BGR($avColors[$iColor]), 6) & _
                    "  (RGB) = 0x" & Hex($avColors[$iColor], 6))
    EndSwitch
WEnd


; #FUNCTION# ==================================================
; Name...........: _RGB2BGR
; Description ...: Converts RGB to BGR color format, and vice versa
; Syntax.........: _RGB2BGR($iColor)
; Parameters ....: $iColor    - 24 bit color to convert
; Return values .: Success    - The converted color
;                 Failure     - -1
; Author ........: PsaltyDS
; ===========================================================
Func _RGB2BGR($iColor)
    If IsInt($iColor) And ($iColor < 2 ^ 24) Then
        Local $iX = BitShift(BitAND($iColor, 0xFF0000), 16)
        Local $iY = BitAND($iColor, 0xFF00)
        Local $iZ = BitShift(BitAND($iColor, 0xFF), -16)
        Return BitOR($iX, $iY, $iZ)
    Else
        Return -1
    EndIf
EndFunc ;==>_RGB2BGR


; #FUNCTION# ==================================================
; Name...........: _WinAPI_GetBkColor
; Description ...: Gets the current background color
; Syntax.........: _WinAPI_GetBkColor($hDC)
; Parameters ....: $hDC      - Handle to the device context
; Return values .: Success    - The background color
;                 Failure     - -1
; Author ........: PsaltyDS, based on Paul Campbell's (PaulIA) _WinAPI_SetBkColor
; Remarks .......:  Colors are returned in BGR fomat, not RGB!
; Link ..........; @@MsdnLink@@ SetBkColor
; ===========================================================
Func _WinAPI_GetBkColor($hDC)
    Local $aResult

    $aResult = DllCall("GDI32.dll", "int", "GetBkColor", "hwnd", $hDC)
    Return $aResult[0]
EndFunc ;==>_WinAPI_GetBkColor

:)

Edit: Wow, you guys were beating the RGB2BGR thing to death already while I was testing this... :)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Nice work PsaltyDS.

The only thing I could suggest is to have the _WinAPI_GetBkColor() and _RGB2BGR() as 1 function...

Then offer an extra param for the _WinAPI_GetBkColor() for the type of return BGR or RGB.

Edit: the other thing would be to get & release the DC from within the function. So a user only needs to pass a hwnd to the function.

Cheers

Edited by smashly
Link to comment
Share on other sites

Nice work PsaltyDS.

The only thing I could suggest is to have the _WinAPI_GetBkColor() and _RGB2BGR() as 1 function...

Then offer an extra param for the _WinAPI_GetBkColor() for the type of return BGR or RGB.

Cheers

Good Idea... I'd like to see that too!

Thats actually what I did without using the GetBkColor call

8)

NEWHeader1.png

Link to comment
Share on other sites

Nice work PsaltyDS.

The only thing I could suggest is to have the _WinAPI_GetBkColor() and _RGB2BGR() as 1 function...

Then offer an extra param for the _WinAPI_GetBkColor() for the type of return BGR or RGB.

Edit: the other thing would be to get & release the DC from within the function. So a user only needs to pass a hwnd to the function.

Cheers

I didn't do that for one specific reason -- the other _WinAPI_* functions don't do it. It would make for a jarring inconsistency.

Turning the issue on it's head, I only used GuiSetBkColor() in the demo because I couldn't get _WinAPI_SetBkColor() change the GUI on the screen. What am I missing here about using _WinAPI_SetBkColor()?

#include <GuiConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $avColors[3] = [0xFF0000, 0xFF00, 0xFF], $iColor = 0
Global $hGUI = GUICreate("Test", 500, 200)
GUISetBkColor($avColors[$iColor])
Global $ctrlButton1 = GUICtrlCreateButton("GetBkColor", 200, 100, 100, 30)
Global $ctrlButton2 = GUICtrlCreateButton("SetBkColor", 200, 150, 100, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ctrlButton1
            $hGuiDC = _WinAPI_GetDC($hGUI)
            $iGetColor = _WinAPI_GetBkColor($hGuiDC)
            _WinAPI_ReleaseDC($hGUI, $hGuiDC)
            WinSetTitle($hGUI, "", "GetBkColor (BGR format) = 0x" & Hex($iGetColor, 6) & _
                    " (RGB) = 0x" & Hex(_RGB2BGR($iGetColor), 6))
        Case $ctrlButton2
            $iColor += 1
            If $iColor > UBound($avColors) - 1 Then $iColor = 0
            $hGuiDC = _WinAPI_GetDC($hGUI)
            $iGetColor = _WinAPI_SetBkColor($hGuiDC, _RGB2BGR($avColors[$iColor]))
            _WinAPI_ReleaseDC($hGUI, $hGuiDC)
            _WinAPI_RedrawWindow($hGUI, 0, 0, BitOR($RDW_ERASE, $RDW_INVALIDATE))
            WinSetTitle($hGUI, "", "SetBkColor (BGR format) = 0x" & Hex(_RGB2BGR($avColors[$iColor]), 6) & _
                    "  (RGB) = 0x" & Hex($avColors[$iColor], 6))
    EndSwitch
WEnd


; #FUNCTION# ==================================================
; Name...........: _RGB2BGR
; Description ...: Converts RGB to BGR color format, and vice versa
; Syntax.........: _RGB2BGR($iColor)
; Parameters ....: $iColor    - 24 bit color to convert
; Return values .: Success    - The converted color
;                 Failure     - -1
; Author ........: PsaltyDS
; ===========================================================
Func _RGB2BGR($iColor)
    If IsInt($iColor) And ($iColor < 2 ^ 24) Then
        Local $iX = BitShift(BitAND($iColor, 0xFF0000), 16)
        Local $iY = BitAND($iColor, 0xFF00)
        Local $iZ = BitShift(BitAND($iColor, 0xFF), -16)
        Return BitOR($iX, $iY, $iZ)
    Else
        Return -1
    EndIf
EndFunc  ;==>_RGB2BGR


; #FUNCTION# ==================================================
; Name...........: _WinAPI_GetBkColor
; Description ...: Gets the current background color
; Syntax.........: _WinAPI_GetBkColor($hDC)
; Parameters ....: $hDC      - Handle to the device context
; Return values .: Success    - The background color
;                 Failure     - -1
; Author ........: PsaltyDS, based on Paul Campbell's (PaulIA) _WinAPI_SetBkColor
; Remarks .......:  Colors are returned in BGR fomat, not RGB!
; Link ..........; @@MsdnLink@@ SetBkColor
; ===========================================================
Func _WinAPI_GetBkColor($hDC)
    Local $aResult

    $aResult = DllCall("GDI32.dll", "int", "GetBkColor", "hwnd", $hDC)
    Return $aResult[0]
EndFunc  ;==>_WinAPI_GetBkColor

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Not sure....

Windows GDI SetBkColor The SetBkColor function sets the current background color to the specified color value, or to the nearest physical color if the device cannot represent the specified color value.

COLORREF SetBkColor(

HDC hdc, // handle to DC

COLORREF crColor // background color value

);

Parameters

hdc [in] Handle to the device context. crColor [in] Specifies the new background color. To make a COLORREF value, use the RGB macro.

Return Values

If the function succeeds, the return value specifies the previous background color as a COLORREF value.

If the function fails, the return value is CLR_INVALID.

Windows NT/2000/XP: To get extended error information, call GetLastError.

**********************************************************

Windows GDI COLORREF The COLORREF value is used to specify an RGB color.

typedef DWORD COLORREF;

typedef DWORD *LPCOLORREF;

Remarks

When specifying an explicit RGB color, the COLORREF value has the following hexadecimal form:

0x00bbggrr The low-order byte contains a value for the relative intensity of red; the second byte contains a value for green; and the third byte contains a value for blue. The high-order byte must be zero. The maximum value for a single byte is 0xFF.

To create a COLORREF color value, use the RGB macro. To extract the individual values for the red, green, and blue components of a color value, use the GetRValue, GetGValue, and GetBValue macros, respectively.

******************************************************************

Windows GDI RGB The RGB macro selects a red, green, blue (RGB) color based on the arguments supplied and the color capabilities of the output device.

COLORREF RGB(

BYTE byRed, // red component of color

BYTE byGreen, // green component of color

BYTE byBlue // blue component of color

);

Parameters

byRed Specifies the intensity of the red color. byGreen Specifies the intensity of the green color. byBlue Specifies the intensity of the blue color.

Return Values

The return value is the resultant RGB color as a <a id="ctl00_rs1_mainContentContainer_ctl02" onclick="java script:Track('ctl00_rs1_mainContentContainer_ctl00|ctl00_rs1_mainContentContainer_ctl02',this);" href="http://msdn.microsoft.com/en-us/library/ms532655%28VS.85%29.aspx">COLORREF value.

Remarks

The intensity for each argument is in the range 0 through 255. If all three intensities are zero, the result is black. If all three intensities are 255, the result is white.

To extract the individual values for the red, green, and blue components of a COLORREF color value, use the GetRValue, GetGValue, and GetBValue macros, respectively.

*********************************************************************

ITS ALL GREEK TO ME.....LOL

8)

NEWHeader1.png

Link to comment
Share on other sites

Tried _WinAPI_UpdateWindow($hGUI) in place of _WinAPI_RedrawWindow(), and also with _WinAPI_InvalidateRect($hGUI, "", True) before that... no joy.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 15 years later...
Posted (edited)
On 5/30/2008 at 6:00 AM, Valuater said:

With a little help from my friend,,, Autoit Wrappers too

; Switch BGR to RGB and vice versa
; Author - RazerM

ConsoleWrite(0xFF0000 = SwitchColor(0x0000FF))
ConsoleWrite(@CRLF)

Func SwitchColor ($iColor)
    Local $iMask
    $iMask = BitXOR(BitAND($iColor, 0xFF) , ($iColor / 0x10000))
    Return BitXOR($iColor, ($iMask * 0x10001))
EndFunc   ;==>SwitchColor

 

I know it's ancient, but this thread is still useful, except this specific function seems to work for me only on black color, so I suggest this instead:

$oppositeColor = BitXOR($color, , 0xFFFFFF)

You can combine it with detecting colors like this:

GUISetBkColor(BitXOR(_GUIGetColor($hwnd), 0xFFFFFF))
GUICtrlSetDefColor(BitXOR(_GUIGetColor($hwnd, false), 0xFFFFFF))
; GUICtrlSetDefBkColor(BitXOR(..., 0xFFFFFF)) ; Couldn't find a way to discover this color automatically, but at least in my case it's 0xE1E1E1

Func _GUIGetColor($hWnd, $background = true)
    Local $hDC = _WinAPI_GetDC($hWnd), $iColor
    if $background then
        $iColor = _WinAPI_GetBkColor($hDC)
    else
        $iColor = _WinAPI_GetTextColor($hDC)
    EndIf
    _WinAPI_ReleaseDC($hWnd, $hDC)
    Return $iColor
EndFunc

 

Edited by LWC
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
 Share

  • Recently Browsing   0 members

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