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 />

command Layout | Tab order (or pressing CTRL+D keys). The Z-order of the controls can be reordered<br />

by clicking them one by one according to the new sequence.<br />

In the left-bottom corner of “Spin properties” property sheet, there is a combo box labeled<br />

“Alignment”. This allows us to specify how the spin will be attached to its buddy window when being<br />

displayed. If we select “Unattatched” style, the spin and the buddy control will be separated. Usually we<br />

select either “Left” or “Right” style to attach the spin to the left or right side of the buddy control. In this<br />

case, the size and position of the spin control in the dialog template has no effect on its real size and<br />

position in the runtime, its layout will be decided according to the size and position of the buddy control.<br />

Also, there is a “Set buddy integer” check box. If this style is set, the spin will automatically send out<br />

message to its buddy control (must be an edit box) and cause it to display a series of integers when the<br />

spin’s position is changed. By default, the integer contained in the edit box will increment or decrement<br />

with a step of 1. If we want to customize this (For example, if we want to change the step or want to display<br />

floating point numbers), we should uncheck this style and set the buddy’s text within the program.<br />

Sample 5.1-1\CCtl demonstrates how to use spin control with edit control and set buddy automatically.<br />

The sample is a standard dialog based application generated by Application Wizard, with all default<br />

settings. The resource ID of the main dialog template is IDD_CCTL_DIALOG, which contains two spin<br />

controls and two edit boxes. Both spins have “Auto buddy” and “Set buddy integer” styles. Also, their<br />

alignment styles are set to “Right” (Figure 5-2).<br />

Figure 5-2. Dialog template IDD_CCTRL_DIALOG<br />

Without adding a single line of code, we can compile the project and execute it. The spin controls and<br />

the edit controls will work together to let us select integers (Figure 5-3).<br />

Figure 5-3. Sample 5-1\CCtrl<br />

In <strong>MFC</strong>, spin control is implemented by class CSpinButtonCtrl. We need to call various member<br />

functions of this class in order to customize the properties of the spin control. In sample 5.1-2\CCtl, the<br />

control’s buddy is set by calling function CSpinButtonCtrl::SetBuddy(…) instead of using automatic<br />

method. The best place to set a spin’s buddy is in the dialog box’s initialization stage. This corresponds to<br />

calling function CDialog::OnInitDialog().<br />

Sample 5.1-2\CCtl is based on sample 5.1-1\CCtl. Here, style “Auto buddy” is removed for two spin<br />

controls. Also, some changes are nade to set the spin buddies manually.<br />

There are two ways of accessing a specific spin: we can use a spin’s ID to call function<br />

CWnd::GetDlgItem(…), which will return CWnd type pointer to the spin control; or we can add a<br />

93

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

Saved successfully!

Ooh no, something went wrong!