Jump to content

GUICtrlGetColor()


 Share

Recommended Posts

  • Moderators

I'm curious on why you need to get a default color.

ControlGetHandle() or GUICtrlGetHandle()

+

http://msdn.microsoft.com/en-us/library/aa921543.aspx

+

http://msdn.microsoft.com/en-us/library/dd144852(VS.85).aspx

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thank you, good solution. But this will only work for the Background color. I now use _WinAPI_GetSysColor($COLOR_WINDOWTEXT).

I'm curious on why you need to get a default color.

I need to change the text color of Input control and then return it back. I wanted to find the elegant solution.
Link to comment
Share on other sites

  • Moderators

Thank you, good solution. But this will only work for the Background color. I now use _WinAPI_GetSysColor($COLOR_WINDOWTEXT).

I need to change the text color of Input control and then return it back. I wanted to find the elegant solution.

You should have been much more specific. You didn't mention text anywhere :D .

Edit:

http://msdn.microsoft.com/en-us/library/dd144934(VS.85).aspx

I believe that comes in BGR but easily remedied :o .

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You should have been much more specific. You didn't mention text anywhere :D .

Sorry. :D

Edit:

http://msdn.microsoft.com/en-us/library/dd144934(VS.85).aspx

I believe that comes in BGR but easily remedied :P .

Many thanks to you! :o
Link to comment
Share on other sites

"GetTextColor" does not work, see example.

:D

#Include <WinAPI.au3>

GUICreate('Test', 200, 200)
$Input = GUICtrlCreateInput('Simple text', 20, 20, 160, 20)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFAFAFA)
GUISetState()

MsgBox(0, '', '0x' & Hex(_WinAPI_GetTextColor(GUICtrlGetHandle($Input))))

func _WinAPI_GetTextColor($hWnd)
    
    local $RGB, $hDC = _WinAPI_GetDC($hWnd)
    
    if $hDC = 0 then
        return SetError(1, 0, -1)
    endif
    $RGB = DllCall('gdi32.dll', 'int', 'GetTextColor', 'hwnd', $hDC)
    if (@error) or ($RGB[0] = 0xFFFFFFFF) then
        $RGB = -1
    endif 
    
    _WinAPI_ReleaseDC($hWnd, $hDC)
    
    if $RGB = -1 then
        return SetError(1, 0, -1)
    endif
    return SetError(0, 0, $RGB[0])
endfunc; _WinAPI_GetTextColor
Link to comment
Share on other sites

This works:

#Include <WinAPI.au3>
#Include <WindowsConstants.au3>
Global $WINDOW_TO_QUERY, $___RGB
GUIRegisterMsg($WM_CTLCOLORSTATIC,"MY_CTLCOLOR")
GUIRegisterMsg($WM_CTLCOLOREDIT,"MY_CTLCOLOR")
GUIRegisterMsg($WM_CTLCOLORBTN,"MY_CTLCOLOR")

GUICreate('Test', 200, 200)

$Input = GUICtrlCreateInput('Simple text', 20, 20, 160, 20)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFAFAFA)
GUISetState()

MsgBox(0, '', '0x' & Hex(_WinAPI_GetTextColor(GUICtrlGetHandle($Input))))

func _WinAPI_GetTextColor($hWnd)
    ; Prog@ndy
    Global $___RGB = -1
    $WINDOW_TO_QUERY = $hWnd
    _WinAPI_RedrawWindow($hWnd,0,0,$RDW_INVALIDATE+$RDW_UPDATENOW)
    Do 
        Sleep(10)
    Until $___RGB <> -1
    Return $___RGB
    
endfunc; _WinAPI_GetTextColor

Func MY_CTLCOLOR($hWnd,$uMsg,$wParam, $lParam)
    ; Prog@ndy
    If $lParam = $WINDOW_TO_QUERY Then
        Local $RGB = DllCall('gdi32.dll', 'int', 'GetTextColor', 'ptr', $wParam)
        If Not @error Then 
            Global $___RGB = $RGB[0]
        Else
            Global  $___RGB = -2
        EndIf
    EndIf
        Return 'GUI_RUNDEFMSG'
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

This works:

#Include <WinAPI.au3>
#Include <WindowsConstants.au3>
Global $WINDOW_TO_QUERY, $___RGB
GUIRegisterMsg($WM_CTLCOLORSTATIC,"MY_CTLCOLOR")
GUIRegisterMsg($WM_CTLCOLOREDIT,"MY_CTLCOLOR")
GUIRegisterMsg($WM_CTLCOLORBTN,"MY_CTLCOLOR")

GUICreate('Test', 200, 200)

$Input = GUICtrlCreateInput('Simple text', 20, 20, 160, 20)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFAFAFA)
GUISetState()

MsgBox(0, '', '0x' & Hex(_WinAPI_GetTextColor(GUICtrlGetHandle($Input))))

func _WinAPI_GetTextColor($hWnd)
    ; Prog@ndy
    Global $___RGB = -1
    $WINDOW_TO_QUERY = $hWnd
    _WinAPI_RedrawWindow($hWnd,0,0,$RDW_INVALIDATE+$RDW_UPDATENOW)
    Do 
        Sleep(10)
    Until $___RGB <> -1
    Return $___RGB
    
endfunc; _WinAPI_GetTextColor

Func MY_CTLCOLOR($hWnd,$uMsg,$wParam, $lParam)
    ; Prog@ndy
    If $lParam = $WINDOW_TO_QUERY Then
        Local $RGB = DllCall('gdi32.dll', 'int', 'GetTextColor', 'ptr', $wParam)
        If Not @error Then 
            Global $___RGB = $RGB[0]
        Else
            Global  $___RGB = -2
        EndIf
    EndIf
        Return 'GUI_RUNDEFMSG'
EndFunc
Exelent example. Thanks.

:D

Edited by Yashied
Link to comment
Share on other sites

Added to ProgAndy's script,

This script returns :-

background colour if 2nd parameter is 0 or absent, using the gdi32.dll 'GetBkColor' function, or,

text colour if 2nd parameter does not equal 0, using the gdi32.dll 'GetTextColor' function.

And using _WinAPI_GetBkColor(), the background colour of a GUI is returned.

#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global $WINDOW_TO_QUERY, $___RGBTC, $___RGBBGC
GUIRegisterMsg($WM_CTLCOLORSTATIC, "MY_CTLCOLOR")
GUIRegisterMsg($WM_CTLCOLOREDIT, "MY_CTLCOLOR")
GUIRegisterMsg($WM_CTLCOLORBTN, "MY_CTLCOLOR")

$hGui = GUICreate('Test', 200, 200)
GUISetBkColor(0xABCDEF)

$Input = GUICtrlCreateInput('Simple text', 20, 20, 160, 20)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFAFAFF)
GUISetState()

MsgBox(0, 'Colours in RGB format', 'Text colour input    = : 0x' & Hex(_WinAPI_GetTextColor($Input, 1), 6) & @CRLF & _
        'BackGnd colour input = : 0x' & Hex(_WinAPI_GetTextColor($Input), 6) & @CRLF & _
        'BackGnd colour GUI   = : 0x' & Hex(_WinAPI_GetBkColor($hGui), 6) & @CRLF)

; $BGC_TC = 0 for background colour,  $BGC_TC <> 0 for text colour
Func _WinAPI_GetTextColor($hWnd, $BGC_TC = 0)
    ; Prog@ndy
    Global $___RGBTC = -1, $___RGBBGC = -1
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    $WINDOW_TO_QUERY = $hWnd
    _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_INVALIDATE + $RDW_UPDATENOW)
    Do
        Sleep(10)
    Until $___RGBTC <> -1 And $___RGBBGC <> -1
    If $BGC_TC = 0 Then
        Return $___RGBBGC
    Else
        Return $___RGBTC
    EndIf
EndFunc   ;==>_WinAPI_GetTextColor

Func MY_CTLCOLOR($hWnd, $uMsg, $wParam, $lParam)
    ; Prog@ndy
    If $lParam = $WINDOW_TO_QUERY Then
        Local $RGB = DllCall('gdi32.dll', 'int', 'GetTextColor', 'ptr', $wParam)
        If Not @error Then
            Global $___RGBTC = "0x" & StringRegExpReplace(Hex($RGB[0], 6), "(.{2})(.{2})(.{2})", "\3\2\1")
        Else
            Global $___RGBTC = -2
        EndIf
        Local $___RGBBGC = DllCall('gdi32.dll', 'int', 'GetBkColor', 'ptr', $wParam)
        If Not @error Then
            Global $___RGBBGC = "0x" & StringRegExpReplace(Hex($___RGBBGC[0], 6), "(.{2})(.{2})(.{2})", "\3\2\1")
        Else
            Global $___RGBBGC = -2
        EndIf
    EndIf
    Return 'GUI_RUNDEFMSG'
EndFunc   ;==>MY_CTLCOLOR

; Get background colour of GUI window, NOT a control.
Func _WinAPI_GetBkColor($hWnd)
    ; Not Prog@ndy
    Local $aResult, $hDC, $Res
    If Not IsHWnd($hWnd) Then $hWnd = ControlGetHandle("", "", $hWnd)
    $hDC = _WinAPI_GetDC($hWnd)
    $aResult = DllCall("GDI32.dll", "int", "GetBkColor", "hwnd", $hDC)
    ConsoleWrite("Hex($aResult[0], 6) = " & Hex($aResult[0], 6) & @CRLF)
    $Res = "0x" & StringRegExpReplace(Hex($aResult[0], 6), "(.{2})(.{2})(.{2})", "\3\2\1")
    _WinAPI_ReleaseDC($hWnd, $hDC)
    Return $Res
EndFunc   ;==>_WinAPI_GetBkColor
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...