Jump to content

Recommended Posts

Posted

Hi All,

I am writing a script to control the Reolink desktop client. My development machine is running Windows 10, while the target machine, our HTPC, is running Windows 11. I have just updated to the current release v3.3.18.0.

The script works wonderfully on my development machine, but failed right from the first try on the HTPC. I then realised the colors were slightly different, so got the correct color codes from Au3Info, but I am still failing.

[After writing a lengthy post, I realised there was 1 other difference between the 2 machines] 

I think I have found the problem. PixelGetColor uses different code than whatever Au3Info uses to get pixel color. PixelGetColor fails when running at a non-100% scaling.

The attached image is a screenshot of the issue. The code to make the mouse box is:

Func DoMouse()
    $mouse = MouseGetPos()
    $mgui = GUICreate("MouseTester", 200, 90, -1, 200, 0x80000000 + 0x00400000, 0x00000080 + 0x00000008)
    $MLoc = GUICtrlCreateLabel("Mouse Pos: x = " & $mouse[0] & ", y = " & $mouse[1], 10, 5, 180)
    $Pix = GUICtrlCreateLabel("Pixel Colour: " & Hex(PixelGetColor($mouse[0], $mouse[1])), 10, 25, 180)
    $WDim = GUICtrlCreateLabel("Window Size: " & $RLWinSize[2] & "x" & $RLWinSize[3], 10, 45, 180)
    GUICtrlCreateLabel("Numpad4 to exit", 10, 65, 180)
    GUISetState(@SW_SHOW)
    GUISetState(@SW_DISABLE)
    While 1
        $mouse = MouseGetPos()
        Sleep(100)
        Select
            Case _IsPressed("67") ; = "{numpad7}"
                ;MsgBox(0, "", $nmsg)
                MouseMove($mouse[0] - 1, $mouse[1])
            Case _IsPressed("69") ; = "{numpad9}"
                MouseMove($mouse[0] + 1, $mouse[1])
            Case _IsPressed("68") ; = "{numpad8}"
                MouseMove($mouse[0], $mouse[1] - 1)
            Case _IsPressed("65") ; = "{numpad5}"
                MouseMove($mouse[0], $mouse[1] + 1)
            Case _IsPressed("64") ; = numpad4
                ExitLoop
        EndSelect
        GUICtrlSetData($MLoc, "Mouse Pos: x = " & $mouse[0] & ", y = " & $mouse[1])
        GUICtrlSetData($Pix, "Pixel Colour: " & PixelGetColor($mouse[0], $mouse[1]))
    WEnd
    GUIDelete($mgui)
EndFunc   ;==>DoMouse

My perfectly running script on the development machine broke immediately when changing scale. AU3Info continued to return the correct colors.

What do I replace PixelGetColor with so my code returns the same value as Au3Info?

Screenshot 2025-11-29 144947.jpg

Posted

put at the top of your script
DllCall("User32.dll", "bool", "SetProcessDpiAwarenessContext" , "HWND", "DPI_AWARENESS_CONTEXT" -2)
and try
 :unsure:

I know that I know nothing

Posted

Thanks for the reply.

That fixed the pixel color, which is great, but ...

Now the mouse coordinates and screen size are being reported wrong. It reports the screen size as if no scaling has occurred. The mouse functions report positions as if no scaling is in effect.

Au3Info still reports everything correctly.

Posted

also tried with
DllCall("User32.dll", "bool", "SetProcessDpiAwarenessContext" , "HWND", "DPI_AWARENESS_CONTEXT" -4)

I know that I know nothing

Posted

Take a look at Control Viewer (mod.). That seem to be using "-3".

7 hours ago, photonbuddy said:

It reports the screen size as if no scaling has occurred

If it reports the physical size, then you're good. Just use the scaling as a factor to multiply/divide by, to get to the point in the monitor you want to get to.

Maybe these help:
_WinAPI_SetProcessDpiAwareness() screen scale script example
"GuiCtrls_HiDpi" (UDF in progress) Example/UDF

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)

Tested on a 125% scale and working correctly :

#include <WinAPI.au3>

HotKeySet("{ESC}", Terminate)

Local $nColor, $nScale = DllCall("Shcore.dll", "int", "GetScaleFactorForDevice", "int", 0)[0] / 100
ConsoleWrite("Scale = " & $nScale & @CRLF)

While Sleep(200)
  $nColor = PixelGetColor(Int(MouseGetPos(0) * $nScale), Int(MouseGetPos(1) * $nScale))
  ConsoleWrite(MouseGetPos(0) & "/" &  MouseGetPos(1) & "/" & Hex($nColor) & @CRLF)
WEnd

Func Terminate()
  Exit
EndFunc

ps...I'm using it for my own purpose.  Works well with my Screen Scraping UDF.  So you now have a good solution...

Edited by Nine
better code
Posted (edited)
7 hours ago, Nine said:

Tested on a 125% scale and working correctly :

ps...I'm using it for my own purpose.  Works well with my Screen Scraping UDF.  So you now have a good solution...

It doesn't work on my system, unless I am doing it wrong ...

Turns out, I was 😒 I had bits of code from testing that was messing things up.

Cleaned it all up, and now things seem to be working fine. My DoMouse() call now returns the correct window size and mouse coordinates, and the PixelGetColor also returns the right color.

Thanks muchly for the help.

 

Edited by photonbuddy
Discovered I had left code laying around ...

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
×
×
  • Create New...