Sign in to follow this  
Followers 0

GUICtrlGetBkColor() - Get the background color of a control.

19 posts in this topic

Posted (edited)

If you set the Label background colour with GUICtrlSetBkColor() and happen to forget the colour you set it as, then why not try GUICtrlGetBkColor()

Function:

#include-once
#include <WinAPIGdi.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: GUICtrlGetBkColor
; Description ...: Retrieves the RGB value of the control background.
; Syntax ........: GUICtrlGetBkColor($hWnd)
; Parameters ....: $hWnd                - Control ID/Handle to the control
; Return values .: Success - RGB value
;                  Failure - 0
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================

Func GUICtrlGetBkColor($hWnd)
    If Not IsHWnd($hWnd) Then
        $hWnd = GUICtrlGetHandle($hWnd)
    EndIf
    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $iColor = _WinAPI_GetPixel($hDC, 0, 0)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    Return $iColor
EndFunc   ;==>GUICtrlGetBkColor
Example use of Function:

#include <MsgBoxConstants.au3>

#include 'GUICtrlGetBkColor.au3'

Example()

Func Example()
    Local $hGUI = GUICreate('GUICtrlGetBkColor() Example', 500, 350)
    Local $iLabel = GUICtrlCreateLabel('', 10, 10, 480, 330)
    GUISetState(@SW_SHOW, $hGUI)

    Local $aColor = [0x0000FF, 0x8FFF9F, 0xEC4841, 0xB0E35D, 0x440BFD] ; Random colour array.
    Local $iColor = 0
    For $i = 0 To UBound($aColor) - 1
        GUICtrlSetBkColor($iLabel, $aColor[$i])
        Sleep(20)
        $iColor = GUICtrlGetBkColor($iLabel) ; Pass the controldid to the function.
        MsgBox($MB_SYSTEMMODAL, '', 'Background Color: ' & _ConvertToHexFormat($aColor[$i]) & @CRLF & _
                'GUICtrlGetBkColor() Hex Format: ' & _ConvertToHexFormat($iColor) & @CRLF & _
                'GUICtrlGetBkColor() Returned: ' & $iColor, 0, $hGUI)
    Next

    GUIDelete($hGUI)
EndFunc   ;==>Example

Func _ConvertToHexFormat($iColor)
    Return Hex($iColor, 6)
EndFunc   ;==>_ConvertToHexFormat
Additional thanks to Yashied for pointing out the obvious in this >forum message about _WinAPI_GetPixel() and the hint about returning a RGB number :) Edited by guinness

Share this post


Link to post
Share on other sites



Posted

you are awesome!

Share this post


Link to post
Share on other sites

Posted

OK, well I'm flattered! But some of that flattery should go to Yashied as well :huh2:

Share this post


Link to post
Share on other sites

Posted

True, btw this function needs to be added to autoit by default

Share this post


Link to post
Share on other sites

Posted

I think it's OK in the Example's Section.

Share this post


Link to post
Share on other sites

Posted

don't be fake-modest guiness :huh2:

Share this post


Link to post
Share on other sites

Posted

Thank you!

It helped to me

Share this post


Link to post
Share on other sites

Posted

It is great, but some small problems

If both of the controls set the color and background color

Then the function returns GUICtrlSetColor() not GUICtrlSetBkColor()

At this point can be modified:

$bGetBkColor = _WinAPI_GetPixel($hDC, 0, 0) ------------------> $bGetBkColor = _WinAPI_GetPixel($hDC, 1, 1)

Share this post


Link to post
Share on other sites

Posted

But this is probably a rare occurrence?

Share this post


Link to post
Share on other sites

Posted (edited)

I've updated the Examples and Code in & Any comments or suggestions then please post below or in the respective threads. Thanks.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

I've updated the example and function syntax. See above for more details. Thanks.

Share this post


Link to post
Share on other sites

Posted

Hi guinness!

I have a litte problem with your function:

If the control is on a tabpage and is NOT visible the func will return wrong colours. (What is clear because the control is on a differen tab and not visible)

Do you have an idea how to fix this? (Without to change the tabpages)

Thanks in advance! :)

Share this post


Link to post
Share on other sites

Posted

It appears that the window and control have to be visible to be detected by this function. If you change the GUISetState command in the example to "GUISetState(@SW_SHOWMINIMIZED , $hGUI)" instead of using @SW_SHOW it won't detect the colors either.

Share this post


Link to post
Share on other sites

Posted

I can't think of a workaround at the moment. Sorry.

Share this post


Link to post
Share on other sites

Posted

I believe it's a limitation of the "GetPixel" function of the gdi32.dll.

Share this post


Link to post
Share on other sites

Posted

It would appear so.

Share this post


Link to post
Share on other sites

Posted (edited)

Hey,

I'm trying to use the function above (GuiCtrlGetBKColor) but found a problem:

The GuiCtrlGetBKColor function needs only the handle to the control as input. Doesn't it need also a handle to the window which the control is located on? How the function knows to go to a specific window and there access to the handle of the subject control?

What I'm doing is editing the example and activating it on a known existing window. So I don't need the color array or the creation of a gui. Just need to know how to tell the function to address a specific window. From there' I'm sure, the function will do it's job

Thanks,

Edited by shahino007

Share this post


Link to post
Share on other sites

Posted

Download the latest AutoIt beta to find the latest WinAPIEx.au3. The functions works for on my Windows 8.1 x64, so I have no clue what the problem is for you.

PS I updated the example to remove magic numbers!

Share this post


Link to post
Share on other sites

Posted

Doesn't hurt to have a spring clean of your code once in a while. So this is what I just did. Tidied the syntax and variable naming.

Share this post


Link to post
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
Sign in to follow this  
Followers 0