Progress Bars
Site Map Feedback

Download:

Up Controls Files Moving Data

Progress Bars made Fast and Easy!

The core of any progress bar is mapping one scale to another. If you're copying files then the file size has to map to the number of pixels you're going to mark on the screen when the whole file has been copied. So you need a variable to store the length of the progress bar and another to store the number of bytes to copy. Since every redraw will want to know how much of the progress bar to draw, you also need to store the number of bytes copied so far. To save calculating this every time a Redraw happens (assuming the progress bar hasn't moved), store the number of pixels to fill across the Progress Bar.

Since most progress bars are rectangular, simply remember a CRect holding the rectangle to draw; the [right] member of CRect will be what is changed as the Progress Bar moves. Since other parameters may need adjusting as the Progress Bar moves, another scale should be maintained that will change from 0 to 1 which can be used to multiply things by... This is demonstrated in the project.

If you're going to draw text in the status bar and the redraw gets too slow if the Status Bar changes quickly, you'll have to use Video Paging.

I developed what I'll be using as a demonstration project for a Dialog Application which always reports information with a 'Status Bar' (a Static Text Control). I didn't want to alter the appearance of the Application just to report progress, and I didn't want to have anything popping up to report separately, so this example is a Static Text Control which can indicate progress when required.

So to begin, you need to create a Static Text Control on your Dialog and call it something like IDC_Status. Use Class Wizard to create a New Class called CStatus derived from CStatic. Subclass the control by adding: CStatus Status; to your Dialog's Header file, then add: Status.SubclassDlgItem(IDC_Status,this); to your Dialog's OnInitDialog() method.

Now you are ready to make the Static Control have the extra behaviour needed to be a Progress Bar. My example CStatus uses colour to show a use for the 0 to 1 scale I talked about. So if you're not going to download the complete project you should be aware that the example uses files from my MemDC and HSL Colour Sections.

If you're going to download the project be aware that the code is written to work as a Debug release only (because the file that is read is one that VC creates when compiling a Debug Release). I chose this file because its a big file that I can guarantee will exist on all users PCs without downloading anything. To change the file being read alter the line in StatusTestDlg.cpp: if(!File.Open("Debug\\StatusTest.pch", CFile::modeRead)) return; Hopefully you'll find lots of interesting optimisations in the code!

The colour change has been made to vary from red to green, but without fading. The overall effect for fast changes is strangely more aesthetic than fading, and the redraw speed is faster. Of course, this is still a fairly standard looking bar, and since you're drawing it all you could do anything you wanted: I've made it capable of make a circular sweep, and a colour wheel; you could make it draw frames from an animation etc.

A useful metaphor, when doing processes like file copying, is a clock: the big hand spinning once round for each file copied, and the small hand once round for all files copied. If a square space is all that is available, this could replace two progress bars.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.