Click here to Skip to main content
15,889,216 members
Articles / Desktop Programming / MFC

Big Number

Rate me:
Please Sign up or sign in to vote.
2.33/5 (4 votes)
27 Feb 2011CPOL 29K   208   2   16
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(<code>$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.

C++
//
// 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:

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Ajay Vijayvargiya2-Mar-11 6:05
Ajay Vijayvargiya2-Mar-11 6:05 
GeneralRe: My vote of 2 Pin
Roger652-Mar-11 12:29
Roger652-Mar-11 12:29 
GeneralRe: My vote of 2 Pin
Ajay Vijayvargiya2-Mar-11 16:28
Ajay Vijayvargiya2-Mar-11 16:28 
GeneralRe: My vote of 2 Pin
Roger652-Mar-11 22:35
Roger652-Mar-11 22:35 
Open the file in your favorite text editor and you will see on the top line: "format version 11", change that value and ..............
Old dog learning new tricks!

GeneralBigNumber Pin
Roger651-Mar-11 18:38
Roger651-Mar-11 18:38 
GeneralCannot extract the archive Pin
Maximilien28-Feb-11 2:57
Maximilien28-Feb-11 2:57 
GeneralRe: Cannot extract the archive Pin
Roger6528-Feb-11 3:10
Roger6528-Feb-11 3:10 
GeneralRe: Cannot extract the archive Pin
Maximilien28-Feb-11 3:17
Maximilien28-Feb-11 3:17 
GeneralRe: Cannot extract the archive Pin
Roger6528-Feb-11 3:30
Roger6528-Feb-11 3:30 
GeneralRe: Cannot extract the archive Pin
Roger6528-Feb-11 3:48
Roger6528-Feb-11 3:48 
GeneralRe: Cannot extract the archive Pin
Maximilien28-Feb-11 7:34
Maximilien28-Feb-11 7:34 
GeneralRe: Cannot extract the archive Pin
Roger6528-Feb-11 9:19
Roger6528-Feb-11 9:19 
GeneralRe: Cannot extract the archive Pin
Maximilien1-Mar-11 3:34
Maximilien1-Mar-11 3:34 
GeneralRe: Cannot extract the archive Pin
Roger651-Mar-11 5:04
Roger651-Mar-11 5:04 
GeneralMy vote of 1 Pin
SteveKing27-Feb-11 21:16
SteveKing27-Feb-11 21:16 
GeneralRe: My vote of 1 Pin
Roger6527-Feb-11 23:43
Roger6527-Feb-11 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.