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

Dual / Multiple Monitor Support in Win32

, 29 Jul 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Asim Goheer

United States United States
No Biography provided

Comments and Discussions

 
GeneralThank for your advise, I read it all PinmemberJoe Zeng19-Jul-10 16:18 
Smile | :) Maybe my experience is limited, I still can't write my project about this problem, if there's nothing impact to me, could you send me your project to, in your artical, I found nothing could be download about this problem, thank you very much.
GeneralRe: Thank for your advise, I read it all PinmemberJoe Zeng19-Jul-10 16:20 
GeneralPrimary Vs Seconday Monitor PinmemberAKG22-Mar-07 20:38 
GeneralCool screensaver but... PinmemberDarren Schroeder31-Jul-04 3:41 
GeneralRe: Cool screensaver but... PinmemberAsim Goheer31-Jul-04 8:55 
GeneralRe: Cool screensaver but... [modified] PinmemberChuck Nicholson16-Mar-11 13:14 

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 | Mobile
Web02 | 2.8.140821.2 | Last Updated 30 Jul 2004
Article Copyright 2004 by Asim Goheer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid