Click here to Skip to main content
15,888,133 members
Articles / Desktop Programming / Win32

XMessageBox - A reverse-engineered MessageBox()

Rate me:
Please Sign up or sign in to vote.
4.95/5 (175 votes)
29 Nov 2008CPOL28 min read 969.7K   10.1K   432  
A reverse-engineered non-MFC MessageBox() that includes custom checkboxes.
// ROEdit.cpp : implementation file
//

#include "stdafx.h"
#include "ROEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma warning(disable : 4996)			// disable bogus deprecation warning

/////////////////////////////////////////////////////////////////////////////
// CROEdit

BEGIN_MESSAGE_MAP(CROEdit, CEdit)
	//{{AFX_MSG_MAP(CROEdit)
	ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CROEdit::CROEdit()
{
	m_brush.CreateSolidBrush(GetSysColor(COLOR_WINDOW));
}

CROEdit::~CROEdit()
{
	if (m_brush.GetSafeHandle())
		m_brush.DeleteObject();
}

/////////////////////////////////////////////////////////////////////////////
// CROEdit message handlers

HBRUSH CROEdit::CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/) 
{
	return (HBRUSH)m_brush; //GetStockObject(WHITE_BRUSH);
	//return NULL;
}

void CROEdit::SetWindowText(LPCTSTR lpszString)
{
	CRect rect;
	GetClientRect(&rect);

	if (GetStyle() & WS_HSCROLL)
	{
		// not wrapped, so see if we can dispense with horz scroll bar

		CDC *pDC = GetDC();

		if (pDC)
		{
			CFont *pFont = GetParent()->GetFont();
			CFont *pOldFont = pDC->SelectObject(pFont);
			int nMaxExtent = 0;
			TCHAR *cp = (TCHAR *) lpszString;
			TCHAR buf[500];
			CString s = _T("");
			while (cp)
			{
				TCHAR *cp1 = cp;
				if (*cp1 == _T('\n'))
					cp1 += 1;
				cp = _tcschr(cp, _T('\r'));
				if (cp)
				{
					int len = cp - cp1;
					cp += 1;
					int n = sizeof(buf)/sizeof(TCHAR);
					if (len >= n)
						len = n-1;
					_tcsncpy(buf, cp1, len);
					buf[len] = 0;
					s = buf;
					s.Replace(_T("\t"), _T("  "));
					CSize size = pDC->GetTextExtent(buf);
					if (size.cx > nMaxExtent)
						nMaxExtent = size.cx;
					//TRACE(_T("size.cx=%d  buf=<%s>\n"), size.cx, buf);
				}
			}

			//TRACE(_T("nMaxExtent=%d\n"), nMaxExtent);

			int w = rect.Width();

			if (GetStyle() & WS_VSCROLL)
				w -= ::GetSystemMetrics(SM_CXVSCROLL);
			//TRACE(_T("w=%d\n"), w);

			if (nMaxExtent < w)
			{
				// ok to remove horz scrollbar
				ModifyStyle(WS_HSCROLL, 0);
			}

			if (pOldFont)
				pDC->SelectObject(pOldFont);
			ReleaseDC(pDC);
		}
	}

	CEdit::SetWindowText(lpszString);

	SetWindowPos(0, 0, 0, 0, 0, 
		SWP_NOZORDER|SWP_NOSIZE|SWP_NOMOVE/*|SWP_FRAMECHANGED*/|SWP_DRAWFRAME);
}

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
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions