11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. Common Controls<br />

Chapter 5<br />

Common Controls<br />

In this chapter we will discuss some common controls that can be included in a dialog box. These<br />

controls include spin control, progress bar control, slider control, tree control, tab control, animate<br />

control and combo box. These controls can all be included in a dialog template as resources. Besides,<br />

all the controls have corresponding <strong>MFC</strong> classes that can be used to implement them.<br />

In Developer Studio, Class Wizard has some features that can be used to add member variables and<br />

message handlers for the common controls. This simplifies the procedure of writing source code.<br />

5.1 Spin Control<br />

Spin control is a rectangular button with two arrows pointing to opposite directions (either vertically or<br />

horizontally), it is one of the most commonly used controls in a dialog box. Usually a spin is used together<br />

with another control, in most cases this control is an edit box (Though not common, this control can also be<br />

a button or a static control). By clicking on one of the arrows, the contents in the accompanying control will<br />

change accordingly indicating current position of the spin control.<br />

The control used together with the spin control is called spin’s Buddy Control. In <strong>MFC</strong>, it is very easy<br />

to use spin control along with edit box, they are specially designed to cooperate together.<br />

Using Spin Control with Edit Box<br />

By default, a spin control should be associated with an edit box. Usually this type of edit box contains<br />

a number indicating the current position of spin. If we make no modification, the range of this number will<br />

be from 0 to 100. If the spin’s orientation is vertical, pressing the downward arrow will cause the number to<br />

increment. If the spin’s orientation is horizontal, pressing the leftward arrow will have the same effect.<br />

When adding a spin control resource, we must set several styles in order to make it work correctly. In<br />

the property page whose caption is “Spin properties”, by clicking “Styles” tab, we will see all the<br />

customizable styles (Figure 5-1). Here, style “Auto buddy” will allow spin’s buddy to be automatically<br />

selected (By enabling this style, we do not need to set spin’s buddy within the program). If we check this<br />

selection, the window prior to the spin control in the Z order will be used as the spin’s buddy window.<br />

Figure 5-1. Spin control styles<br />

To let buddy window be automatically selected, we must first add resource for the buddy control then<br />

resource for the spin control. For example, if we want to use an edit box together with a spin control, we<br />

can add edit box resource first, then add spin control next. We can check controls’ Z order by executing<br />

92

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!