大数
在对话框中显示大整数。
引言
我尝试使用变量类型:__int64
在一个简单的对话框应用程序中,并决定使其有用。
所以我让它显示 4 块 1 GB RAM 芯片的地址范围。
背景
在简单的对话框应用程序中,框架不太擅长显示字符串,字符的间距不好。
因此,我尝试将Font
更改为 True Type 字体。经过几次失败的尝试,我得出了
一个不同的解决方案,我将从Static Window
获取字符串,并将其重新格式化为Courier New
并重写字符串。GetClientRect(
获取的是对话框窗口,而不是$rc
)Static Window
。
好吧,让我们使用它。下面的代码显示了结果。
Using the Code
框架在绘制之前调用OnEraseBkgnd()
,所以我将在那里执行它。
//
// 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;
}
另外,让程序隐藏代码之后的Static Window
// 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);
}
}
关注点
CString.Format(...)
在阅读帮助库之前有点吓人。
szStr.Format(_T("%0.10I64d = 0x0%0.8I64X to (%0.10I64d)-1 = 0x0%0.8I64X"), m_nLow, m_nLow, m_nHigh, m_nHigh-1);
历史
大数字版本 1.0 2011 年 2 月 28 日