Progress Controls and Animation Controls

[Previous] [Next]

Comctl32.dll includes two convenient tools for providing visual feedback to users concerning the status of ongoing operations. The first is the progress control. A progress control is a vertical or horizontal rectangle containing a colored bar that grows as an operation approaches completion. The second is the animation control, which reduces the complex task of playing an AVI file to two simple function calls. Although animation controls enjoy a variety of uses, very often they are used simply to let the user know that a lengthy operation is underway.

Progress Controls

MFC represents progress controls with instances of the class CProgressCtrl. By default, a progress control is oriented horizontally and its bar is drawn as a series of line segments. You can orient a progress control vertically by assigning it the style PBS_VERTICAL, and you can change the bar from broken to solid with PBS_SMOOTH. (See Figure 16-11.) Unfortunately, neither style is supported in the absence of Internet Explorer 3.0 or later.

Figure 16-11. Progress controls with and without the style PBS_SMOOTH.

Like slider controls, progress controls have ranges and positions. If a progress control's range is 0 to 100 and its position is 20, the bar fills 20 percent of the control. If the range is 100 to 400 and the position is 300, the bar extends two-thirds of the control's length. The default range is 0 to 100, but you can change it to anything you like with CProgressCtrl::SetRange. If m_wndProgress is a CProgressCtrl object, the statement

 m_wndProgress.SetRange (100, 400); 

sets the control's range to 100 to 400. SetRange limits its minimums and maximums to 16-bit values, but if Internet Explorer 3.0 or later is installed, you can specify 32-bit ranges using the newer CProgressCtrl::SetRange32 function. To retrieve the current range, use GetRange. GetRange handles both 16-bit and 32-bit ranges.

Once you've created a progress control and set its range, you can set its position with CProgressCtrl::SetPos. The following example steps a progress control from 0 to 100 in about 2½ seconds by calling SetPos repeatedly from a loop that uses the ::Sleep API function to ensure that each iteration requires at least 25 milliseconds:

 m_wndProgress.SetRange (0, 100); m_wndProgress.SetPos (0); for (int i=0; i<100; i++) {     m_wndProgress.SetPos (i);     ::Sleep (25); } m_wndProgress.SetPos (0); 

That's one way to step a progress control. You can also use the OffsetPos function to specify a new position that's relative to the current one. Here's the previous code sample rewritten to use OffsetPos:

 m_wndProgress.SetRange (0, 100); m_wndProgress.SetPos (0); for (int i=0; i<100; i++) {     m_wndProgress.OffsetPos (1);     ::Sleep (25); } m_wndProgress.SetPos (0); 

A third way to step a progress control is to assign the control a step size with SetStep and then to increment the position by the current step size with StepIt:

 m_wndProgress.SetRange (0, 100); m_wndProgress.SetPos (0); m_wndProgress.SetStep (1); for (int i=0; i<100; i++) {     m_wndProgress.StepIt ();     ::Sleep (25); } m_wndProgress.SetPos (0); 

You can call the complementary CProgressCtrl::GetPos function at any time to retrieve the control's current position.

By default, the color of the bar in a progress control is the system color COLOR_HIGHLIGHT, and the control's background color is COLOR_3DFACE. On systems equipped with Internet Explorer 4.0 or higher, you can change the bar color with a PBM_SETBARCOLOR message, and you can change the control's background color with a PBM_SETBKCOLOR message. Because CProgressCtrl lacks wrapper functions for these messages, you must send the messages yourself. For example, the statement

 m_wndProgress.SendMessage (PBM_SETBARCOLOR, 0, (LPARAM) RGB (255, 0, 0)); 

changes m_wndProgress's bar color to red.

One of the sample programs in the next chapter—ImageEdit—uses a progress control to provide visual feedback regarding an image processing operation. The progress control is attached to a status bar and in fact appears to be an ordinary status bar pane until the operation begins and the bar begins stepping across the face of the control. If you'd like to see a progress control in action, feel free to skip ahead and take a look at ImageEdit.

Animation Controls

Animation controls simplify the task of playing video clips in a dialog box or a window. The video clips must be in Windows AVI format, and they can have at most two streams inside them. If one of the streams is an audio stream, it is ignored. Visual Studio comes with a number of sample AVI files that work well in animation controls. One of those sample files, Findfile.avi, contains the circling magnifying glass featured in the system's Find utility. Another, Filecopy.avi, contains the "flying paper" clip you see when you drag-copy a large file or group of files from one folder to another.

CAnimateCtrl wraps the functionality of animation controls in an easy-to-use C++ class. Using CAnimateCtrl is simplicity itself. CAnimateCtrl::Open loads an AVI clip from a resource or an external file. CAnimateCtrl::Play begins playing the clip, CAnimateCtrl::Stop stops it, and CAnimateCtrl::Close unloads the clip. Assuming that m_wndAnimate is an instance of CAnimateCtrl and that it is associated with an animation control, the following code sample loads an AVI file named Findfile.avi and begins playing it:

 m_wndAnimate.Open (_T ("Findfile.avi")); m_wndAnimate.Play (0, -1, -1); 

Open will accept a resource ID in lieu of a file name, enabling you to embed AVI clips as resources in EXE files:

 // In the RC file IDR_FINDFILE AVI "Findfile.avi"      // In the CPP file m_wndAnimate.Open (IDR_FINDFILE); m_wndAnimate.Play (0, -1, -1); 

Play starts the animation and returns immediately; it doesn't wait around for the animation to stop. That's good, because it means the thread that called Play can continue working while the animation plays in the background.

Play accepts three parameters: the starting and ending frame numbers and the number of times the animation should be played. Specifying 0, -1, and -1 for these parameters tells the control to play all the frames and to repeat them indefinitely until Stop is called, like this:

 m_wndAnimate.Stop (); 

After you call Stop, you should call Close to remove the clip from memory if you don't intend to play it anymore:

 m_wndAnimate.Close (); 

Every call to Open should be accompanied by a call to Close to prevent resource leaks.

Animation controls support four styles that affect their appearance and operation. ACS_AUTOPLAY configures the control to begin playing an animation as soon as it is opened rather than waiting for Play to be called. ACS_CENTER centers the output in the control rectangle. Without this style, the clip plays in the upper left corner of the control rectangle and the control is resized to fit the frames contained in the animation. ACS_TRANSPARENT plays the animation using a transparent background instead of the background color designated inside the AVI file. Finally, ACS_TIMER prevents the control from launching a background thread to do its drawing. Rather than start another thread (threads consume resources, and too many threads can bog down the system), an ACS_TIMER-style animation control sets a timer in the caller's thread and uses timer callbacks to draw successive frames. ACS_TIMER is supported only on systems equipped with Internet Explorer 3.0 or later.



Programming Windows with MFC
Programming Windows with MFC, Second Edition
ISBN: 1572316950
EAN: 2147483647
Year: 1999
Pages: 101
Authors: Jeff Prosise

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net