Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Debug With Apps Displayed on Secondary Monitor

, 29 Nov 2006
Here's some code that will automatically start your app on a secondary monitor if it's compiled with debug info.
// SecondaryDebug.cpp : Defines the class behaviors for the application.

#include "stdafx.h"
#include "SecondaryDebug.h"
#include "MainFrm.h"

#include "SecondaryDebugDoc.h"
#include "SecondaryDebugView.h"

#ifdef _DEBUG
#define new DEBUG_NEW

// CSecondaryDebugApp

BEGIN_MESSAGE_MAP(CSecondaryDebugApp, CWinApp)
	ON_COMMAND(ID_APP_ABOUT, &CSecondaryDebugApp::OnAppAbout)
	// Standard file based document commands

// CSecondaryDebugApp construction

	// TODO: add construction code here,
	// Place all significant initialization in InitInstance

// The one and only CSecondaryDebugApp object

CSecondaryDebugApp theApp;

// CSecondaryDebugApp initialization

BOOL CSecondaryDebugApp::InitInstance()
	InitCtrls.dwSize = sizeof(InitCtrls);
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
	if (!pDocTemplate)
		return FALSE;
	CCommandLineInfo cmdInfo;
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it

	// Our code starts

	// The object of this code is to cause the application to display on the 
	// first secondary monitor we find, but only if we're in _DEBUG mode. If 
	// we're nbot in debug mode, the app will display maximized on the 
	// primary monitor.
#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.

	// Our code ends - our special functions are found at the end of this file.

	return TRUE;

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

void CAboutDlg::DoDataExchange(CDataExchange* pDX)


// App command to run the dialog
void CSecondaryDebugApp::OnAppAbout()
	CAboutDlg aboutDlg;

// CSecondaryDebugApp message handlers

// make the app display on the secondary monitor if we're in debug mode

// 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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for 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

John Simmons / outlaw programmer
Software Developer (Senior)
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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 29 Nov 2006
Article Copyright 2006 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid