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

similar to that of list control and tree control: first we need to add tab control resource to the dialog<br />

template; then in the dialog’s initialization stage, we need to create the image list, select it into the tab<br />

control, and initialize the tab control. The function that can be used to assign image list to a tab control is<br />

CTabCtrl::SetImageList(…), which has the following format:<br />

CImageList *CTabCtrl::SetImageList(CImageList *pImageList);<br />

The function that can be used to add an item to the tab control is CTabCtrl::InsertItem(…):<br />

BOOL CTabCtrl::InsertItem(int nItem, TC_ITEM *pTabCtrlItem);<br />

The first parameter of this function is the index of the tab (zero based), and the second parameter is a<br />

pointer to TC_ITEM type object. Before calling this function, we need to stuff structure TC_ITEM with tab’s<br />

information:<br />

typedef struct _TC_ITEM {<br />

UINT mask;<br />

UINT lpReserved1;<br />

UINT lpReserved2;<br />

LPSTR pszText;<br />

int cchTextMax;<br />

int iImage;<br />

LPARAM lParam;<br />

} TC_ITEM;<br />

We need to use three members of this structure in order to create a tab with both text and image: mask,<br />

pszText and iImage. Member mask indicates which of the other members of this structure contains valid<br />

data; member pszText is a pointer to string text; and iImage is an image index to the associated image list.<br />

We see that using this structure is very similar to that of TV_ITEM and LV_ITEM.<br />

To respond to tab control’s activities, we need to add message handlers for it. The most commonly<br />

used messages of tab control are TCN_SELCHANGE and TCN_SELCHANGING, which indicate that the current<br />

selection has changed or the current selection is about to change respectively.<br />

Sample 5.16\CCtl demonstrates how to use tab control, it is based on sample 5.15\CCtl. In this sample,<br />

four radio buttons are replaced by a tab control IDC_TAB (see Figure 5-14). Also, message handlers of radio<br />

buttons are removed. In order to access the tab control, a CTabCtrl type control variable m_tabCtrl is<br />

added to class CCCtlDlg through using Class Wizard. Beside this, four bitmap resources IDB_BITMAP_ICON,<br />

IDB_BITMAP_SMALLICON, IDB_BITMAP_LIST and IDB_BITMAP_REPORT are added to the application to create<br />

image list for tab control.<br />

In function CCCtlDlg::OnInitDialog(), first the image list is created and assigned to the tab control.<br />

Next, four items are added to the tab control:<br />

……<br />

……<br />

……<br />

BOOL CCCtlDlg::OnInitDialog()<br />

{<br />

TC_ITEM tc_item;<br />

CImageList *pilTab;<br />

CBitmap bmp;<br />

pilTab=new CImageList();<br />

VERIFY(pilTab->Create(TAB_BMP_WIDTH, TAB_BMP_HEIGHT, ILC_COLOR, 4, 0));<br />

bmp.LoadBitmap(IDB_BITMAP_ICON);<br />

pilTab->Add(&bmp, RGB(127, 127, 127));<br />

bmp.DeleteObject();<br />

bmp.LoadBitmap(IDB_BITMAP_SMALLICON);<br />

pilTab->Add(&bmp, RGB(127, 127, 127));<br />

bmp.DeleteObject();<br />

bmp.LoadBitmap(IDB_BITMAP_LIST);<br />

pilTab->Add(&bmp, RGB(127, 127, 127));<br />

bmp.DeleteObject();<br />

bmp.LoadBitmap(IDB_BITMAP_REPORT);<br />

pilTab->Add(&bmp, RGB(127, 127, 127));<br />

bmp.DeleteObject();<br />

136

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

Saved successfully!

Ooh no, something went wrong!