Jump to content

Working example of GetTextColor()/GetBkColor()


rover
 Share

Recommended Posts

Get text and background colour from standard controls

How to retrieve the data you already have...

Not a very useful thing, as it only works for in-process standard controls, but there you go.

This example is based on code by Prog@ndy and Malkey in this thread:

GUICtrlGetColor()

?do=embed' frameborder='0' data-embedContent>

Standard Controls do not store colour in their Device Context. (CS_OWNDC)

Using GetTextColor() on the controls DC returns 0xFFFFFF, (COLOR_WINDOW)

the default value in the WNDCLASS structure. The parent window sets the colour (CS_PARENTDC)

By sending a WM_CTLCOLORSTATIC message to the controls parent window.

we can have it write to a temporary memory DC and then use GetTextColor/GetBkColor.

The four functions return the RGB colours of visible, hidden or disabled Standard Controls

on active, inactive, hidden, disabled, locked or minimized AutoIt GUI forms.

The following form/control classes have been tested in XP/VISTA x86:

AutoIt v3 GUI, Static (Label), Edit(Edit/Input), Button (Ownerdrawn/Classic Theme PushButton**, Group, Radio, CheckBox), ListBox, ComboBox

_GuiCtrlGetTextColorEx() - get RGB text colour of visible/hidden/disabled in-process* controls.

on an active, inactive, hidden, locked, disabled or minimized gui.

_GuiCtrlGetBkColorEx() - get RGB background colour of visible/hidden/disabled in-process* controls.

on an active, inactive, hidden, locked, disabled or minimized gui.

_GUIGetBkColorEx() - get RGB background colour of active, inactive, hidden, locked, disabled or

minimized in-process* forms. - the only feature this adds over existing forum examples is getting colour from a minimized gui

_GuiCtrlGetColorEx() - all-in-one testing version with ownerdrawn/themed button testing

Use the separate functions _GUICtrlGetTextColorEx() and_GUICtrlGetBkColorEx() instead of this

*This UDF only supports controls in the current script process. It does not work with external process controls.

Nor does it support controls that are painted on (colour not set by WM_CTLCOLORSTATIC message)

themed controls or Common Controls (they have their own methods to retrieve colour)

Does not return transparent layered window attributes, use _WinAPI_GetLayeredWindowAttributes()

Disabled and/or hidden controls will still return their enabled colours,

the WM_CTLCOLORSTATIC message does not return the system colours for disabled controls.

**NOTE: ownerdrawn buttons with no set background color default to the COLOR_BTNFACE system colour,

but the returned background mode is OPAQUE and the colour is the underlying colour of the parent gui,

so GetPixel() is required to get the actual colour.

There will probably be some conditions or OS versions where this code will return incorrect or no colour.

Other get control colour examples:

Get GUI Background color? is there such a thing??

GUICtrlGetColor()

?do=embed' frameborder='0' data-embedContent>

GUICtrlGetBkColor() - Get the background color of a control

Get colour get label colour

Example:

Get text, background colour and background mode from four standard controls (visible/hidden/disabled)

while looping through the following gui states: active, inactive, hidden, minimized, locked and disabled.

;#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include "_GetCtrlColorEx.au3"

#include <GUIConstantsEx.au3>
#include <Constants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

Opt('MustDeclareVars', 1)

Global $aColour, $aStr[4] = ["Edit", "Static 1", "Static 2", "Button "]
Global $hGui = GUICreate('Get Control Colours', 230, 200)
GUISetBkColor(0xABCDEF)
Global $cFst = GUICtrlCreateInput('Edit', 20, 10, 160, 20);, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFAFAFF)
GUICtrlCreateLabel("Static 1", 20, 45, 160, 20, BitOR($SS_CENTERIMAGE, $SS_CENTER))
GUICtrlSetColor(-1, 0x191970)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlCreateLabel("Static 2", 20, 80, 160, 20)
GUICtrlSetColor(-1, 0xFFE9A9)
GUICtrlSetBkColor(-1, 0x6E7B80)
GUICtrlCreateButton("Button", 20, 115, 160, 25)
;Ownerdrawn
GUICtrlSetColor(-1, 0xFAFAFF)
GUICtrlSetBkColor(-1, 0x494949)
;classic theme
;DllCall("uxtheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle(-1), "wstr", "", "wstr", "")

GUISetState()

For $i = 0 To 6
    Switch $i
        Case 0
            ConsoleWrite("+ ACTIVE ====================" & @CRLF)
        Case 1
            Sleep(1000)
            ConsoleWrite("+ INACTIVE ==================" & @CRLF)
            _WinAPI_SetWindowPos($hGui, $HWND_BOTTOM, 0, 0, 0, 0, BitOR($SWP_SHOWWINDOW, $SWP_NOSIZE, $SWP_NOMOVE))
        Case 2
            Sleep(1000)
            _WinAPI_SetWindowPos($hGui, $HWND_TOP, 0, 0, 0, 0, BitOR($SWP_SHOWWINDOW, $SWP_NOSIZE, $SWP_NOMOVE))
            Sleep(1000)
            ConsoleWrite("+ HIDDEN ====================" & @CRLF)
            GUISetState(@SW_HIDE)
        Case 3
            Sleep(1000)
            GUISetState(@SW_SHOW)
            Sleep(1000)
            ConsoleWrite("+ MINIMIZED ==================" & @CRLF)
            GUISetState(@SW_MINIMIZE)
        Case 4
            Sleep(1000)
            GUISetState(@SW_RESTORE)
            ConsoleWrite("+ LOCKED ======================" & @CRLF)
            GUISetState(@SW_LOCK)
        Case 5
            Sleep(1000)
            GUISetState(@SW_UNLOCK)
            ConsoleWrite("+ DISABLED ====================" & @CRLF)
            GUISetState(@SW_DISABLE)
            Sleep(1000)
        Case 6
            GUISetState(@SW_ENABLE)
            ConsoleWrite("+ CONTROLS HIDDEN/DISABLED =====" & @CRLF)
            For $j = $cFst To ($cFst+3)
                GUICtrlSetState($j, $GUI_DISABLE)
                GUICtrlSetState($j, $GUI_HIDE)
            Next
            Sleep(1000)
    EndSwitch

    For $k = $cFst To ($cFst+3)
        $aColour = _GUICtrlGetColorEx($k, 1)
        ConsoleWrite("+ "&$aStr[$k-$cFst]&" Text  Colour: " & $aColour[0][0] & @CRLF)
        ConsoleWrite("+ "&$aStr[$k-$cFst]&" Text  Colour: " & _GUICtrlGetTextColorEx($k, 1) & @CRLF)
        ConsoleWrite("- "&$aStr[$k-$cFst]&" BkGnd Colour: " & $aColour[1][0] & " - " & $aColour[1][1] & @CRLF)
        ConsoleWrite("- "&$aStr[$k-$cFst]&" BkGnd Colour: " & _GUICtrlGetBkColorEx($k, 1) & " - Mode: " & @extended & @CRLF & @CRLF)
    Next

    ConsoleWrite("> GUI BkGnd Colour: " & _GUIGetBkColorEx($hGui, 1) & @CRLF & @CRLF)
Next

For $j = $cFst To ($cFst+3)
    GUICtrlSetState($j, $GUI_ENABLE)
    GUICtrlSetState($j, $GUI_SHOW)
Next

Do
Until GUIGetMsg() = -3
Exit

_GetCtrlColorEx.au3

I see fascists...

Link to comment
Share on other sites

Nice :)

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 8 years later...

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...