AndyS01 Posted August 20, 2018 Posted August 20, 2018 I created an Edit box control and set the control's Foreground (text) and Background colors, and when I click on a button, I want to detect those values. However, I can only detect the control's Background color. How can I detect the control's Foreground color? Here is my test code: expandcollapse popup#include <Debug.au3> _DebugSetup(@ScriptName & "_debug.txt", False, 2) _DebugOut("=============== " & @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " ====================") #include <EditConstants.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <ColorConstants.au3> #include <WinAPI.au3> #include <WinAPIGdi.au3> Opt("GUICloseOnESC", 1) ; ESC closes GUI? (0 = no, 1 = yes) Opt("GUIOnEventMode", 1) ; Change to OnEvent mode Opt('MustDeclareVars', 1) Opt("GUIEventOptions", 1) ;0=default, 1=just notification, 2=GuiCtrlRead tab index Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase OnAutoItExitRegister("ExitStageLeft") Global $hGUI, $idTest_btn, $idMemo, $hMemo _Main() Func _Main() Local $str $hGUI = GUICreate("test color detection", 400, 300) $idTest_btn = GUICtrlCreateButton("TEST", 10, 10, 29, 20) GUICtrlSetOnEvent($idTest_btn, "handle_Test_btn") $idMemo = GUICtrlCreateEdit("", 10, 40, 394, 268, BitOR($ES_WANTRETURN, $WS_VSCROLL)) $hMemo = GUICtrlGetHandle($idMemo) GUICtrlSetFont($idMemo, 10, 400, 0, "Courier New") GUICtrlSetColor($idMemo, $COLOR_RED) ; Set the FG color to RED GUICtrlSetBkColor($idMemo, $COLOR_YELLOW) ; Set the BG color to YELLOW $str &= @CRLF $str &= " FG $COLOR_RED = 0x" & Hex($COLOR_RED, 8) & @CRLF $str &= " BG $COLOR_YELLOW = 0x" & Hex($COLOR_YELLOW, 8) & @CRLF $str &= " ABCDEF" & @CRLF & "asdfghjk" & @CRLF GUICtrlSetData($idMemo, $str) _DebugOut("+++: " & $str) GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft') GUISetState() ; Loop until the user exits. Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>_Main Func ExitStageLeft() Exit (9) EndFunc ;==>ExitStageLeft Func handle_Test_btn() _DebugOut("+++ handle_Test_btn() entered") Local $str = "", $iColor, $was $was = Opt("PixelCoordMode") $str &= "PixelCoordMode was = " & $was & ", setting it to 2" & @CRLF Opt("PixelCoordMode", 2) ; 2 = relative coords to the client area of the defined window $iColor = PixelGetColor(0, 0, $hMemo) $str &= "PixelGetColor(0,0): 0x" & Hex($iColor, 8) & @CRLF $iColor = GUICtrlGetColor($hMemo) $str &= "GUICtrlGetColor: 0x" & Hex($iColor, 8) & @CRLF $iColor = GUICtrlGetBkColor($hMemo) $str &= "GUICtrlGetBkColor(): 0x" & Hex($iColor, 8) & @CRLF Opt("PixelCoordMode", $was) _DebugOut("" & $str) EndFunc ;==>handle_Test_btn Func GUICtrlGetColor($hWnd) ; I want this to get the Memo box Foreground (text) color Local $iColor, $aPos, $x, $y $aPos = WinGetPos($hWnd) $x = $aPos[0] ; Absolute (desktop) X-pos of the control $y = $aPos[1] ; Absolute (desktop) Y-pos of the control $iColor = PixelGetColor($x, $y, $hWnd) Return ($iColor) EndFunc ;==>GUICtrlGetColor Func GUICtrlGetBkColor($hWnd) Local $hDC = _WinAPI_GetDC($hWnd) Local $iColor = _WinAPI_GetPixel($hDC, 0, 0) _WinAPI_ReleaseDC($hWnd, $hDC) Return $iColor EndFunc ;==>GUICtrlGetBkColor
BrewManNH Posted August 20, 2018 Posted August 20, 2018 If you are setting the colors in your code, the easiest way to know what they are is to make sure you record them in a variable. Anything else is just complicating a very simple procedure. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
AndyS01 Posted August 21, 2018 Author Posted August 21, 2018 I would like to do that, but my actual app lets me hover over a control on another window and I want to get the FG/BG colors of that control. I can get the ID and hWnd of the control, and I want to find its colors. The BG color is easy, but I have not found a way to get the FG (or Text color). I'm using my test code to prove the concept.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now