Article ID: 151662
Article Last Modified on 11/21/2006
// This sample disables page #2 of a property sheet.
// We need to include this file so we can use CMap
#include <afxtempl.h>
class CMySheet : public CPropertySheet
{
protected:
// we save the current page in TCN_SELCHANGING
int m_nLastActive;
// list of indexes of disabled pages
CMap <int, int&, int, int&> m_DisabledPages;
public:
void EnablePage (int nPage, BOOL bEnable = TRUE);
...
};
BOOL CMySheet::OnInitDialog()
{
// OnInitDialog() must be called before EnablePage()
// or GetTabControl() will fail.
BOOL bReturn = CPropertySheet::OnInitDialog();
// disable page #2
EnablePage (1, FALSE);
return bReturn;
}
void CMySheet::EnablePage (int nPage, BOOL bEnable)
{
// if we want to enable the page
if (bEnable)
{
// remove the index from the map
m_DisabledPages.RemoveKey (nPage);
// take out " - Disabled" from tab label
CTabCtrl* pTab = GetTabControl();
ASSERT (pTab);
TC_ITEM ti;
char szText[100];
ti.mask = TCIF_TEXT;
ti.pszText = szText;
ti.cchTextMax = 100;
VERIFY (pTab->GetItem (nPage, &ti));
char * pFound = strstr (szText, " - Disabled");
if (pFound)
{
*pFound = '\0';
VERIFY (pTab->SetItem (nPage, &ti));
}
}
// if we want to disable it
else
{
// add the index to the map
m_DisabledPages.SetAt (nPage, nPage);
// add " - Disabled" to tab label
CTabCtrl* pTab = GetTabControl();
ASSERT (pTab);
TC_ITEM ti;
char szText[100];
ti.mask = TCIF_TEXT;
ti.pszText = szText;
ti.cchTextMax = 100;
VERIFY (pTab->GetItem (nPage, &ti));
strcat (szText, " - Disabled");
VERIFY (pTab->SetItem (nPage, &ti));
}
}
BOOL CMySheet::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
NMHDR* pnmh = (NMHDR*)lParam;
// tab is about to change
if (TCN_SELCHANGING == pnmh->code)
// save the current page index
m_nLastActive = GetActiveIndex ();
// tab has been changed
else if (TCN_SELCHANGE == pnmh->code)
{
// get the current page index
int nCurrentPage = GetActiveIndex ();
// if current page is in our map of disabled pages
if (m_DisabledPages.Lookup (nCurrentPage, nCurrentPage))
// activate the previous page
PostMessage (PSM_SETCURSEL, m_nLastActive);
}
return CPropertySheet::OnNotify(wParam, lParam, pResult);
}
/* Compile options needed: default
*/
Additional query words: 4.00 4.10 CPropertySheet CPropertyPage disable
Keywords: kbhowto kbpropsheet kbuidesign KB151662