Jump to content

PixelGetColor not showing accurately


Recommended Posts

As the title suggest I am having trouble getting the Pixel Color of the mouse point to show the accurate color. It seems to be off by a random amount of pixels each time.

My assumption is that one of my GUI's (SearchArea) is throwing it off but I don't know how to fix it, I've been troubleshooting for hours diggings through similar posts.

The watered down code for troubleshooting:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WINAPI.au3>
#include <Misc.au3>
#include <ImageSearch.au3>
#include <WinAPIGdi.au3>

opt("PixelCoordMode", 2)
opt("MouseCoordMode", 2)
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{END}", "Terminate")
OnAutoItExitRegister("_MyExitFunc")
Global $InLoop = 0
Global $y = 0, $x = 0
$help = 0
$Setup = 0

_WinAPI_AddFontResourceEx("\Fonts\FRIZQT__.ttf", $FR_PRIVATE)

Global $searchL, $searchT, $searchR, $searchB, $color, $Paused, $dll, $OverlapLable, $OverlapLable2, $ColorGUI2, $ColorGUI, $FirstRun
$prog = "TEST"

WinActivate($prog,"")

#Region ### START Koda GUI section ### Form=
$MainGUI = GUICreate($prog & " - Setup", 237, 220)
$Group1 = GUICtrlCreateGroup("", 8, 8, 217, 49)
GUICtrlCreateCombo("", 16, 24, 201, 25)
GUICtrlSetData(-1, "")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group1 = GUICtrlCreateGroup("", 8, 50, 217, 49)
GUICtrlCreateInput("", 16, 66, 201, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group1 = GUICtrlCreateGroup("", 8, 92, 217, 49)
GUICtrlCreateCheckbox("", 16, 112, 200, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Label1 = GUICtrlCreateLabel("To PAUSE at any time press the Pause key", 16, 144, 226, 17)
$Label2 = GUICtrlCreateLabel("To STOP at any time press the End key", 16, 161, 226, 17)
$Start = GUICtrlCreateButton("Start", 8, 180, 219, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

start()

Func start()
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
         Case $GUI_EVENT_CLOSE
            Exit 0
        Case $Start
            GUIWindow()
    EndSwitch
 WEnd
EndFunc

Func GUIWindow()
    $ColorGUI2 = GUICreate("PaintWindow", 520, 256, (@DesktopWidth/2 - 260), 25, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_LAYERED)
    GUISetBkColor(0xABCDEF)
    $GUIPic = GUICtrlCreatePic("bg.gif", 0, 0, 520, 256, -1, $WS_EX_LAYERED)
    GUICtrlSetState(-1, $GUI_DISABLE)
    $GUIPic2 = GUICtrlCreatePic("help.gif", 425, 45, 75, 19, -1, $WS_EX_LAYERED)
    GUICtrlSetCursor(-1, 0)
    GUISetState(@SW_SHOW)
    $exitbtn = GUICtrlCreateLabel("", 489, 13, 22, 22)
    GUICtrlSetBKColor(-1,$GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetCursor(-1, 0)
    $OverlapLable2 = GUICtrlCreateLabel("", 0, 0, 520, 256, -1, $GUI_WS_EX_PARENTDRAG)
    GUICtrlSetBkColor($OverlapLable2, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetTip($OverlapLable2, "You can click and drag me!")
    GUISetState(@SW_SHOW)
    sleep(500)
    GUISetState()

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit 0
        Case $exitbtn
            Exit 0
        Case $GUIPic2
            If $help = 0 Then
            $HelpWindow = GUICreate("", 188, 254, (@DesktopWidth/2-205), 3, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_MDICHILD+$WS_EX_LAYERED, $ColorGUI2)
            GUISetBkColor(0xABCDEF)
            $GUIPic3 = GUICtrlCreatePic("helpGUI.gif", 0, 0, 188, 254, -1, $WS_EX_LAYERED)
            GUICtrlSetState(-1, $GUI_DISABLE)
            GUISetState(@SW_SHOW)
            $help = 1
            Else
            GUIDelete($HelpWindow)
            $help = 0
            EndIf
         EndSwitch
            If $Setup = 0 Then
               $Setup = 1
               Setup()
            EndIf
      WEnd
EndFunc

Func setup()
    GUISetState(@SW_HIDE, $MainGUI)
    Sleep(3000)
    $dll = DllOpen("user32.dll")
    searcharea()
EndFunc

Func searcharea()
    Local $hMask, $hMaster_Mask, $iTemp
    ToolTip("Setup - Click and drag to draw desired search area", 0, 0)
    $hCross_GUI = GUICreate("Test", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)
    WinSetTrans($hCross_GUI, "", 20)
    GUISetState(@SW_SHOW, $hCross_GUI)
    GUISetCursor(3, 1, $hCross_GUI)
    Global $hRectangle_GUI = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
    GUISetBkColor(0xF96924)
    While Not _IsPressed("01", $dll)
        Sleep(10)
    WEnd
    $mouse = MouseGetPos()
    $searchL = $mouse[0]
    $searchT = $mouse[1]
While _IsPressed("01", $dll)
        $mouse = MouseGetPos()
        $hMaster_Mask = _WinAPI_CreateRectRgn(0, 0, 0, 0)
        $hMask = _WinAPI_CreateRectRgn($searchL,  $mouse[1], $mouse[0],  $mouse[1] + 1) ; Bottom of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($searchL, $searchT, $searchL + 1, $mouse[1]) ; Left of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($searchL + 1, $searchT + 1, $mouse[0], $searchT) ; Top of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($mouse[0], $searchT, $mouse[0] + 1,  $mouse[1]) ; Right of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        ; Set overall region
        _WinAPI_SetWindowRgn($hRectangle_GUI, $hMaster_Mask)
        If WinGetState($hRectangle_GUI) < 15 Then GUISetState()
        Sleep(10)
     WEnd
    $searchR = $mouse[0]
    $searchB = $mouse[1]
    If $searchR < $searchL Then
        $iTemp = $searchL
        $searchL = $searchR
        $searchR = $iTemp
    EndIf
    If $searchB < $searchT Then
        $iTemp = $searchT
        $searchT = $searchB
        $searchB = $iTemp
    EndIf
    GUIDelete($hRectangle_GUI)
    GUIDelete($hCross_GUI)
$Window = MsgBox($MB_YESNO, $prog, 'Continue or rety the "Search" window?')
If $Window <> 6 Then
    searcharea()
Else
    If $Window = 2 Then
        Run(FileGetShortName(@ScriptFullPath))
    EndIf
EndIf
Sleep(1000)
    Color()
 EndFunc

Func Color()
    $ColorGUI = GUICreate("",90,90,(@DesktopWidth/2 - 310), 85, $WS_BORDER+$WS_POPUP, $WS_EX_TOPMOST+$WS_EX_MDICHILD, $ColorGUI2)
    GUISetState(@SW_SHOW)
While 1
    $mouse = MouseGetPos()
    $color = PixelGetColor($mouse[0]+2,$mouse[1]+2); GUI is not showing accurate mouse position color
    GUISetBkColor("0x" & Hex($color,6),$ColorGUI) ; Update gui with color seen
    If _IsPressed("01",$dll) Then
       MsgBox(0, "", "Coords are: " & $mouse[0]+2 & " " & $mouse[1]+2) ; Added to error check the coordinates stored.
       ExitLoop; Exit loop when user left clicks
    EndIf
WEnd

Sleep(500)
While _IsPressed("02",$dll) = 0 ; Wait until the user right-click
    Sleep(10)
Wend

Sleep(500)
Sleep(Random(1890,3124))
EndFunc


Func Terminate()
   $Abort = MsgBox($MB_YESNO, $prog, "Are you sure you want to stop?")
   If $Abort = 6 Then
    If @Compiled = 1 Then
        Run( FileGetShortName(@ScriptFullPath))
    Else
        Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
     EndIf
      DllClose($dll)
   EndIf
    Exit
EndFunc


Func _MyExitFunc()
    DllClose($dll)
EndFunc

Specifically line 178, or this Function:

Func Color()
    $ColorGUI = GUICreate("",90,90,(@DesktopWidth/2 - 310), 85, $WS_BORDER+$WS_POPUP, $WS_EX_TOPMOST+$WS_EX_MDICHILD, $ColorGUI2)
    GUISetState(@SW_SHOW)
While 1
    $mouse = MouseGetPos()
    $color = PixelGetColor($mouse[0]+2,$mouse[1]+2); GUI is not showing accurate mouse position color
    GUISetBkColor("0x" & Hex($color,6),$ColorGUI) ; Update gui with color seen
    If _IsPressed("01",$dll) Then
       MsgBox(0, "", "Coords are: " & $mouse[0]+2 & " " & $mouse[1]+2) ; Added to error check the coordinates stored.
       ExitLoop; Exit loop when user left clicks
    EndIf
WEnd

Sleep(500)
While _IsPressed("02",$dll) = 0 ; Wait until the user right-click
    Sleep(10)
Wend

Sleep(500)
Sleep(Random(1890,3124))
EndFunc

The MsgBox I added is definitely showing the wrong coordinates, I just can't find what they're relative to.

Any help would be appreciated!

Edited by kjpolker
Added some comments and error checking
Link to comment
Share on other sites

Is your display set to anything other than 100% zoom level? If it is, that's the problem.

BTW, this line doesn't need to be written like this.

GUISetBkColor("0x" & Hex($color,6),$ColorGUI)
 ; Just write it like this
GUISetBkColor($color,$ColorGUI)

GUISetBkColor will take decimal numbers as well as Hex, so no need to try and convert it to a string to imitate a hex number first.

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 Gude
How 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

Link to comment
Share on other sites

10 minutes ago, BrewManNH said:

Is your display set to anything other than 100% zoom level? If it is, that's the problem.

BTW, this line doesn't need to be written like this.

GUISetBkColor("0x" & Hex($color,6),$ColorGUI)
 ; Just write it like this
GUISetBkColor($color,$ColorGUI)

GUISetBkColor will take decimal numbers as well as Hex, so no need to try and convert it to a string to imitate a hex number first.

I am not sure what you mean, I get the same effect on two different computers and my display is set to 100%.

It's worth noting that after I added the MsgBox to show coords and I click somewhere on the screen I get bad results (like 10, -160 even tough I am in the middle of the screen).

This is from clicking in the same spot:

results.png

Edited by kjpolker
Link to comment
Share on other sites

You're using Client coordinate mode, which means it's using the coordinates inside the client area of the window you're clicking on, not the coordinates of the whole screen.

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 Gude
How 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

Link to comment
Share on other sites

9 minutes ago, BrewManNH said:

You're using Client coordinate mode, which means it's using the coordinates inside the client area of the window you're clicking on, not the coordinates of the whole screen.

Still getting funky results, the coordinates are accurate if I change to Absolute but the colors are off by about 500 pixels to the left.

Link to comment
Share on other sites

You and I are getting different results, I'm getting the correct colors and the correct coordinates using your script. I don't use display scaling, and that's usually why that happens.

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 Gude
How 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

Link to comment
Share on other sites

I apologize, I did have display settings jacked up. Was in 150% (Thanks Surface...). I won't be able to check back on my home computer for a few hours but I am certain that is 100% and I THOUGHT I was getting bad results... 

That being said, is there an easy solution to this considering some clients may be outside of 100%?

Link to comment
Share on other sites

There have been a few threads on this subject, you can search for DPI aware on the forums for some suggestions.

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 Gude
How 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

Link to comment
Share on other sites

alpine over at autoit.de has recently posted a script called GUIScaler, there's some functions about DPI scaling in it, check it out, google translate it if you need.

https://autoit.de/index.php?thread/86505-guiscaler-guis-automatisch-zur-dpi-skalieren-lassen-windows-7-und-windows-10-per/

Some guy's script + some other guy's script = my script!

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