Click here to Skip to main content
15,894,896 members
Articles / Desktop Programming / MFC

XGroupBox - an MFC groupbox control to display text and icon

Rate me:
Please Sign up or sign in to vote.
4.98/5 (60 votes)
2 Sep 2008CPOL5 min read 126.1K   4.7K   92  
XGroupBox is an MFC control that displays a flicker-free groupbox with text and/or icon. You can use this to display either a standard groupbox or a header-only groupbox.
// 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