jones jones -4 years ago 127
C++ Question

Get handler of a specific monitor to place pop-up window in

So I've been looking through the web for clues how to do this but I can't seem to get it right. I have a dual monitor setup by default and i want to "pick" 'Monitor 2', so i can put a window there in full-screen when a user presses a button in the program.

So far as I've understood i need the handler of the specified monitor as the first step, my approach is to call

EnumDisplayMonitors
according to msdn
"To retrieve information about all of the display monitors, use code like this":

int main()
{
EnumDisplayMonitors(NULL, NULL, MyInfoEnumProc, 0);
}


where
MyInfoEnumProc
is callback defined as follows:

std::vector<HMONITOR> handlerList;
static BOOL CALLBACK MyInfoEnumProc(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData)
{
MONITORINFOEX info_;
info_.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMon, &info_); // retrieve monitor info, put it in info_?
handlerList.push_back(hMon); // push handler to array
std::cout << info_.szDevice; // attempt to print data
std::cout << std::endl;

return true;
}


So this callback should go through all the monitors connected to the system but i don't quite understand how i obtain data like resolution, id and name? Like if i go into Monitor settings from Desktop, there is an ID assigned to each monitor, it would be useful to get these so I can place my window in monitor 2 instead of my primary monitor, monitor 1.
Also regarding the handler, I put this in an array but i really need the data so i know which monitor's handler that I have acquired I suppose? When i print the device name of the monitor
std::cout << info_.szDevice;
i just get the same number for both monitors.

I'm newbie at c++ so i have might have missed something obvious. Hope anyone can help.

Edit:

Thanks to Iinspectable, he mentioned that in the callback function, you can basically check the
dwFlags
attribute to find the primary monitor and then you know which one is the second screen:

static BOOL CALLBACK MyInfoEnumProc(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData)
{
MONITORINFOEX info_;
info_.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMon, &info_);
if (info_.dwFlags == 0) {
std::cout << std::endl;
std::cout << "Found the non-primary monitor" << std::endl;
handlerList.push_back(hMon);
}
return true;
}


Would be useful to have a general solution to this problem in case i wanna connect a third screen,
dwFlags
= 0 in 2 cases for an example with 3 monitors.

Answer Source

The MONITORINFOEX structure populated by the call to GetMonitorInfo has an rcMonitor field, storing the size of the display (in virtual coordinates).

The dwFlags field has the MONITORINFOF_PRIMARY set for the primary monitor. Since you only have 2 displays, you are looking for the monitor that doesn't have this flag set.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download