Click here to Skip to main content
12,818,315 members (34,202 online)
Click here to Skip to main content


23 bookmarked
Posted 4 Jan 2005

A drop-in replacement for MessageBox()

, 4 Jan 2005
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_)

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


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

#if !(WINVER >= 0x0400)
#endif /* !(WINVER >= 0x0400) */

// CRHProgressCtrl window

class CRHProgressCtrl : public CProgressCtrl
// Construction

// Attributes

// Operations

// Overrides
	// ClassWizard generated virtual function overrides

// Implementation
	virtual ~CRHProgressCtrl();

	// Generated message map functions
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);


// CRHYesNoToAllDialog dialog

class CRHYesNoToAllDialog : public CDialog
    CRHYesNoToAllDialog(CRHYesNoToAllDialog &); // private copy constructor
// Construction
	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
	enum { IDD = 0 };

// Overrides
	// ClassWizard generated virtual function overrides
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation

	// Generated message map functions
	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);

	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;

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


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

Chris Hills
Software Developer (Senior) Sepura plc
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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 4 Jan 2005
Article Copyright 2005 by Chris Hills
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid