Home Index  | Back

New Page 1
Lesson 1 -Building Your First Windows Application

1.1 : Creating a Windows Application

1.2 : Creating a Dialog-Based Application

1.3 : Creating DLLs, Console Applications, and More

1.4 : Changing Your AppWizard Decisions
 1.5 : Understanding AppWizard’s Code
 1.6 : Understanding a MDI Application
 1.7 : Understanding the Components of a Dialog-Based Application

Lesson 2 - Dialogs and Controls

2.1 : Understanding Dialog Boxes

2.2 : Creating a Dialog Box Resource

 2.3 : Writing a Dialog Box Class
 2.4 : Using the Dialog Box Class
Lesson 3 - Messages and Commands

3.1 : Understanding Message Routing

3.2 : Understanding Message Loops

 3.3 : Reading Message Maps
 3.4 : Learning How ClassWizard Helps You Catch Messages
 3.5 : Recognizing Messages
 3.6 : Understanding Commands
 3.7 : Understanding Command Updates
 3.8 : Learning How ClassWizard Helps You Catch Commands and Command Updates

Lesson 4 - Documents and Views

4.1 : Understanding the Document Class

4.2 : Understanding the View Class

4.3 : Creating the Rectangles Application

 4.4 : Other View Classes

4.5 : Document Templates, Views, and Frame Windows

Lesson 5 - Drawing on the Screen

5.1 :Understanding Device Contexts

 5.2 : Introducing the Paint1 Application
 5.3 : Building the Paint1 Application
 5.4 : Scrolling Windows
 5.5 : Building the Scroll Application
Lesson 6 - Building a Complete Application: ShowString

6.1 : Building an Application That Displays a String

 6.2 : Building the ShowString Menus
 6.3 : Building the ShowString Dialog Boxes
 6.4 : Making the Menu Work
 6.5 : Making the Dialog Box Work
 6.6 : Adding Appearance Options to the Options Dialog Box
Lesson 7 -  Status Bars and Toolbars

7.1 : Working with Toolbars

 7.2 : Working with Status Bars
Lesson 8 - Common Controls

8.1 : The Progress Bar Control

 8.2 : The Up-Down Control
 8.3 : The Image List Control
 8.4 : The List View Control
 8.5 : The Tree View Control
 8.6 : The Rich Edit Control
 8.7 : The Date Picker Control
 8.8 : Month Calendar Control
 8.9 : Scrolling the View
Lesson 9 - Property Pages and Sheets

9.1 : Introducing Property Sheets

 9.2 : Creating the Property Sheet Demo Application
 9.3 : Running the Property Sheet Demo Application
Lesson 10 - ActiveX Concepts

10.1 : The Purpose of ActiveX

10.2 : Object Linking

10.3 : Object Embedding

 10.4 : Containers and Servers
 10.5 : Toward a More Intuitive User Interface
 10.6 : The Component Object Model  

10.7 : Automation

 10.8 : ActiveX Controls

Lesson 11 -  Building an ActiveX Control

11.1 : Creating a Rolling-Die Control

11.2 : Displaying the Current Value

11.3 : Reacting to a Mouse Click and Rolling the Die 

 11.4 : Creating a Better User Interface
 11.5 : Generating Property Sheets
Lesson 12 - Database Access

12.1 : Understanding Database Concepts

12.2 : Creating an ODBC Database Program

 12.3 : Choosing Between ODBC and DAO
 12.4 : OLE DB

Lesson -8:Common Controls

8.1- The Progress Bar Control

As a Windows user, you’re accustomed to seeing controls such as buttons, list boxes, menus, and edit boxes. As Windows developed, however, Microsoft noticed that developers routinely create other types of controls in their programs: toolbars, status bars, progress bars, tree views, and others. To make life easier for Windows programmers, Microsoft included these popular controls as part of the operating environment of Windows 95 (as well as later versions of Windows NT and then Windows 98). Now Windows programmers no longer need to create from scratch their own versions of these controls. This unit introduces you to many of the 32-bit Windows common controls.

This Unit’s sample program is called Common. It demonstrates seven of the Windows 95 common controls: the progress bar, up-down, list view, tree view, rich edit,date picker, and month calendar controls, all of which are shown in Figure 8.1. In the following sections, you learn the basics of creating and using these controls in your own applications.


FIG. 8.1 The Common sample application demonstrates nine Windows 95 common controls.

To make Common, create a new project with AppWizard and name it Common. Choose a single-document interface (SDI) application in Step 1 and accept all the defaults until Step 6. Drop down the Base Class box and choose CScrollView from the list. This ensures that users can see all the controls in the view, even if they have to scroll to do so. Click Finish and then OK to complete the process.

The controls themselves are declared as data members of the view class. Double-click CCommonView in ClassView to edit the header file and add the lines in Listing 8.1 in the Attributes section. As you can see, the progress bar is an object of the CProgressCtrl class. It’s discussed in the next section, and the other controls are discussed in later sections of this Unit.

Listing 8.1 CommonView.h—Declaring the Controls

protected:

//Progress Bar

CProgressCtrl m_progressBar;

//Trackbar or Slider

CSliderCtrl m_trackbar;

BOOL m_timer;

// Up-Down or Spinner

CSpinButtonCtrl m_upDown;

CEdit m_buddyEdit;

// List View

CListCtrl m_listView;

CImageList m_smallImageList;

CImageList m_largeImageList;

CButton m_smallButton;

CButton m_largeButton;

CButton m_listButton;

CButton m_reportButton;

// Tree View

CTreeCtrl m_treeView;

CImageList m_treeImageList;

// Rich Edit

CRichEditCtrl m_richEdit;

CButton m_boldButton;

CButton m_leftButton;

CButton m_centerButton;

CButton m_rightButton;

// IP Address

CIPAddressCtrl m_ipaddress;

// Date Picker

CDateTimeCtrl m_date;

// Month Calendar

CMonthCalCtrl m_month;

Expand the CCommonView class. Double-click CCommonView::OnDraw() in ClassView and replace the TODO comment with these lines:

pDC->TextOut(20, 22, "Progress Bar Control");

pDC->TextOut(270, 22, "Trackbar Control:");

pDC->TextOut(20, 102, "Up-Down Control");

pDC->TextOut(160, 102, "List View Control");

pDC->TextOut(20, 240, "Tree View Control");

pDC->TextOut(180, 240, "Rich Edit Control");

pDC->TextOut(470, 22, "IP Address Control");

pDC->TextOut(470, 102, "Date Picker Control");

pDC->TextOut(470, 240, "Month Calendar Control");

These label the controls that you will add to CCommonView in this Unit.

The common control that’s probably easiest to use is the progress bar, which is nothing more than a rectangle that slowly fills in with colored blocks. The more colored blocks that are filled in, the closer the task is to being complete. When the progress bar is completely filled in, the task associated with the progress bar is also complete. You might use a progress bar to show the status of a sorting operation or to give the user visual feedback about a large file that’s being loaded.

8.1.1 Creating the Progress Bar

Before you can use a progress bar, you must create it. Often in an MFC program, the controls are created as part of a dialog box. However, Common displays its controls in the application’s main window, the view of this single-document interface (SDI) application. All the controls are created in the view class OnCreate() function, which responds to the WM_CREATE Windows message. To set up this function, right-click CCommonView in ClassView and choose Add Windows Message Handler. Choose WM_CREATE from the list on the left and click Add and Edit. Add this line in place of the TODO comment:

CreateProgressBar();

Right-click CCommonView in ClassView again and this time choose Add Member Function. Enter void for the Function Type and enter CreateProgressBar() for the Function Declaration. Leave the access as Public. Click OK to add the function; then add the code in Listing 8.2.

Listing 8.2 CommonView.cpp—CCommonView::CreateProgressBar()

void CCommonView::CreateProgressBar()

{

m_progressBar.Create(WS_CHILD | WS_VISIBLE | WS_BORDER,

CRect(20, 40, 250, 80), this, IDC_PROGRESSBAR);

m_progressBar.SetRange(1, 100);

m_progressBar.SetStep(10);

m_progressBar.SetPos(50);

m_timer = FALSE;

}

CreateProgressBar() first creates the progress bar control by calling the control’s Create() function. This function’s four arguments are the control’s style flags, the control’s size (as a CRect object), a pointer to the control’s parent window, and the control’s ID. The resource ID, IDC_PROGRESSBAR, is added by hand. To add resource symbols to your own applications, choose View, Resource Symbols and click the New button. Type in a resource ID Name, such as IDC_PROGRESSBAR, and accept the default Value Visual Studio provides.

The style constants are the same constants that you use for creating any type of window (a control is nothing more than a special kind of window, after all). In this case, you need at least the following:

  • WS_CHILD Indicates that the control is a child window

  • WS_VISIBLE Ensures that the user can see the control

  • The WS_BORDER is a nice addition because it adds a dark border around the control, setting it off from the rest of the window.

    8.1.2 Initializing the Progress Bar

    To initialize the control, CCommonView::CreateProgressBar() calls SetRange(), SetStep(), and SetPos(). Because the range and the step rate are related, a control with a range of 1-10 and a step rate of 1 works almost identically to a control with a range of 1-100 and a step rate of 10.

    When this sample application starts, the progress bar is already half filled with colored blocks. (This is purely for aesthetic reasons. Usually a progress bar begins its life empty.) It’s half full because CreateProgressBar() calls SetPos() with the value of 50, which is the midpoint of the control’s range.

    8.1.3 Manipulating the Progress Bar

    Normally you update a progress bar as a long task moves toward completion. In this sample, you will fake it by using a timer. When the user clicks in the background of the view, start a timer that generates WM_TIMER messages periodically. Catch these messages and advance the progress bar. Here’s what to do:

    1. Open ClassWizard. Make sure that CCommonView is selected in the upper-right drop- down box.

    2. Scroll most of the way through the list box on the right until you find WM_LBUTTONDOWN, the message generated when the user clicks on the view. Select it.

    3. Click Add Function; then click Edit Code.

    4. Edit OnLButtonDown() so that it looks like this:

    void CCommonView::OnLButtonDown(UINT nFlags, CPoint point)

    {

    if (m_timer)

    {

    KillTimer(1);

    m_timer = FALSE;

    }

    else

    {

    SetTimer(1, 500, NULL);

    m_timer = TRUE;

    }

    CView::OnLButtonDown(nFlags, point);

    }

    This code enables users to turn the timer on or off with a click. The parameter of 500 in the SetTimer call is the number of milliseconds between WM_TIMER messages: This timer will send a message twice a second.

    5. In case a timer is still going when the view closes, you should override OnDestroy() to kill the timer. Right-click CCommonView in ClassView yet again and choose Add Windows Message Handler. Select WM_DESTROY and click Add and Edit. Replace the TODO comment with this line:

    KillTimer(1);

    6. Now, catch the timer messages. Open ClassWizard and, as before, scroll through the list of messages in the far right list box. WM_TIMER is the second-to-last message in the alphabetic list, so drag the elevator all the way to the bottom and select WM_TIMER. Click Add Function and then click Edit Code. Replace the TODO comment with this line:

    m_progressBar.StepIt();

    The StepIt() function increments the progress bar control’s value by the step rate, causing new blocks to be displayed in the control as the control’s value setting counts upward. When the control reaches its maximum, it automatically starts over.

    Build Common and execute it to see the progress bar in action. Be sure to try stopping the timer as well as starting it.

    Next>>
     
    © Dewsoft Overseas