Click here to Skip to main content
15,891,905 members
Articles / Desktop Programming / MFC

A drop-in replacement for MessageBox()

Rate me:
Please Sign up or sign in to vote.
4.26/5 (10 votes)
4 Jan 2005CPOL8 min read 62K   1.1K   23  
Other articles describe replacements for MessageBox() with extra buttons for "Yes to All" and "No to All", but you still have to write the code to handle those buttons. This article presents a class that does all the work for you.
#if !defined(AFX_CRHYESNOTOALLDIALOG_H__8B17B6A1_0396_48BE_A4C5_3073B18C3AC7__INCLUDED_)
#define AFX_CRHYESNOTOALLDIALOG_H__8B17B6A1_0396_48BE_A4C5_3073B18C3AC7__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CRHYesNoToAllDialog.h : header file
//

/////////////////////////////////////////////////////////////////////////////

enum
{
    NBUTTONS = 6,               // Yes, No, Yes to All, No to All, Stop, Cancel
    NCONTROLS = NBUTTONS + 1,   // +1 for the Progress Bar
};

#if !(WINVER >= 0x0400)
#define MB_DEFBUTTON4 (MB_DEFBUTTON3 + (MB_DEFBUTTON2 - MB_DEFBUTTON1))
#endif /* !(WINVER >= 0x0400) */
#define MB_DEFBUTTON5 (MB_DEFBUTTON4 + (MB_DEFBUTTON2 - MB_DEFBUTTON1))

/////////////////////////////////////////////////////////////////////////////
// CRHProgressCtrl window

class CRHProgressCtrl : public CProgressCtrl
{
// Construction
public:
	CRHProgressCtrl();

// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CRHProgressCtrl)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CRHProgressCtrl();

	// Generated message map functions
protected:
	//{{AFX_MSG(CRHProgressCtrl)
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
// CRHYesNoToAllDialog dialog

class CRHYesNoToAllDialog : public CDialog
{
private:
    CRHYesNoToAllDialog(CRHYesNoToAllDialog &); // private copy constructor
    
// Construction
public:
	CRHYesNoToAllDialog(bool Topmost, CWnd* pParent = NULL);   // standard constructor - CRH modified
	virtual ~CRHYesNoToAllDialog();

	int CRHMessageBox(LPCTSTR lpszText, LPCTSTR lpszCaption = NULL, UINT nType = MB_OK);
        void CRHUpdateProgressBar(int SoFar, int Total);

// Dialog Data
	//{{AFX_DATA(CRHYesNoToAllDialog)
	//}}AFX_DATA
	enum { IDD = 0 };


// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CRHYesNoToAllDialog)
	public:
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:

	// Generated message map functions
	//{{AFX_MSG(CRHYesNoToAllDialog)
	virtual void OnOK();
	virtual void OnCancel();
	afx_msg void OnPaint();
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	virtual BOOL OnInitDialog();
	afx_msg void OnWindowPosChanged(WINDOWPOS FAR* lpwndpos);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
protected:

private:
	bool CRHEraseBkgndNeeded;
	void CRHSetDefaultButton();
	CButton * pDefaultButton;
	UINT DefaultButtonID;
	void CRHDetermineDefaultButton();
	void CRHFindWindowPosition();
        // all these ints are in pixels ...
	int CRHIconWidthPlusGap;    // 0 if there's no icon
        int CRHIconHeight;          // 0 if there's no icon
        int CRHQuestionWidth;     
        int CRHQuestionHeight;    
        int CRHButtonsTopOriginal;
        int CRHButtonsTop;        
        int CRHButtonsLeft;       
	int CRHMarginsWidth;      
	int CRHMinimumWidth;      
        int CRHMinimumHeight;     
        int CRHLeftMarginWidth;   
        int CRHTopMarginHeight;   
	int CRHCentreX;             // screen coordinates
	int CRHCentreY;             // screen coordinates
	int CRHTop;                 // screen coordinates

        CStatic	c_CStatic_Icon;
	CStatic	c_CStatic_Question;
	CButton	c_CButton_Yes;
	CButton	c_CButton_No;
	CButton	c_CButton_YesToAll;
	CButton	c_CButton_NoToAll;
	CButton	c_CButton_Cancel;
	CButton	c_CButton_Stop;
	CRHProgressCtrl	c_CRHProgressCtrl;

	afx_msg void OnButtonYes();
	afx_msg void OnButtonNo();
	afx_msg void OnButtonYestoall();
	afx_msg void OnButtonNotoall();
	afx_msg void OnButtonCancel();
	afx_msg void OnButtonStop();

        struct CRHButtonInfo
        {
            UINT nID;
            char *Text;
            CButton CRHYesNoToAllDialog::*pCButton;
            DWORD Style;
            int lx;
            int ty;
            int Width;
            int Height;
        };
        static CRHButtonInfo CRHButtonInfos[NBUTTONS];

        struct CRHControlInfo
        {
            CWnd *pControl;
            int left;
            int top;
        };
        CRHControlInfo CRHControlInfos[NCONTROLS];

    	CFont vCFont;

	void CRHAdjustSizes(LPCTSTR lpszText);
	void CRHMoveControls(int DeltaX, int DeltaY);

        CWnd* const CRHpParent;
	bool const CRHTopmost;
	HWND hWndParent;
	BOOL bEnableParent;

	void CRHDisplay();
	bool CRHYesToAll;
	bool CRHNoToAll;
	bool CRHCancel;
	void CRHAutoAnswer(bool Flag);
	bool CRHOnPaintShouldEndModalLoop;

        UINT CRHType;
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CRHYESNOTOALLDIALOG_H__8B17B6A1_0396_48BE_A4C5_3073B18C3AC7__INCLUDED_)

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United Kingdom United Kingdom
I've been programming computers since about 1968. I started at school with Algol 60 on an Elliott 803. From there I progressed through the Z80 and other microprocessors to the PC, DOS and Windows, Pascal, C and C++.

My other interests include astronomy and classical music. All of my contributions to Code Project have arisen from programs I've written in these areas.

Comments and Discussions