There are various ways of getting a monitor. You can get the monitor from a point, from a rectangle, or from a window. But how do you get the primary monitor?
The primary monitor is defined to be the one which has (0, 0) as its origin. Therefore, one solution is
HMONITOR GetPrimaryMonitor() { POINT ptZero = { 0, 0 }; return MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY); }
The desktop window by convention is deemed to reside primarily on the primary monitor, so you could also use this:
HMONITOR GetPrimaryMonitor() { return MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY); }
Or you could just pass the null window handle.
This is technically an illegal parameter,
but by specifying
MONITOR_DEFAULTTOPRIMARY
,
you are saying,
“If anything goes wrong,
give me the primary monitor.”
HMONITOR GetPrimaryMonitor() { return MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY); }
In this case, we are intentionally going astray
because we want to kick in the
MONITOR_DEFAULTTOPRIMARY
behavior.
0 comments