How to get the Monitor Screen Resolution from a hWnd?

screen-resolution, winapi

Solution

The `user32` function MonitorFromWindow allows you to pass in an hwnd, and returns a handle to the monitor it's on (or a default - see the linked MSDN article for details). With that you can call GetMonitorInfo to retrieve a MONITORINFO struct which contains a RECT detailing its resolution.

See the Multiple Screens Reference section of MSDN for more details.

I'd add example code but I don't know the language you referenced, and I don't know how useful C# example code would be to you. If you think it'll help, let me know and I'll code up something real quick.

Problem

How to get the monitor screen resolution from a hWnd? I am using a hWnd because the window could be located on any one of multiple monitors. i.e. the hWnd top/left coordinate is on a Monitor that has a Screen Resolution of 800 x 600. I program in a language called PL/B and it allows calling Windows API. What Window APIs can be used?

Original source