Click here to Skip to main content
14,299,664 members

Debug With Apps Displayed on Secondary Monitor

Rate this:
4.54 (20 votes)
Please Sign up or sign in to vote.
4.54 (20 votes)
29 Nov 2006
Here's some code that will automatically start your app on a secondary monitor if it's compiled with debug info.


I'm working on a project that needs to be debugged on a dual monitor system. Fortunately, I have such a system at work. While dragging the application window to the secondary monitor for the umpteenth billion time, I thought to myself that it would be really cool if I could get the application to display itself on the secondary monitor automatically while running in debug mode.

The Code

All of the work we need to do is in the application's .CPP and .H files. To start things off, add the following code to your application's .H file:

class CSecondaryDebugApp : public CWinApp
    CRect m_secondaryRect;
    void UseSecondaryMonitor();

That code simply defines a member variable that receives the secondary monitor's rectangle, and the function prototype for the code that actually does the work of finding your secondary monitor.

Next, we have to add some code to OnInitInstance():

BOOL CSecondaryDebugApp::InitInstance()
#ifdef _DEBUG

    // if we don't have a secondary monitor, go ahead and maximize the window
    if (m_secondaryRect.Width() + m_secondaryRect.Height() == 0)
    // otherwise, use the rectange we got from UseSecondaryMonitor()
        m_pMainWnd->MoveWindow(m_secondaryRect, TRUE);
    // The one and only window has been initialized, so show and update it.

    return TRUE;

Here, we call our monitor finding method, and then react based on what the method found.

Finally, we add the meat of the code to the end of our .CPP file:

// a global CRect variable to hold the rectangle found by the callback function
CRect secondaryRect(0,0,0,0);

// This callback function is called by EnumDisplayMonitors, and this is where 
// we snatch our secondary monitor rectangle.
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,  // handle to display monitor
                              HDC hdc1,           // handle to monitor DC
                              LPRECT lprcMonitor, // monitor intersection 
                                                  // rectangle
                              LPARAM data)        // data
    RECT rc = *lprcMonitor;

    mInfo.cbSize = sizeof(mInfo);
    ::GetMonitorInfo(hMonitor, &mInfo);

    if (mInfo.dwFlags != MONITORINFOF_PRIMARY)
        // for purposes of example we always take the first secondary monitor 
        // we find.  I leave it as an exercise for the programmer to change 
        // the code to support multiple secondary displays (and therefore 
        // pick the desired monitor
        secondaryRect = mInfo.rcWork;
        // return 0 to stop the enumeration
        return 0;
    // if we get here, the current monitor isn't a secondary monitor
    return 1;

// This is the function we call to enumerate through all
// monitors attached to the system.
void CSecondaryDebugApp::UseSecondaryMonitor() 
    // init the memebr rectangle
    // enumerate
    ::EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, 0);
    // store the secondary monitor's rectangle in our member rectangle
    m_secondaryRect = secondaryRect;
    // at this point, we are ready to display the application's window

The UseSecondaryMonitor() function simply initializes the member rectangle, enumerates the available monitors, and sets the member rectangle variable. Of course, you could choose to put these three lines of code into the OnInitInstance() function, but that's not how *I* chose do it.

The callback function checks the dwFlags field of the MONITORINFOEX structure to see if this is not the primary monitor, and if it isn't, it grabs the work rectangle (the rectangle available to the application) and returns 0 to stop the enumeration (remember, this example is only looking for the first secondary monitor on your system).

End Result

When you compile the program with debug info, running the program will cause the program to display itself on the secondary monitor. You could easily expand this concept to release versions that will re-display themselves on whatever monitor they were last displayed on.

Have fun.


This article is meant to illustrate how to find a secondary monitor on your system using the EnumerateDisplayMonitors() and GetMonitorInfo() functions. As usual, the bulk of the info I found came from MSDN. Most of my articles are ripped directly out of a project I'm working on, so along with the "how" aspects, you usually get a real life example of why I developed the code.

I don't expect many here would have the same requirements that I do, and I can't be expected to divine everyone's needs in advance. Since you're all programmers (or at least claim to be), I leave it to you to adapt this code to your own project.

Finally, if this article inspires you to more fully investigate multiple monitor systems (one user here has six monitors on three different video cards in one box), by all means, write an article about it!


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


About the Author

Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

GeneralMy vote of 5 Pin
pulireddy13-Oct-10 5:10
memberpulireddy13-Oct-10 5:10 
General[Message Removed] Pin
immetoz6-Oct-08 7:53
memberimmetoz6-Oct-08 7:53 
QuestionHow about mirror display? Pin
furry2223-Apr-08 1:37
memberfurry2223-Apr-08 1:37 
General.NET Version Pin
Miszou20-Apr-07 7:26
memberMiszou20-Apr-07 7:26 
GeneralThanks for showing how Pin
Jim Reye27-Mar-07 20:51
memberJim Reye27-Mar-07 20:51 
GeneralPrimary Vs Secodary monitor Pin
AKG22-Mar-07 20:25
memberAKG22-Mar-07 20:25 
GeneralSave Location Pin
Howard Birkett29-Nov-06 23:02
memberHoward Birkett29-Nov-06 23:02 
GeneralRe: Save Location Pin
#realJSOP29-Nov-06 23:09
mve#realJSOP29-Nov-06 23:09 
GeneralRe: Save Location Pin
#realJSOP30-Nov-06 2:19
mve#realJSOP30-Nov-06 2:19 
GeneralVS on secondary Pin
PJ Arends29-Nov-06 14:01
professionalPJ Arends29-Nov-06 14:01 
GeneralRe: VS on secondary Pin
#realJSOP29-Nov-06 23:23
mve#realJSOP29-Nov-06 23:23 

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.

Posted 29 Nov 2006


29 bookmarked