Introduction
There are many occasions where it's nice to have a popup window that shows the progress of a lengthy operation. Incorporating a dialog resource with a progress control and cancel button, then linking up the control messages for every project you wish to have the progress window can get monotonous and messy.
The class CProgressWnd
is a simple drop in window that contains a progress control, a cancel button and a text area for messages. The text area can display 4 lines of text as default, although this can be changed using CProgressWnd::SetWindowSize()
(below)
Construction
CProgressWnd();
CProgressWnd(CWnd* pParent, LPCTSTR strTitle, BOOL bSmooth=FALSE);
BOOL Create(CWnd* pParent, LPCTSTR strTitle, BOOL bSmooth=FALSE);
Construction is either via the constructor or a two-step process using the constructor and the Create
function. pParent
is the parent of the progress window,strTitle
is the window caption title. bSmooth
will only be effective if you have the header files and commctrl32.dll from IE 3.0 or above (no problems for MS VC 5.0). It specifies whether the progress bar will be smooth or chunky.
Operations
BOOL GoModal(LPCTSTR strTitle = _T("Progress"), BOOL bSmooth=FALSE);
int SetPos(int nPos);
int OffsetPos(int nPos);
int SetStep(int nStep);
int StepIt();
void SetRange(int nLower, int nUpper, int nStep = 1);
void Hide();
void Show();
void Clear();
void SetText(LPCTSTR fmt, ...);
BOOL Cancelled()
void SetWindowSize(int nNumTextLines, int nWindowWidth = 390);
void PeekAndPump(BOOL bCancelOnESCkey = TRUE);
The PeekAndPump
function allows messages to be pumped during long operations. The first parameter allows the window to be cancelled by pressing the ESC key.
You can also make the window modal by creating the window and calling GoModal()
. This will disable the main window, and re-enable the main window when this window is destroyed. See the demo app for example code.
The window will also store and restore its position to and from the registry between incantations.
To use the window, just do something like:
CProgressWnd wndProgress(this, "Progress");
wndProgress.SetRange(0,5000);
wndProgress.SetText("Processing...");
for (int i = 0; i < 5000; i++) {
wndProgress.StepIt();
wndProgress.PeekAndPump();
if (wndProgress.Cancelled()) {
MessageBox("Progress Cancelled");
break;
}
}
or it can be done two stage as:
CProgressWnd wndProgress;
if (!wndProgress.Create(this, "Progress"))
return;
wndProgress.SetRange(0,5000);
wndProgress.SetText("Processing...");
History
- 13 Apr 2002 - added
SaveSettings
call to OnCancel
. Updated project for VC.NET.
- 22 Apr 2002 - minor mods by Luke Gabello
Chris Maunder is the co-founder of
CodeProject, DeveloperMedia and ContentLab, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.
In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project,
CodeProject.AI.
In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. While at CodeProject, Chris' roles included Architecture and coding, Product Development, Content Creation, Community Growth, Client Satisfaction and Systems Automation, and many, many sales meetings. All while keeping his sense of humour.