Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC
Article

A Popup Progress Window

Rate me:
Please Sign up or sign in to vote.
4.92/5 (56 votes)
21 Apr 2002CPOL1 min read 305.9K   11.1K   153   59
A popup window containing a progress control and cancel button - no resource file needed

Sample Image

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);
                                    // Make window modal

int  SetPos(int nPos);              // Same as CProgressCtrl
int  OffsetPos(int nPos);           // Same as CProgressCtrl
int  SetStep(int nStep);            // Same as CProgressCtrl
int  StepIt();                      // Same as CProgressCtrl
void SetRange(int nLower, int nUpper, int nStep = 1);
                                    // Set min, max and step size

void Hide();                        // Hide the window
void Show();                        // Show the window
void Clear();                       // Clear the text and reset the
                                    // progress bar
void SetText(LPCTSTR fmt, ...);     // Set the text in the text area

BOOL Cancelled()                    // Has the cancel button been pressed?

void SetWindowSize(int nNumTextLines, int nWindowWidth = 390);
                                    // Sets the size of the window
                                    // according to the number of text
                                    // lines specifed and the
                                    // desired window size in pixels.

void PeekAndPump(BOOL bCancelOnESCkey = TRUE);
                                    // Message pumping, with options of
                                    // allowing Cancel on ESC key.

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.GoModal(); // Call this if you want a modal window
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, 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. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
QuestionAnother improved class based on this one Pin
mrsilver2-Jun-17 12:41
mrsilver2-Jun-17 12:41 
QuestionHow Do I close the ProgressWindow ? Pin
sunil_rbc13-Oct-15 2:07
professionalsunil_rbc13-Oct-15 2:07 
Questionquestion Pin
kcpnevergiveup15-Apr-13 16:21
kcpnevergiveup15-Apr-13 16:21 
AnswerRe: question Pin
kcpnevergiveup15-Apr-13 17:01
kcpnevergiveup15-Apr-13 17:01 
GeneralThis is really cool.... Pin
Rangarajan Varadan8-Apr-09 6:52
Rangarajan Varadan8-Apr-09 6:52 
Generalthanks Pin
Dirk Higbee2-Sep-08 3:17
Dirk Higbee2-Sep-08 3:17 
QuestionProgress bar frozen Pin
Solange200831-Aug-08 12:05
Solange200831-Aug-08 12:05 
QuestionCrash in CProgressWnd::Create() in VS2008 Pin
oldNic15-Aug-08 7:28
oldNic15-Aug-08 7:28 
AnswerRe: Crash in CProgressWnd::Create() in VS2008 Pin
oldNic18-Aug-08 10:41
oldNic18-Aug-08 10:41 
GeneralRe: Crash in CProgressWnd::Create() in VS2008 Pin
arokicki5-Mar-09 7:57
arokicki5-Mar-09 7:57 
AnswerRe: Crash in CProgressWnd::Create() in VS2008 Pin
Ards6-Jun-11 18:02
Ards6-Jun-11 18:02 
Generalerror C2504: 'CWnd' : base class undefined Pin
Luke DeStevens9-Oct-07 10:29
Luke DeStevens9-Oct-07 10:29 
GeneralRe: error C2504: 'CWnd' : base class undefined Pin
jaymz12-May-08 1:43
jaymz12-May-08 1:43 
QuestionHow get rid of flickering? Pin
Walxer28-Aug-07 13:04
Walxer28-Aug-07 13:04 
GeneralThank you Chris : I no more need two threads Pin
Ahmed Charfeddine24-Aug-07 1:28
Ahmed Charfeddine24-Aug-07 1:28 
QuestionAccessing Progress Window? Pin
WVP10-Aug-07 5:48
WVP10-Aug-07 5:48 
AnswerRe: Accessing Progress Window? Pin
Ahmed Charfeddine24-Aug-07 2:21
Ahmed Charfeddine24-Aug-07 2:21 
GeneralGood! Pin
napo@burgasnet.com26-Jun-07 3:28
napo@burgasnet.com26-Jun-07 3:28 
GeneralThe same for .NET Pin
_slav_4-Feb-07 3:04
_slav_4-Feb-07 3:04 
GeneralCool ! Pin
eigen30-Sep-06 18:03
eigen30-Sep-06 18:03 
Generalproblem used in worker thread Pin
how jack24-Feb-06 9:48
how jack24-Feb-06 9:48 
GeneralRe: problem used in worker thread Pin
Kevin Done9-Nov-11 21:21
Kevin Done9-Nov-11 21:21 
GeneralCrash on MouseOver Pin
Staati25-Jan-06 4:48
Staati25-Jan-06 4:48 
Hi there,
thank you for this great class!
Unfortunately the Progress Window crashes if i move the mouse over the Dialog.

I use this Dialog without a parent window. If the dialog runs without moving the mouse over it, it works fine.

If i debug my program i get stuck on this line:
AfxGetApp()->PumpMessage()
in the PeekAndPump method.

Any suggestions?

-- modified at 10:57 Wednesday 25th January, 2006
GeneralRe: Crash on MouseOver Pin
Staati26-Jan-06 5:37
Staati26-Jan-06 5:37 
GeneralAlways crashes for me :-( Pin
EthannCastell4-Oct-05 0:49
EthannCastell4-Oct-05 0:49 

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.