Click here to Skip to main content
6,630,289 members and growing! (15,398 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate License: The Code Project Open License (CPOL)

A Popup Progress Window

By Chris Maunder

A popup window containing a progress control and cancel button - no resource file needed
VC6, VC7, MFC, Dev
Posted:29 Nov 1999
Updated:21 Apr 2002
Views:150,090
Bookmarked:94 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
82 votes for this article.
Popularity: 9.26 Rating: 4.84 out of 5

1

2

3
4 votes, 9.3%
4
39 votes, 90.7%
5

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)

About the Author

Chris Maunder


Member
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.
Occupation: Founder
Company: The Code Project
Location: Canada Canada

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 53 (Total in Forum: 53) (Refresh)FirstPrevNext
GeneralThis is really cool.... PinmemberRangarajan Varadan7:52 8 Apr '09  
Generalthanks PinmemberDirk Higbee4:17 2 Sep '08  
QuestionProgress bar frozen PinmemberSolange200813:05 31 Aug '08  
QuestionCrash in CProgressWnd::Create() in VS2008 PinmemberoldNic8:28 15 Aug '08  
AnswerRe: Crash in CProgressWnd::Create() in VS2008 PinmemberoldNic11:41 18 Aug '08  
GeneralRe: Crash in CProgressWnd::Create() in VS2008 Pinmemberarokicki8:57 5 Mar '09  
Generalerror C2504: 'CWnd' : base class undefined PinmemberLuke DeStevens11:29 9 Oct '07  
GeneralRe: error C2504: 'CWnd' : base class undefined Pinmemberjaymz2:43 12 May '08  
GeneralHow get rid of flickering? PinmemberWalxer14:04 28 Aug '07  
GeneralThank you Chris : I no more need two threads Pinmembercharfeddine_ahmed2:28 24 Aug '07  
GeneralAccessing Progress Window? PinmemberWVP6:48 10 Aug '07  
GeneralRe: Accessing Progress Window? Pinmembercharfeddine_ahmed3:21 24 Aug '07  
GeneralGood! Pinmembernapo@burgasnet.com4:28 26 Jun '07  
GeneralThe same for .NET Pinmember_slav_4:04 4 Feb '07  
GeneralCool ! Pinmembereigen19:03 30 Sep '06  
Generalproblem used in worker thread Pinmemberhow jack10:48 24 Feb '06  
GeneralCrash on MouseOver PinmemberStaati5:48 25 Jan '06  
GeneralRe: Crash on MouseOver PinmemberStaati6:37 26 Jan '06  
GeneralAlways crashes for me :-( PinmemberEthannCastell1:49 4 Oct '05  
Generalgreat timesaver Pinmemberjefflewis23:38 26 Aug '05  
Generalget information popup PinmemberTailana23:33 1 Jun '05  
GeneralConsole Pinmemberbachi22:12 13 Feb '05  
GeneralWorkaround to bitmap buttons showing through PinmemberTom Archer9:09 9 Jan '05  
GeneralRe: Workaround to bitmap buttons showing through Pinmemberkido9723:43 2 Nov '09  
GeneralVC++ 7.0 Problem PinsussAnonymous2:36 22 Oct '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Apr 2002
Editor: Nishant Sivakumar
Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project