dart-windows / win32

Build Win32 apps with Dart!

Home Page:https://win32.pub

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add `MONITORINFOEX` struct

halildurmus opened this issue · comments

Discussed in https://github.com/orgs/dart-windows/discussions/741

Originally posted by aweinber August 23, 2023
Hi,

I'm hoping to screenshot the foreground window and I'm running into some problems matching the foreground window handle to the display's device on which the window primarily sits.

Screenshotting the foreground window is quite doable (with help from some examples ) when dealing with a single-monitor environment using the win32 api: you can create a device context for the singe display, take a screenshot as given in the example, then BitBlt the coordinates of the foreground window into a new device context. Otherwise practically everything is the same. (This approach - taking the full screen screenshot then taking a portion of that - avoids problems screenshotting window directly for windows that have GPU acceleration. I have tried and failed to directly use the device context for foreground window.)

However, the problem occurs when we need to isolate the display on which the window rests. A pure win32 way to do this would be something like this:

HWND hWindow = GetForegroundWindow()
HMONITOR hMonitor = MonitorFromWindow(hWindow, MONITOR_DEFAULTTONULL);
MONITORINFOEX miex;
GetMonitorInfo(hMonitor, &miex);

miex.szDevice  // <---- this is the name of the device, which I can use to CreateDC(nullptr, deviceName, nullptr, nullptr)

Using the MONITORINFOEX struct which includes the device name.

However, dart's MONITORINFO struct does not include the device name, and no MONITORINFOEX struct exists in the dart ffi code as far as I can tell.

(I do have a stupid workaround, which is to use device pixel area to match the monitor and device, but this will fail whenever user has 2 devices of same size.)

Anyone have any suggestions here? How hard would it be to implement the MONITORINFOEX struct such that dart's GetMonitorInfo matches the win32 API?

Thanks!