Click here to Skip to main content
Click here to Skip to main content

MFC classes for multiple monitors

, 27 Aug 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
Class wrappers around the Win32 multi-monitor API
<!-- Article image -->

Sample Image - multimon.jpg

<!-- Add the rest of your HTML here -->


Awhile back I decided to write a screensaver application as a way to pick up MFC. After I got my first implementation up and running I shipped it off to a friend. Well the first thing he noted was a a glaring bug: he uses multiple monitors and my screen saver only showed up on one.

After some scouring of the internet and research into MSDN I was able to to get the screensaver running on multiple monitors by using some API calls the were introduced with Windows 98/2000. This project is a couple of small MFC classes that wrap the multi-monitor API.

These classes can be safely used on Windows 95/NT4 as well. On those platforms, they just provide the properties of the one and only monitor.


Eveyrything you ever wanted to know about the API for multiple monitors is described in a very well written article by David Campbell in the June 1997 issue of MSJ.

The API itself is simple and straightforward. It has some new constants to pass to GetSystemMetrics and a couple methods to enumerate all of the monitors currently attached to the system and get the properties of a given monitor.

All of the types and functions are defined by the platform SDK in the file multimon.h . If you #define WINVER to be greater than 0x400 you don't need to link to multimon.h, as the API gets defined in windef.h for Windows 98 and later targeted builds. Multimon.h also provides some stub functions that allow calls to be safely made on Windows 95/NT4 machines. These wrappers interrogate the runtime OS and either make fall-through calls into the actual API or return the propeties of the one (and only) monitor on those older platforms.

The multi monitor classes take care of including multimon.h correctly depending on the value of WINVER.

Using the code

CMointor is a basic MFC class that allows you to safely use the multi-monitor API on any Win32 platform. 

There are three classes in this library:

CMonitors represents the collection of monitors currently attached to the system and wraps the EnumDisplayMonitors API function.

//CMonitors' interface
CMonitor GetMonitor( const int index ) const;
int GetCount() const; 

//returns the monitor closest to the specified item
static CMonitor GetNearestMonitor( const LPRECT lprc );
static CMonitor GetNearestMonitor( const POINT pt );
static CMonitor GetNearestMonitor( const CWnd* pWnd );

//is the specificed item visible on any monitor
static BOOL IsOnScreen( const POINT pt );
static BOOL IsOnScreen( const CWnd* pWnd );
static BOOL IsOnScreen( const LPRECT lprc );

//returns the rectangle encompassing all monitors
static void GetVirtualDesktopRect( LPRECT lprc );

//determines whether the given handle is a valid monitor handle
static BOOL IsMonitor( const HMONITOR hMonitor );
static CMonitor GetPrimaryMonitor();
static BOOL AllMonitorsShareDisplayFormat();

static int GetMonitorCount();

CMonitor is a wrapper around an HMONITOR handle (returned from EnumDisplayMonitors) and the GetMonitorInfo function. With CMonitor you can get at the characteristics of a given monitor.

//The interface of CMonitor            
void Attach( const HMONITOR hMonitor );
HMONITOR Detach();

void ClipRectToMonitor( LPRECT lprc, <BR>                        const BOOL UseWorkAreaRect = FALSE ) const;
void CenterRectToMonitor( LPRECT lprc, <BR>                          const BOOL UseWorkAreaRect = FALSE ) const;
void CenterWindowToMonitor( CWnd* const pWnd,<BR>                            const BOOL UseWorkAreaRect = FALSE ) const;

//creates a device context for the monitor - the client is responsible for <BR>// DeleteDC
HDC CreateDC() const;

void GetMonitorRect( LPRECT lprc ) const;
//the work area is the monitor rect minus the start bar
void GetWorkAreaRect( LPRECT lprc ) const;

void GetName( CString& string ) const;

int GetBitsPerPixel() const;

//determines if the specified item on the monitor
BOOL IsOnMonitor( const POINT pt ) const;
BOOL IsOnMonitor( const CWnd* pWnd ) const;
BOOL IsOnMonitor( const LPRECT lprc ) const;

BOOL IsPrimaryMonitor() const;
BOOL IsMonitor() const;

CMonitorDC is a CDC derived class that represents a monitor specific device context. I haven't really gone to far with this class but it seemed like a logical part of the library.  

Known Limitations

CMonitor and CMonitors rely on the assumption that a monitor handle does not change. This has proved to be a safe assumption empirically but isn't nessecarily a guarantee.


  • 02/20/2003 - Initial Release
  • 08/25/2003 - Made changes to make compatible with VC6 environment


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Don Kackman
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10
It only went downhill from there.
Hey look, I've got a blog
Follow on   Twitter

Comments and Discussions

QuestionNice work. PinmemberKim Moung Soo29-May-13 22:29 
AnswerRe: Nice work. PinmemberDon Kackman30-May-13 3:43 
GeneralMy vote of 5 PinmemberKim Moung Soo29-May-13 22:23 
Questionversion PinmemberMember 1005080919-May-13 16:50 
AnswerRe: version PinmemberDon Kackman19-May-13 17:04 
GeneralMy vote of 5 PinmemberGeorgy Meshkov15-Oct-12 2:57 
QuestionMultimon Code Project Licensing PinmemberDonaldJLucas17-May-09 18:40 
AnswerRe: Multimon Code Project Licensing PinmemberDon Kackman18-May-09 7:59 
QuestionRe: Multimon Code Project Licensing PinmemberDonaldJLucas18-May-09 20:17 
AnswerRe: Multimon Code Project Licensing PinmemberDon Kackman19-May-09 3:00 or c# version please ? PinmemberUltraWhack12-May-08 10:43 
AnswerRe: or c# version please ? PinmemberDon Kackman5-Aug-08 2:27 
Questionmirror display Pinmemberfurry2223-Apr-08 2:40 
QuestionMFC Classes Pinmemberkannettan15-Sep-06 21:49 
GeneralBits per pixel Pinmemberakorka17-Jul-06 10:39 
GeneralRe: Bits per pixel PinmemberDon Kackman17-Jul-06 12:02 
GeneralProblem Pinmemberakorka17-Jul-06 9:40 
GeneralSplitterWND question PinmemberKarl Bahr26-Nov-05 7:02 
GeneralNice classes, one fix PinmemberHans Dietrich24-Feb-05 16:48 
GeneralRe: Nice classes, one fix PinmemberDon Kackman19-Mar-05 16:18 
GeneralRe: Nice classes, one fix PinmemberHans Dietrich19-Mar-05 19:43 
GeneralRe: Nice classes, one fix PinmemberFreeDragon8029-May-08 10:22 
GeneralNice! What about turning on/off the second monitor PinmemberAORD17-Feb-05 22:48 
GeneralCMonitor not defined Pinmemberbujal20-Dec-04 14:58 
GeneralRe: CMonitor not defined PinmemberDon Kackman20-Dec-04 15:13 
GeneralRe: CMonitor not defined Pinmemberbujal21-Dec-04 23:39 
GeneralLink errors when MFC statically linked PinmemberTerry7608-Sep-04 10:14 
GeneralRe: Link errors when MFC statically linked PinmemberNeville Franks8-Sep-04 11:35 
GeneralRe: Link errors when MFC statically linked PinsussAnonymous16-Nov-04 16:22 
GeneralRe: Link errors when MFC statically linked Pinmemberleemidgley19-May-07 3:58 
GeneralRe: Link errors when MFC statically linked Pinmemberkifaayet8-Oct-07 7:02 
GeneralRE: Displaying on second monitor PinmemberBK214-Oct-03 8:45 
GeneralRe: RE: Displaying on second monitor PinmemberDon Kackman17-Oct-03 15:14 
GeneralRe: RE: Displaying on second monitor Pinmembermeouvn9-Jun-05 21:02 
GeneralCool PinmemberNormski28-Aug-03 21:57 
GeneralRe: Cool PinmemberDon Kackman29-Aug-03 5:00 
GeneralAdded VC6 project PinmemberDon Kackman28-Aug-03 17:31 
GeneralMy expieriences with VC6 &amp; this class Pinsussfigmo23-Aug-03 18:40 
GeneralRe: My expieriences with VC6 &amp; this class Pinmemberfigmo23-Aug-03 18:59 
GeneralRe: My expieriences with VC6 &amp; this class PinmemberDon Kackman25-Aug-03 7:01 
Questioncan someone send me multimon.cpp ? Pinmembercweng200312-Apr-03 12:51 
AnswerRe: can someone send me multimon.cpp ? PinmemberDon Kackman14-Apr-03 6:28 
Generalproject workspace file does not exist Pinmembercweng200312-Apr-03 12:36 
GeneralRe: project workspace file does not exist PinmemberDon Kackman14-Apr-03 6:25 
GeneralRe: project workspace file does not exist PinmemberDon Kackman14-Apr-03 7:13 
GeneralLinking error PinsussAnonymous6-Mar-03 7:11 
GeneralRe: Linking error PinmemberDon Kackman7-Mar-03 5:50 
GeneralRe: Linking error PinsussAnonymous9-Mar-03 23:28 
GeneralRe: Linking error PinmemberDonders67-Jul-05 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150224.1 | Last Updated 28 Aug 2003
Article Copyright 2003 by Don Kackman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid