Click here to Skip to main content
12,824,787 members (40,571 online)
Click here to Skip to main content


61 bookmarked
Posted 7 Jul 2003

Modeless Dialog Management

, 7 Jul 2003
A simple method for tracking and dealing with multiple modeless dialogs.
#pragma once
	// this class combined with the following helper class provides a simple way to deal with
	// modeless dialogs.  See the example at the end of the file for implementation details.
class ModelessDialogHelper;

class ModelessDialogTracker  
	virtual ~ModelessDialogTracker();

	BOOL IsAlreadyPopped() const;			// call before creating.  If true, don't bother (it will be set to active)
	CDialog* GetDlg() const;				// allows direct (with casting) access to dialog.

	virtual void OnDialogClosed() {}		// override if you want to do something when closed.
	void CloseDialog();						// call to forcibly close dialog (automatic when destructed)
	BOOL IsPopped() const;			// just return the status of the dialog pointer.

	friend ModelessDialogHelper;			// so the helper can access the start and stop functions.
	CDialog* pDlg;								// pointer to modeless dialog (or NULL)
	void StartTracking( CDialog& dlg );	// called by ModelessDialogHelper constructor
	void StopTracking();						// called by ModelessDialogHelper destructor

class ModelessDialogHelper
	ModelessDialogHelper(ModelessDialogTracker& tracker, CDialog& dlg);  // sets the tracked pointer.

	virtual ~ModelessDialogHelper();  // clears the tracked pointer.
	ModelessDialogTracker* pDlgTracker;

#pragma warning( disable : 4355 )  // the warning given when passing "this" to a member constructor.
												// in the case of the helper class, we WANT to do this.


///////////////////////////// Simple usage example///////////////////////////////////////////

// 1) instantiate a tracker object somewhere you want to track the dialog (document or app for example)
	ModelessDialogTracker TrackMrM;

// 2) when you create and pop your dialog, use the tracker to test if its already up:
//    if not, create it, passing the tracker to the constructor, then show it.
		if (TrackMrM.IsAlreadyPopped())

		MrModeless* pnewdlg = new MrModeless(TrackMrM);	// note the passing of the tracker.

// 3) Derive your dialog class (Mr. Modeless in this case) from ModelessDialogHelper and add some stuff to the header:
	class MrModeless : public CDialog, ModelessDialogHelper
	// Construction
		// stuff specific to the modeless dialog.
		MrModeless(ModelessDialogTracker& tracker);   
		BOOL Create(UINT nID, CWnd* pWnd)           	
			{ return CDialog::Create(nID,pWnd);	}
		void PostNcDestroy()
			{ delete this; }
		void OnCancel()								// make sure dialog is only closed with DestroyWindow.. not CDialog::OnOK or CDialog::OnCancel
			{ DestroyWindow(); }

// 4) also make some changes to the constructor in the cpp file:
	MrModeless::MrModeless(ModelessDialogTracker& tracker)
	: ModelessDialogHelper(tracker, *this)
			// NOTE: the ClassWizard will add member initialization here

// When the helper (and dialog) is constructed, it sets the pointer in the tracker to the dialog pointer.
// When the helper (and dialog) is destructed, it clears the pointer.
// All the preamble constructors are needed to let each class know about the others.
// Nothing needs to be done in the class or whatever is holding the ModelessDialogTracker object.
// When it is deleted, it will close the dialog.  You can do a few things with it if you want, however:
		TrackMrM.CloseDialog();		// force it to close.

		((MrModeless*)TrackMrM.GetDlg())->SomeFunctionInTheDialog(data);		// access dialog directly.

		TrackMrM::OnDialogClosed()				// override a virtual to sense when it is closed.
			// do some stuff (like lightup a button or whatever) after the dialog closes.


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

Joe Calkins
Web Developer
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 8 Jul 2003
Article Copyright 2003 by Joe Calkins
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid