Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C++
Article

Dual / Multiple Monitor Support in Win32

Rate me:
Please Sign up or sign in to vote.
4.11/5 (13 votes)
29 Jul 20042 min read 76.4K   35   6
How to add dual / multiple monitor support to your Win32 programs.

Screenshot taken of GXTS on dual monitor system http://www.goheer.com

Introduction

When I was writing the GXTS screensaver for http://www.goheer.com, one of the problems I faced was support for multiple monitors. My goal was to display the same image on all monitors connected to the system regardless of their number.

There are many articles available online relating to multiple monitors, all of which provided no help to me. Somewhere someone is using ::GetSystemMetrics, while another is registering self made classes just to make the multiple monitor thing work. That lead me to go and search for an easy solution as I was not using MFC or any other fancy library. In the end, I was very surprised to find an easy solution in MSDN library. Let's take a look at the solution.

Background

Basic knowledge of Win32 API.

Using the code

Normally, when you are handling the WM_PAINT event, you do this... catch the WM_PAINT and draw whatever you have to in that block.

case WM_PAINT:
{
    PAINTSTRUCT ps = {0};
    HDC hdcE = BeginPaint(hWnd, &ps );

    // Draw your stuff here

    EndPaint(hWnd, &ps);
}
break;

OK, now if we have 2+ monitors and you want to call the paint procedure for each one of it, just use the EnumDisplayMonitors function and supply a callback function where you will be doing painting for each monitor. In this case, I am supplying the callback function MyPaintEnumProc.

case WM_PAINT:
{
    PAINTSTRUCT ps = {0};
    HDC hdcE = BeginPaint(hWnd, &ps );
    // call the function for enumerating all the monitors
    EnumDisplayMonitors(hdcE,NULL, MyPaintEnumProc, 0);

    EndPaint(hWnd, &ps);
}
break;

Now, let's examine the callback function MyPaintEnumProc. By supplying a HDC as first param and NULL as lprect in second argument, the system will call our callback function for each monitor. Every time hMonitor will be the handle to the monitor for which that function is called, hdcl will be the HDC to that monitor, and lprcMonitor will be monitor intersection rectangle. Lastly, in our case, data will be null because we supplied 0 as last argument to EnumDisplayMonitors.

BOOL CALLBACK MyPaintEnumProc(
          HMONITOR hMonitor,  // handle to display monitor
      HDC hdc1,     // handle to monitor DC
      LPRECT lprcMonitor, // monitor intersection rectangle
      LPARAM data       // data
      )
{
    RECT rc = *lprcMonitor;
    // you have the rect which has coordinates of the monitor

    // Draw here now
    return 1;
}

Remember to return true in MyPaintEnumProc to enumerate through all monitors. If you returned false, the enumeration will stop at that point.

Points of Interest

If you think the screenshot of the screensaver "GXTS" is cool then try it and let me know about your comments. It's available free on http://www.goheer.com.

History

First release of this code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThank for your advise, I read it all Pin
Joe Zeng19-Jul-10 16:18
Joe Zeng19-Jul-10 16:18 
GeneralRe: Thank for your advise, I read it all Pin
Joe Zeng19-Jul-10 16:20
Joe Zeng19-Jul-10 16:20 
GeneralPrimary Vs Seconday Monitor Pin
AKG22-Mar-07 20:38
AKG22-Mar-07 20:38 
GeneralCool screensaver but... Pin
Darren Schroeder31-Jul-04 3:41
Darren Schroeder31-Jul-04 3:41 
GeneralRe: Cool screensaver but... Pin
Asim Goheer31-Jul-04 8:55
Asim Goheer31-Jul-04 8:55 
GeneralRe: Cool screensaver but... [modified] Pin
Roland426916-Mar-11 13:14
Roland426916-Mar-11 13:14 

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

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