Jump to content

Recommended Posts

Posted

Thanks PsaltyDS :)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Thanks guys :)

But I wanted to get the colour of the text and not the background.

How can I change the function to make it get the text colour in the Label? ;)

Posted (edited)

Did you set the colour of the text? And you didn't mention this in the OP!

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 6/30/2011 at 8:56 PM, 'guinness said:

Did you set the colour of the text? And you didn't mention this in the OP!

My bad =/ yeah I did,

I have used the example of GUICtrGetBkColor() from your example script to test it out:

#include <WinAPIEx.au3> ; Reguired for _WinAPI_GetPixel()

_Main()

Func _Main()
    Local $aColor_Array[6] = [5, 0x0000FF, 0x8FFF9F, 0xEC4841, 0xB0E35D, 0x440BFD] ; Random Color Array.
    Local $hGUI, $iColor, $iLabel

    $hGUI = GUICreate("GUICtrlGetBkColor() Example", 500, 350)
    $iLabel = GUICtrlCreateLabel("wt", 0, 0, 500, 350)
    GUICtrlSetColor(-1, 0x000000)
    GUISetState(@SW_SHOW)

    $iColor = GUICtrlGetBkColor($iLabel)
MsgBox(0, "The Hex value is...", _ConvertToHexFormat($iColor))
    Exit
EndFunc   ;==>_Main



Func _ConvertToHexFormat($iColor)
    Return Hex($iColor, 6)
EndFunc   ;==>_ConvertToHexForma


Func GUICtrlGetBkColor($iControlID)
    Local $bGetBkColor, $hDC, $hHandle
    $hHandle = GUICtrlGetHandle($iControlID)
    $hDC = _WinAPI_GetDC($hHandle)
    $bGetBkColor = _WinAPI_GetPixel($hDC, 0, 0)
    _WinAPI_ReleaseDC($hHandle, $hDC)
    Return $bGetBkColor
EndFunc   ;==>GUICtrlGetBkColor
Posted

I could be wrong, but I don't think what you're asking is possible.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

Hmm... seems like this should have worked, but it doesn't:

#include <WinAPIEx.au3>

_Main()

Func _Main()
    Local $aColor_Array[6] = [5, 0x0000FF, 0x8FFF9F, 0xEC4841, 0xB0E35D, 0x440BFD] ; Random Color Array.
    Local $hGUI, $iColor, $aLabel[6] = [5]

    $hGUI = GUICreate("GUICtrlGetBkColor() Example", 500, 350)

    For $n = 1 To 5
        $aLabel[$n] = GUICtrlCreateLabel("Label " & $n, 10, 10 + ($n * 30), 480, 20)
        GUICtrlSetColor($aLabel[$n], $aColor_Array[$n])
    Next

    GUISetState(@SW_SHOW)

    For $n = 1 To 5
        $hHandle = GUICtrlGetHandle($aLabel[$n])
        $hDC = _WinAPI_GetDC($hHandle)
        $iColor = _WinAPI_GetTextColor($hDC)
        $iErrSav = @error
        ConsoleWrite("$aColor_Array[" & $n & "] = 0x" & Hex($aColor_Array[$n]) & "; $aLabel[" & $n & "] = " & $aLabel[$n] & _
                "; $hHandle = " & $hHandle & "; $hDC = " & $hDC & "; $iColor = 0x" & Hex($iColor) & "; @error = " & $iErrSav & @LF)
        _WinAPI_ReleaseDC($hHandle, $hDC)
    Next

    Do
    Until GUIGetMsg() = -3
EndFunc   ;==>_Main

:)

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
Posted (edited)

This also should work, but nope... :)

#include <WinAPIEx.au3>
#include <WindowsConstants.au3>
Global $hGUI, $iColor, $aLabel[6] = [5]
_Main()
Func _Main()
    Local $aColor_Array[6] = [5, 0x0000FF, 0x8FFF9F, 0xEC4841, 0xB0E35D, 0x440BFD] ; Random Color Array.
    $hGUI = GUICreate("GUICtrlGetBkColor() Example", 500, 350)
    GUIRegisterMsg($WM_CTLCOLORSTATIC, "WM_CTLCOLORSTATIC")
    For $n = 1 To 5
        $aLabel[$n] = GUICtrlCreateLabel("Label " & $n, 10, 10 + ($n * 30), 480, 20)
        GUICtrlSetColor($aLabel[$n], $aColor_Array[$n])
    Next
    GUICtrlCreateButton("", 150, 200, 100, 60)
    GUISetState(@SW_SHOW)
    Do
    Until GUIGetMsg() = -3
EndFunc   ;==>_Main
Func WM_CTLCOLORSTATIC($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_SetTextColor($wParam, 0xff0000) ; All the labels must have red text..
    _WinAPI_SetBkColor($wParam, 0x000000) ; All the labels must have black background..
;   _winapi_getbkcolor($wParam)
    _WinAPI_SetBkMode($wParam, $OPAQUE) ; All the labels must not have transparent Bk..
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>WM_CTLCOLORSTATIC
Edited by monoscout999
  • Moderators
Posted

XTensionX,

  Quote

Guess this isnt possible

Guessed wrong! ;)

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$hLabel = GUICtrlCreateLabel("Test", 10, 10, 100, 20)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFFFF00)

GUISetState()

$iBackColour = GUICtrlGetBkColorEx($hLabel)
ConsoleWrite("BackColor: 0x" & Hex($iBackColour, 6) & @CRLF)

$iTextColour = _GUICtrlGetTextColor($hGUI, $hLabel, $iBackColour)
If @error Then
    ConsoleWrite("No text found" & @CRLF)
Else
    ConsoleWrite("TextColor: 0x" & Hex($iTextColour, 6) & @CRLF)
EndIf

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Func _GUICtrlGetTextColor($hWnd, $hControl, $iBkCol)

    Local $tPoint = DllStructCreate("int X;int Y")

    ; Get position of control
    Local $aPos = ControlGetPos($hGUI, "", $hControl)
    ; Convert to screen coords
    DllStructSetData($tPoint, "X", $aPos[0])
    DllStructSetData($tPoint, "Y", $aPos[1])
    _WinAPI_ClientToScreen($hGUI, $tPoint)
    $aPos[0] = DllStructGetData($tPoint, "X") + 1 ; Needs the added 1 to hit the first coloured pixel
    $aPos[1] = DllStructGetData($tPoint, "Y") + 1 ; I have no idea why

    ; Sweep through control to find another colour
    For $iX = $aPos[0] To $aPos[0] + $aPos[2] - 2 ; Again the need to adjust the coords returned by ControlGetPos
        For $iY = $aPos[1] To $aPos[1] + $aPos[3] - 2 ; And here too
            If PixelGetColor($iX, $iY) <> $iBkCol Then
                ; Assume that it is text colour
                Return PixelGetColor($iX, $iY)
            EndIf
        Next
    Next

    ; Text colour not found, so return error
    Return SetError(1, 0, 0)

EndFunc

Func GUICtrlGetBkColorEx($iControlID)
    Local $aResult, $bGetBkColor, $hDC, $hHandle
    $hHandle = GUICtrlGetHandle($iControlID)
    $aResult = DllCall("user32.dll", "handle", "GetDC", "hwnd", $hHandle)
    If @error Then
        Return SetError(@error, @extended, 0)
    EndIf
    $hDC = $aResult[0]
    $aResult = DllCall("gdi32.dll", "dword", "GetPixel", "hwnd", $hDC, "int", 0, "int", 0)
    If @error Or ($aResult[0] = 0xFFFFFFFF) Then
        Return SetError(@error, @extended, 0)
    EndIf
    $bGetBkColor = $aResult[0]
    $aResult = DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $hHandle, "handle", $hDC)
    If @error Then
        Return SetError(@error, @extended, 0)
    EndIf
    Return BitOR(BitAND($bGetBkColor, 0x00FF00), BitShift(BitAND($bGetBkColor, 0x0000FF), -16), BitShift(BitAND($bGetBkColor, 0xFF0000), 16))
EndFunc   ;==>GUICtrlGetBkColorEx

Not pretty but it works. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

Not working for me Melba. :)

The text color output is wrong and changes every time I run the script.

Edit: Seems like the aero fading effect of Windows 7 caused the problem.

Passing WinWait($hGUI) after GUISetState() fixes it.

Great work Melba!

Edited by Info
Posted (edited)

  On 7/2/2011 at 8:13 AM, 'Melba23 said:

Not pretty but it works. ;)

Hmm... not for me. You are setting background to 0xFFFF00 (yellow), and text to 0xFF0000. But this is what I get:
>Running:(3.3.7.10):C:\Program Files\AutoIt3\Beta\autoit3.exe "C:\Temp\Test1.au3"    
BackColor: 0xFFFF00
TextColor: 0xE5ECF3
Moreover, I get a different color every time, i.e. 0xB0B5F6, 0xF4F0F9, etc.

Same problem in either Prod or Beta.

Win7Pro x32.

;)

Edit: Added some debug to it, and now I get the same wrong answer every time. Weird. Debug version:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$hLabel = GUICtrlCreateLabel("Test", 10, 10, 100, 20)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFFFF00)

GUISetState()

$iBackColour = GUICtrlGetBkColorEx($hLabel)
ConsoleWrite("BackColor: 0x" & Hex($iBackColour, 6) & @CRLF)

$iTextColour = _GUICtrlGetTextColor($hGUI, $hLabel, $iBackColour)
If @error Then
    ConsoleWrite("No text found" & @CRLF)
Else
    ConsoleWrite("TextColor: 0x" & Hex($iTextColour, 6) & @CRLF)
EndIf

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _GUICtrlGetTextColor($hWnd, $hControl, $iBkCol)
    Local $tPoint = DllStructCreate("int X;int Y")

    ; Get position of control
    Local $aPos = ControlGetPos($hGUI, "", $hControl)

    ; Convert to screen coords
    DllStructSetData($tPoint, "X", $aPos[0])
    DllStructSetData($tPoint, "Y", $aPos[1])
    _WinAPI_ClientToScreen($hGUI, $tPoint)
    $aPos[0] = DllStructGetData($tPoint, "X") + 1 ; Needs the added 1 to hit the first coloured pixel
    $aPos[1] = DllStructGetData($tPoint, "Y") + 1 ; I have no idea why
    ConsoleWrite("Debug:  X = " & $aPos[0] & "; Y = " & $aPos[1] & "; W = " & $aPos[2] & "; H = " & $aPos[3] & @LF)

    ; Sweep through control to find another colour
    For $iX = $aPos[0] To $aPos[0] + $aPos[2] - 2 ; Again the need to adjust the coords returned by ControlGetPos
        For $iY = $aPos[1] To $aPos[1] + $aPos[3] - 2 ; And here too
            $iFindColor = PixelGetColor($iX, $iY)
            ConsoleWrite("Debug:  $iX = " & $iX & "; $iY = " & $iY & "; $iFindColor = 0x" & Hex($iFindColor, 6) & @LF)

            If $iFindColor <> $iBkCol Then
                ; Assume that it is text colour
                MouseMove($iX, $iY) ; Point to where it was found
                Return $iFindColor
            EndIf
        Next
    Next

    ; Text colour not found, so return error
    Return SetError(1, 0, 0)
EndFunc   ;==>_GUICtrlGetTextColor

Func GUICtrlGetBkColorEx($iControlID)
    Local $aResult, $bGetBkColor, $hDC, $hHandle
    $hHandle = GUICtrlGetHandle($iControlID)
    $aResult = DllCall("user32.dll", "handle", "GetDC", "hwnd", $hHandle)
    If @error Then
        Return SetError(@error, @extended, 0)
    EndIf
    $hDC = $aResult[0]
    $aResult = DllCall("gdi32.dll", "dword", "GetPixel", "hwnd", $hDC, "int", 0, "int", 0)
    If @error Or ($aResult[0] = 0xFFFFFFFF) Then
        Return SetError(@error, @extended, 0)
    EndIf
    $bGetBkColor = $aResult[0]
    $aResult = DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $hHandle, "handle", $hDC)
    If @error Then
        Return SetError(@error, @extended, 0)
    EndIf
    Return BitOR(BitAND($bGetBkColor, 0x00FF00), BitShift(BitAND($bGetBkColor, 0x0000FF), -16), BitShift(BitAND($bGetBkColor, 0xFF0000), 16))
EndFunc   ;==>GUICtrlGetBkColorEx

Result:

>Running:(3.3.7.10):C:\Program Files\AutoIt3\Beta\autoit3.exe "C:\Temp\Test1.au3"    
BackColor: 0xFFFF00
Debug:  X = 445; Y = 137; W = 100; H = 20
Debug:  $iX = 445; $iY = 137; $iFindColor = 0xF0F4F9
TextColor: 0xF0F4F9

I don't get it. The mouse pointer goes to the right place, so it looks like PixelGetColor() is actually failing.

:)

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
Posted (edited)

Arrrgh! Just put a Sleep() after GuiSetState() and it works! The pixel checking was happening before the GUI was displayed. This could be variable based on Windows effects like fading in new windows.

Sheesh! And could have saved myself all the trouble by reading Info's post earlier!

:)

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
Posted

You did a good job melba23... but despite that you find a solution, i still have the bitter sensation that the _winapi_gettextcolor() function should be work, but it doesn´t, even with Static classes outside autoit.

Posted

Well I never! :)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 7/2/2011 at 8:13 AM, 'Melba23 said:

XTensionX,

Guessed wrong! ;)

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$hLabel = GUICtrlCreateLabel("Test", 10, 10, 100, 20)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFFFF00)

GUISetState()

$iBackColour = GUICtrlGetBkColorEx($hLabel)
ConsoleWrite("BackColor: 0x" & Hex($iBackColour, 6) & @CRLF)

$iTextColour = _GUICtrlGetTextColor($hGUI, $hLabel, $iBackColour)
If @error Then
    ConsoleWrite("No text found" & @CRLF)
Else
    ConsoleWrite("TextColor: 0x" & Hex($iTextColour, 6) & @CRLF)
EndIf

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Func _GUICtrlGetTextColor($hWnd, $hControl, $iBkCol)

    Local $tPoint = DllStructCreate("int X;int Y")

    ; Get position of control
    Local $aPos = ControlGetPos($hGUI, "", $hControl)
    ; Convert to screen coords
    DllStructSetData($tPoint, "X", $aPos[0])
    DllStructSetData($tPoint, "Y", $aPos[1])
    _WinAPI_ClientToScreen($hGUI, $tPoint)
    $aPos[0] = DllStructGetData($tPoint, "X") + 1 ; Needs the added 1 to hit the first coloured pixel
    $aPos[1] = DllStructGetData($tPoint, "Y") + 1 ; I have no idea why

    ; Sweep through control to find another colour
    For $iX = $aPos[0] To $aPos[0] + $aPos[2] - 2 ; Again the need to adjust the coords returned by ControlGetPos
        For $iY = $aPos[1] To $aPos[1] + $aPos[3] - 2 ; And here too
            If PixelGetColor($iX, $iY) <> $iBkCol Then
                ; Assume that it is text colour
                Return PixelGetColor($iX, $iY)
            EndIf
        Next
    Next

    ; Text colour not found, so return error
    Return SetError(1, 0, 0)

EndFunc

Func GUICtrlGetBkColorEx($iControlID)
    Local $aResult, $bGetBkColor, $hDC, $hHandle
    $hHandle = GUICtrlGetHandle($iControlID)
    $aResult = DllCall("user32.dll", "handle", "GetDC", "hwnd", $hHandle)
    If @error Then
        Return SetError(@error, @extended, 0)
    EndIf
    $hDC = $aResult[0]
    $aResult = DllCall("gdi32.dll", "dword", "GetPixel", "hwnd", $hDC, "int", 0, "int", 0)
    If @error Or ($aResult[0] = 0xFFFFFFFF) Then
        Return SetError(@error, @extended, 0)
    EndIf
    $bGetBkColor = $aResult[0]
    $aResult = DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $hHandle, "handle", $hDC)
    If @error Then
        Return SetError(@error, @extended, 0)
    EndIf
    Return BitOR(BitAND($bGetBkColor, 0x00FF00), BitShift(BitAND($bGetBkColor, 0x0000FF), -16), BitShift(BitAND($bGetBkColor, 0xFF0000), 16))
EndFunc   ;==>GUICtrlGetBkColorEx

Not pretty but it works. :)

M23

Thanks Thanks Melba23 ^^

This is working perfectly ;)

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
  • Recently Browsing   0 members

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