0

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.

jones
  • 585
  • 3
  • 10
  • 30

1 Answers1

1

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.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • In case i have 3 monitors , what do I do then? – jones Mar 01 '17 at 14:04
  • In that case you need to ask a different question. I answered the question you asked. – IInspectable Mar 01 '17 at 14:05
  • ALright thanks, I suppose you could check the rcMonitor.left / right attribute if you knew beforehand how the setup would look like. In case you want the leftmost screen in a 3 monitor setup, you can look for `info_.rcMonitor.left == -1920` for example – jones Mar 01 '17 at 14:44
  • @jones: I don't think there is a public API to query the display number displayed in the display settings dialog. That's usually not very interesting information anyway. The by far more common use case is, that you tell your application, which display you want to use. The application can then query the geometry of the display, and store that information. – IInspectable Mar 01 '17 at 14:58
  • okay thank you, i just thought you would tell your application what display to use by that number, i dont know about the common way of doing it – jones Mar 01 '17 at 15:03
  • 1
    @jones: That monitor number has no universal meaning. It's an implementation detail of the display settings dialog. If you were to implement a similar dialog, you could assign your own numbers to the displays, use them as identifiers when interacting with human users, and store the relevant data (the geometry of the selected display in your case). – IInspectable Mar 01 '17 at 15:21