Archive for the ‘MSDN’ tag

Enumerating Active Display Resolutions

without comments

This post is quick C++ tidbit on Win32 for enumerating the displays on a system as well as the current resolution and pixel offset of each display. I’m using this to automatically layout multiple windows within an application to different monitors (when possible).

The code is straightforward and comes directly from the MSDN documentation; however, this may provide a direct example for someone looking to accomplish something similar. (Note for understanding the code below: the “lxvar” data type is a variant data-type very similar to a Javascript var – i.e. a object capable of storing ints, floats, strings, arrays, and maps in a nested fashion.)

The Code

lxvar
_lx_monitor_info ()
{
    // 
    // Hide the boilerplate of the WIN32 callback in an hidden class
    //
    class _MonitorInfo
    {
    public:
        _MonitorInfo()
        {
            index = 0;
            ::EnumDisplayMonitors(NULL, NULL, enumMonitorsCallback, (LPARAM)this);
        }
 
        int     index;
        lxvar   data;
 
    protected:
        static
        BOOL CALLBACK
        enumMonitorsCallback (HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
        {
            _MonitorInfo& monitorInfo = *reinterpret_cast<_MonitorInfo*>(dwData);
 
            auto& info = monitorInfo.data[monitorInfo.index];
            info["offset"][0] = lprcMonitor->left;
            info["offset"][1] = lprcMonitor->top;
            info["size"][0] = lprcMonitor->right - lprcMonitor->left;
            info["size"][1] = lprcMonitor->bottom - lprcMonitor->top;
 
            monitorInfo.index++;
            return TRUE;
        }
    };
 
    _MonitorInfo info;
    return info.data;
}

Written by arthur

January 23rd, 2012 at 5:05 pm