Click here to Skip to main content
Licence CPOL
First Posted 27 Feb 2011
Views 7,375
Downloads 64
Bookmarked 2 times

Big Number

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

About the Author

Roger65



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmvpAjay Vijayvargiya6:05 2 Mar '11  
GeneralRe: My vote of 2 PinmemberRoger6512:29 2 Mar '11  
GeneralRe: My vote of 2 PinmvpAjay Vijayvargiya16:28 2 Mar '11  
GeneralRe: My vote of 2 PinmemberRoger6522:35 2 Mar '11  
GeneralBigNumber PinmemberRoger6518:38 1 Mar '11  
GeneralCannot extract the archive PinmemberMaximilien2:57 28 Feb '11  
GeneralRe: Cannot extract the archive PinmemberRoger653:10 28 Feb '11  
GeneralRe: Cannot extract the archive PinmemberMaximilien3:17 28 Feb '11  
GeneralRe: Cannot extract the archive PinmemberRoger653:30 28 Feb '11  
GeneralRe: Cannot extract the archive PinmemberRoger653:48 28 Feb '11  
GeneralRe: Cannot extract the archive PinmemberMaximilien7:34 28 Feb '11  
GeneralRe: Cannot extract the archive PinmemberRoger659:19 28 Feb '11  
GeneralRe: Cannot extract the archive PinmemberMaximilien3:34 1 Mar '11  
GeneralRe: Cannot extract the archive PinmemberRoger655:04 1 Mar '11  
GeneralMy vote of 1 PinmemberSteveKing21:16 27 Feb '11  
GeneralRe: My vote of 1 PinmemberRoger6523:43 27 Feb '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120517.1 | Last Updated 27 Feb 2011
Article Copyright 2011 by Roger65
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid