PixelGetColor has an optional parameter hwnd (handle of window the pixel is read from). Therefore I assume it is possible to read from unfocused windows (i.e not minimized, but behind another window); but I can't get it to work like that.
Is my assumption wrong? If not, how would this be done? If so;
- why the hwnd parameter?
- is there another method involving pixel recognition?
Abstract
You want to create a simple empty bitmap and transfer the DeviceContext content of the hidden window into it. Then you can read any value at any position.
Creating an empty bitmap
Includes
We will need to include the WinAPI definitions and constants.
And that's about it.
Initial struct
Now we have to create a new DLL struct from the
$tagBITMAPINFO
template. We need to fill the struct with the bitmaps parameters. To ensure compatibility across all AutoIt version, I will access the struct items by index.1) Create compatible Device Context:
2) Create struct from template, fill with data:
You need to adjust the width and height parameters to match the region you want to transfer. In your case, I guess the size of the window would be a good choice, though the smaller the region, the faster.
3) Create GDI Object
Create a CreateDIBSection and save the important variables (object handle and struct pointer):
4) Activate
Select the object to use it:
5) Create a pixel-map
This is a dword-array of color values. Replace 160000 with
width*height
of your region:Capture window
Now we need to transfer the hidden window DeviceContext into our "virtual" context, but first the DC of the target (I'm using Paint as an example):
Let's transfer the DC into our DC, using PrintWindow:
Reading single pixels
Since
$hPixelStruct
is a continuous stream of values, we have to do a little bit of math to point at the right pixel:Cleaning up
Lastly, destroy the resources:
Results
Works perfectly. Though this is only valid for hidden windows, NOT minimized, as minimized windows do not have any drawn context.
Script
Here's a Gist containing the script: minxomat/readcolor.au3