Click here to Skip to main content
13,901,646 members
Click here to Skip to main content
Add your own
alternative version

Stats

23.3K views
188 downloads
2 bookmarked
Posted 27 Feb 2011
Licenced CPOL

Big Number

, 27 Feb 2011
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

Roger65
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 2 Pin
Ajay Vijayvargiya2-Mar-11 6:05
memberAjay Vijayvargiya2-Mar-11 6:05 
GeneralRe: My vote of 2 Pin
Roger652-Mar-11 12:29
memberRoger652-Mar-11 12:29 
GeneralRe: My vote of 2 Pin
Ajay Vijayvargiya2-Mar-11 16:28
memberAjay Vijayvargiya2-Mar-11 16:28 
GeneralRe: My vote of 2 Pin
Roger652-Mar-11 22:35
memberRoger652-Mar-11 22:35 
GeneralBigNumber Pin
Roger651-Mar-11 18:38
memberRoger651-Mar-11 18:38 
GeneralCannot extract the archive Pin
Maximilien28-Feb-11 2:57
memberMaximilien28-Feb-11 2:57 
GeneralRe: Cannot extract the archive Pin
Roger6528-Feb-11 3:10
memberRoger6528-Feb-11 3:10 
GeneralRe: Cannot extract the archive Pin
Maximilien28-Feb-11 3:17
memberMaximilien28-Feb-11 3:17 
GeneralRe: Cannot extract the archive Pin
Roger6528-Feb-11 3:30
memberRoger6528-Feb-11 3:30 
GeneralRe: Cannot extract the archive Pin
Roger6528-Feb-11 3:48
memberRoger6528-Feb-11 3:48 
GeneralRe: Cannot extract the archive Pin
Maximilien28-Feb-11 7:34
memberMaximilien28-Feb-11 7:34 
GeneralRe: Cannot extract the archive Pin
Roger6528-Feb-11 9:19
memberRoger6528-Feb-11 9:19 
GeneralRe: Cannot extract the archive Pin
Maximilien1-Mar-11 3:34
memberMaximilien1-Mar-11 3:34 
GeneralRe: Cannot extract the archive Pin
Roger651-Mar-11 5:04
memberRoger651-Mar-11 5:04 
GeneralMy vote of 1 Pin
SteveKing27-Feb-11 21:16
memberSteveKing27-Feb-11 21:16 
GeneralRe: My vote of 1 Pin
Roger6527-Feb-11 23:43
memberRoger6527-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 27 Feb 2011
Article Copyright 2011 by Roger65
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid