StickysoftDESIGNS
Skip Navigation Links : Articles : WTL : Multiple monitors support

Don Kackman Multiple monitors classes port to WTL

I was researching the multiple monitors support in Windows and came across the Don Kackman's excellent article describing his MFC wrapper classes around the Win32 multi-monitor API. Since I needed it for a WTL project I ported these classes to WTL retaining a majority of the original code.


How to use it

There are 3 classes:
CMonitor - wrapper around an HMONITOR handle (returned from EnumDisplayMonitors() function) and the GetMonitorInfo() function. With CMonitor you can get at the characteristics of a given monitor.
CMonitors - represents the collection of monitors currently attached to the system and wraps the EnumDisplayMonitors() API function.
CMonitorDC - CDC derived class that represents a monitor specific device context. It's not used anywhere but I kept it for completeness.

With the help of these classes it's very easy to add a multiple monitors support to your application. Take a look at the couple examples below.

1. Add the "Monitor.h" and "Monitors.h" header files to your project.

2. If you are saving a window position to restore it later you need to make sure that the position is still valid before using it. Add the following code right before you create or show a window:

CMonitors::MakeSureRectIsVisible(pRect);
Below is the implementation of this function. No need to explain what it does, right?
void CMonitors::MakeSureRectIsVisible(const LPRECT lprc)
{
    if (!CMonitors::IsOnScreen(lprc))
    {
        CMonitor monitor = CMonitors::GetPrimaryMonitor();
        monitor.CenterRectToMonitor(lprc);
    }
}

3. If you need to center a window on the primary monitor add the following code:

CMonitor monitor = CMonitors::GetPrimaryMonitor();
monitor.CenterWindowToMonitor(this);

Please do not forget that negative coordinates or coordinates larger than SM_CXSCREEN, SM_CYSCREEN are now valid.

Download the source code