Jump to content

Getting color of a control click position


Recommended Posts

I have spent time time trying to figure out a hack around this and I think it's time to ask for help. The problem is as follows:

I need to check the color change on a button. Since this window is a child window and may open in different places the ControlClick coordinates will tell me where the color change should be.

My problem is that when using PixelGetColor, the coordinates it accepts are not where I need to check. I need to check the color under the mouse pointer(which is why I was trying to use the controlclick coordinates).

Any thoughts?

Link to comment
Share on other sites

Make sure your options/defaults for MouseCoordMode and PixelCoordMode are the same and when you get the mouse pos, move the mouse at least a few pixels off it before getting the pixel color, else you get the color of the mouse pointer itself instead of the pixel below it.

;)

Edit: Mouse moving not required:

Global $aPos, $iColor

Opt("MouseCoordMode", 1) ; Default, screen relative
Opt("PixelCoordMode", 1) ; Default, screen relative

HotKeySet("{ESC}", "_Quit")

While 1
    $aPos = MouseGetPos()
    $iColor = PixelGetColor($aPos[0], $aPos[1])
    ToolTip("Pos: x = " & $aPos[0] & ", y = " & $aPos[1] & @CRLF & _
            "Color = 0x" & Hex($iColor, 6))
    Sleep(500)
WEnd

Func _Quit()
    Exit
EndFunc
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
Link to comment
Share on other sites

The problem I have is that I will be running my script on multiple systems and have see the mouse and pixel modes relative to the active window I am working in. On thing I thought was to ControlClick on the button I need (it may open in different areas) and then get the mouse position. But, I see that a ControlClick doesn't actually move the mouse pointer. I haven't figured out how to read a pixel color from where I just clicked on the control.

Link to comment
Share on other sites

The problem I have is that I will be running my script on multiple systems and have see the mouse and pixel modes relative to the active window I am working in. On thing I thought was to ControlClick on the button I need (it may open in different areas) and then get the mouse position. But, I see that a ControlClick doesn't actually move the mouse pointer. I haven't figured out how to read a pixel color from where I just clicked on the control.

That was my mistake reading it too fast. I wasn't thinking of ControlClick().

Do you have a control ID? Either way you will want to use PixelCoordMode = 3 (window client area relative). If you have the control ID, then ControlGetPos() and do the math, relative to the window client area. If you don't then the PixelGetColor() X\Y coordinates will be the same as the ControlClick() X\Y, again relative to window client area.

I don't know off the top of my head, but I'll bet you can get the control's background and text colors from the _WinAPI* or _GuiCtrlButton_* UDF functions without any console interaction at all.

;)

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
Link to comment
Share on other sites

Here's one trick for it: A control is sort of a child window. It has a handle the same way a window does, and you can pass that handle to PixelGetColor() the same as for a window:

#include <GUIConstantsEx.au3>

Opt("PixelCoordMode", 0) ; Window relative

$hGUI = GUICreate("Test", 300, 210)
$ctrlButton1 = GUICtrlCreateButton("Button One", 100, 30, 100, 30)
GUICtrlSetBkColor(-1, 0xFF0000) ; Red
$ctrlButton2 = GUICtrlCreateButton("Button Two", 100, 90, 100, 30)
GUICtrlSetBkColor(-1, 0xFF00) ; Blue
$ctrlButton3 = GUICtrlCreateButton("Button Three", 100, 150, 100, 30)
GUICtrlSetBkColor(-1, 0xFF) ; Green
GUISetState()

$iColor = PixelGetColor(10, 10, $hGUI)
ConsoleWrite("GUI: $iColor = 0x" & Hex($iColor, 6) & @LF)
$iColor = PixelGetColor(10, 10, ControlGetHandle($hGUI, "", $ctrlButton1))
ConsoleWrite("Button One: $iColor = 0x" & Hex($iColor, 6) & @LF)
$iColor = PixelGetColor(10, 10, ControlGetHandle($hGUI, "", $ctrlButton2))
ConsoleWrite("Button Two: $iColor = 0x" & Hex($iColor, 6) & @LF)
$iColor = PixelGetColor(10, 10, ControlGetHandle($hGUI, "", $ctrlButton3))
ConsoleWrite("Button Three: $iColor = 0x" & Hex($iColor, 6) & @LF)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

I'm probably forgetting a slick ControlGetBkColor() function somewhere, but this will work for now.

;)

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