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

m_tabCtrl.SetImageList(pilTab);<br />

tc_item.mask=TCIF_TEXT | TCIF_IMAGE;<br />

tc_item.pszText="Icon";<br />

tc_item.iImage=0;<br />

m_tabCtrl.InsertItem(0, &tc_item);<br />

tc_item.pszText="Small Icon";<br />

tc_item.iImage=1;<br />

m_tabCtrl.InsertItem(1, &tc_item);<br />

tc_item.pszText="List";<br />

tc_item.iImage=2;<br />

m_tabCtrl.InsertItem(2, &tc_item);<br />

tc_item.pszText="Report";<br />

tc_item.iImage=3;<br />

m_tabCtrl.InsertItem(3, &tc_item);<br />

}<br />

return TRUE;<br />

Macro TAB_BMP_WIDTH and TAB_BMP_HEIGHT are defined as the width and height of the bitmaps. In<br />

function CCCtlDlg::Destroy(), the following statements are added to delete the tab items and the image<br />

list used by the tab control:<br />

void CCCtlDlg::OnDestroy()<br />

{<br />

CImageList *pilCtrl;<br />

……<br />

}<br />

pilCtrl=m_tabCtrl.GetImageList();<br />

pilCtrl->DeleteImageList();<br />

m_tabCtrl.DeleteAllItems();<br />

Handling Tab Control Message<br />

We trap message TCN_SELCHANGE to respond to the changes on the tab control. After receiving this<br />

message, we call function CTabCtrl::GetCurSel() to obtain the newly selected item, then call function<br />

::SetWindowLong(…) to set the style of the list control accordingly. In the sample, function<br />

CCCtlDlg::OnSelchangeTab(…) is added to class CCCtlDlg through using Class Wizard for handling this<br />

message. It is implemented as follows:<br />

void CCCtlDlg::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult)<br />

{<br />

LONG lStyle;<br />

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

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

switch(m_tabCtrl.GetCurSel())<br />

{<br />

case 0:<br />

{<br />

lStyle|=LVS_ICON;<br />

break;<br />

}<br />

case 1:<br />

{<br />

lStyle|=LVS_SMALLICON;<br />

break;<br />

}<br />

case 2:<br />

{<br />

lStyle|=LVS_LIST;<br />

break;<br />

}<br />

case 3:<br />

{<br />

137

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

Saved successfully!

Ooh no, something went wrong!