Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Big Number

0.00/5 (No votes)
27 Feb 2011 1  
Displaying big integers in dialogs.

Introduction

I was trying the variable type: __int64 in a simple dialog application and decided to make it useful

so I had it display the address range of 4 x 1 GB RAM chips.

Background

The framework in a simple dialog app. doesn't display strings very well, the spacing of characters is not good.

Therefore, I tried to change the Font to a True Type font. After a few failed attempts I came up with

a different solution, I'll get the string from the Static Window and reformat it to Courier New

and rewrite the string. GetClientRect($rc) gets the dialog box window not the Static Window.

Fine, let's use that. The code below shows the result.

Using the Code

The framework calls OnEraseBkgnd() before painting so I'll do it there.

//
// Change the Font
BOOL CBigNumberDlg::OnEraseBkgnd(CDC* pDC)
{
	CString szStr;
	CWnd *pWnd;
	CSize m_sizeCharScn;
	CRect rct;
	GetClientRect(&rct);
	pDC->FillSolidRect(rct, RGB(135, 206, 250));	// lightsky blue, fill rectangle
	CFont font;
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));
	lf.lfHeight = 18;
	// True Type font for nice digits
	strcpy_s(lf.lfFaceName, sizeof("Courier New"), "Courier New");
	lf.lfWeight = FW_BOLD;
	font.CreateFontIndirect(&lf);
	CFont *pOldFont = (CFont *)pDC->SelectObject(&font);
 
	CBrush brush;
	// lightsky blue for the brush
	brush.CreateSolidBrush(RGB(135, 206, 250));
	CBrush *pOldBrush = pDC->SelectObject(&brush);
		
	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
    
	// Store some useful text metrics
	m_sizeCharScn.cy = tm.tmHeight + tm.tmExternalLeading;
	m_sizeCharScn.cx = tm.tmAveCharWidth; 
	rct.left += m_sizeCharScn.cx * 2;
	rct.top += m_sizeCharScn.cy * 6;
	pDC->SetTextColor(RGB(0,0,96));	// gunmetal blue
	// Copy static text
	for(int index = 0; index < 4; index++)
	{
		switch(index) {
			case 0: pWnd = GetDlgItem(IDC_STATIC_NUM);
				break;
			case 1: pWnd = GetDlgItem(IDC_STATIC_NUM_ONE);
				break;
			case 2:	pWnd = GetDlgItem(IDC_STATIC_NUM_TWO);
				break;
			case 3:	pWnd = GetDlgItem(IDC_STATIC_NUM_THREE);
				break;
		}
		pWnd->GetWindowText(szStr);
		pDC->TextOut(rct.left, rct.top, szStr);
		rct.top += m_sizeCharScn.cy;
	}
	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldFont);
 
	return FALSE;
}

Also, have the program hide the Static Window that code is next:

// Hide Static Text Window
void CBigNumberDlg::OnBnClickedHide()
{
	// TODO: Add your control notification handler code here
	CWnd *pWnd;
	// Hide the four values
	for(int index = 0; index > 4; index++)
	{
		switch(index) {
			case 0: pWnd = GetDlgItem(IDC_STATIC_NUM);
				break;
			case 1: pWnd = GetDlgItem(IDC_STATIC_NUM_ONE);
				break;
			case 2:	pWnd = GetDlgItem(IDC_STATIC_NUM_TWO);
				break;
			case 3:	pWnd = GetDlgItem(IDC_STATIC_NUM_THREE);
				break;
		}
		Sleep(250);	// Just so you can watch them get zapped.
		pWnd->ShowWindow(SW_HIDE);
	}
}

Points of Interest

CString.Format(...)is a little scrary till you read the Help Library.

szStr.Format(_T("%0.10I64d = 0x0%0.8I64X to (%0.10I64d)-1 = 0x0%0.8I64X"),
    m_nLow, m_nLow, m_nHigh, m_nHigh-1);

History

BigNumber Version 1.0 Feb, 28, 2011

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