From: Jeff Armstrong (
[email protected])
Subject: Re: Progress Bar
View: Complete Thread (9 articles)
Original Format
Newsgroups: comp.os.ms-windows.programmer.tools.mfc, comp.os.ms-windows.programmer.win32, microsoft.public.win32.programmer.ui
Date: 2000/05/03
I wrote a progress bar class that displays the progress as a solid bar with
a percentage XOR'd with the bar color as the progress increases. (I didn't
like the CProgressBar provided by MFC) Following is the code that draws the
actual drawing. I can give you (or anyone else in this newsgroup) a copy of
the entire class if you would like to EMail me at
[email protected]
void CProgressBar::DrawBar(CDC &cdcBar)
{
CBrush cbrBrush,
cbrBar;
CRect rctColor,
rctClient;
WORD wCx,
wRange;
CFont cfFont;
CDC cdcBase,
cdcMem;
CBitmap cbmpBase, // BMP for memory DC
cbmpText; // BMP for memory DC
ASSERT_VALID(this);
// Get dimensions of progress bar
GetClientRect(&rctClient);
// Create a bitmap compatible with the screen.
// This will be used to blit the completed progress bar and thus prevent any
flicker.
cdcBase.CreateCompatibleDC(&cdcBar);
cdcBase.SaveDC();
// Create a bitmap compatible with the screen
cbmpBase.CreateCompatibleBitmap(&cdcBar, rctClient.right,
rctClient.bottom);
cdcBase.SelectObject(cbmpBase);
// Create a white canvass to draw on
cdcBase.SelectObject(GetStockObject(WHITE_BRUSH));
cdcBase.SelectObject(GetStockObject(WHITE_PEN));
cdcBase.Rectangle(rctClient);
// Calculate how far to display color
wRange = m_wEnd - m_wStart;
rctColor = rctClient;
wCx = (WORD)(((float)(m_wPosition) * rctClient.right)/wRange + 0.5);
// Now draw the color portion
rctColor.right = rctColor.left + wCx;
cbrBar.CreateSolidBrush(m_rgbColor);
cdcBase.FillRect(&rctColor, &cbrBar);
// Display the percentage complete
CString cszPct;
cszPct.Format(_T("%d %%"), (m_wPosition*100)/wRange);
// Draw text to a bitmap in memory
cdcMem.CreateCompatibleDC(&cdcBase);
cdcMem.SaveDC();
// Create a bitmap compatible with the screen
cbmpText.CreateCompatibleBitmap(&cdcBase, rctClient.right,
rctClient.bottom);
cdcMem.SelectObject(cbmpText);
// Make our memory bitmap all white
cdcMem.SelectObject(GetStockObject(WHITE_BRUSH));
cdcMem.SelectObject(GetStockObject(WHITE_PEN));
cdcMem.Rectangle(rctClient);
// Select a 10-point Times New Roman font
cfFont.CreatePointFont(100, _T("Times New Roman"), &cdcBase);
cdcMem.SelectObject(&cfFont);
// Give our text a transparent background
cdcMem.SetBkMode(TRANSPARENT);
cdcMem.SetTextColor(m_rgbColor);
// Finally, draw our text
cdcMem.DrawText(
cszPct,
cszPct.GetLength(),
rctClient,
DT_CENTER | DT_SINGLELINE | DT_VCENTER);
// Now blit the text bitmap to the bar
// NOTE: We want colored text to change to white as the progress
// bar moves underneath it. The ternary raster code for
// this is DSxn, index 99 in the ternary raster code table.
cdcBase.BitBlt(
0, 0,
rctClient.right, rctClient.bottom,
&cdcMem,
0, 0,
0x00990066 // DSxn raster code
);
// Blit the completed progress bar to the destination DC
cdcBar.BitBlt(
0, 0,
rctClient.right, rctClient.bottom,
&cdcBase,
0, 0,
SRCCOPY // Blit source to dest. DC
);
// Restore our original device context
cdcMem.RestoreDC(-1);
cdcBase.RestoreDC(-1);
}
Jeff Armstrong
Sr. Programmer
ProModel Corporation
"Michael D. Ober" <
[email protected]> wrote in message
news:
[email protected]...
> How do I put a XOR'd percent complete on top of a progress bar control.
> Win98's setup program contains an example where the progress bar shows a
> graphic status and the actual percent complete is displayed right on top of
> the progress bar.
>
> Thanks,
> Mike Ober.
This might be a good start.
Better would be to contact Jeff Armstrong,
[email protected], and ask him to send you the complete code.