11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5. Common Controls<br />

Changing List Style Dynamically<br />

The style of the list control can be set in property sheet “List control properties” before the application<br />

is compiled(see Figure 5-14). But, sometimes we may want to provide the user with the power of changing<br />

this style dynamically. When the program is running, we can call API function ::SetWindwoLong(…) to<br />

change the application’s style. For list control, we can choose from one of the following styles: LVS_ICON,<br />

LVS_SMALLICON, LVS_LIST and LVS_REPORT.<br />

Select one of the<br />

styles<br />

Figure 5-14. Set the style for list control<br />

In the sample, four radio buttons are added to the dialog template for selecting different styles. Their<br />

IDs are IDC_RADIO_ICON, IDC_RADIO_SMALLICON, IDC_RADIO_LIST and IDC_RADIO_REPORT respectively.<br />

We need to handle BN_CLICKED message for the four radio buttons in order to respond to mouse events.<br />

These message handlers are added through using Class Wizard. Within the member functions, the style of<br />

the list control is changed according to which radio button is being clicked. The following is one of the<br />

message handlers that sets the style of the list control to “Normal Icon”:<br />

void CCCtlDlg::OnRadioIcon()<br />

{<br />

LONG lStyle;<br />

}<br />

lStyle=GetWindowLong(m_listCtrl.GetSafeHwnd(), GWL_STYLE);<br />

lStyle&=~(LVS_TYPEMASK);<br />

lStyle|=LVS_ICON;<br />

SetWindowLong(m_listCtrl.GetSafeHwnd(), GWL_STYLE, lStyle);<br />

First, the list control’s old style is retrieved by calling function ::GetWindowLong(…), and is bit-wisely<br />

ANDed with LVS_TYPEMASK, which will turn off all the style bits. Then style LVS_ICON is added to the<br />

window style (through bit-wise ORing), and function ::SetWindowLong(…) is called to update the new<br />

style. Both function ::GetWindowLong(…) and ::SetWindowLong(…) require a window handle, it could be<br />

obtained by calling function CWnd:: GetSafeHwnd().<br />

The list control and tree control can also be implemented in SDI and MDI applications. In this case, we<br />

need to use classes derived from CListView or CTreeView. Although the creating procedure is a little<br />

different from that of a dialog box, the properties of the controls are exactly the same for two different<br />

types of applications. We will further explore list control and tree control in chapter 15.<br />

5.16 Tab Control<br />

In the previous sample, we used radio buttons to let the user set the style of list control dynamically.<br />

An alternate way of doing this is to use tab control, which is widely used in various types of applications.<br />

Usually a tab control is used together with dialog box to implement property sheets, which can let the user<br />

easily switch among different property pages. This topic will be discussed in a chapter 7. Here, we will<br />

discuss some basics on how to implement tab control and handle its messages.<br />

Using Tab Control<br />

In <strong>MFC</strong>, tab control can be implemented by using class CTabCtrl. A tab control can be associated with<br />

an image list, so we can display both image and text on each tab. The steps of using a tab control is very<br />

135

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

Saved successfully!

Ooh no, something went wrong!