Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / MFC

Modeless Dialog Management

Rate me:
Please Sign up or sign in to vote.
4.71/5 (25 votes)
7 Jul 20035 min read 148.4K   6.1K   62  
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  
{
public:
	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.
private:
	CDialog* pDlg;								// pointer to modeless dialog (or NULL)
	void StartTracking( CDialog& dlg );	// called by ModelessDialogHelper constructor
	void StopTracking();						// called by ModelessDialogHelper destructor
};

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

	virtual ~ModelessDialogHelper();  // clears the tracked pointer.
private:
	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.

#ifdef JUST_FOR_EXAMPLE

/////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// 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())
			return;

		MrModeless* pnewdlg = new MrModeless(TrackMrM);	// note the passing of the tracker.
		pnewdlg->Create(pnewdlg->IDD,NULL)		
	}

// 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
	public:
		///////////////////////////////////////////
		// 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.
		}


#endif

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.

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
Web Developer
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