Re: Tab Control message handler ?
I believe the code (below) will help answer any questions about the
details. And this code is for a simple demo for a Tab Control on the main
dialog of a dialog-based program.
1. A Tab Control from the controls "ToolBar" is placed on the main dialog.
2. Then using the Class Wizard, a CTabCtrl "control" member variable (named
m_cTabCtrl) is add to the tab control ID.
3. Then three dialog forms are inserted from "Insert" menu, using "New
Form". And these are named Tab1, Tab2, Tab3.
4. These are placed in the main dialog header file:
#include "Tab1.h"
#include "Tab2.h"
#include "Tab3.h"
5. The this code is placed in the usual OnInitDialog() of the main dialog:
BOOL CTabDemoDlg::OnInitDialog()
{
// TODO: Add extra initialization here
TC_ITEM TabCtrlItem;
TabCtrlItem.mask = TCIF_TEXT;
TabCtrlItem.pszText = "Tab 1";
m_cTabCtrl.InsertItem( 0, &TabCtrlItem );
TabCtrlItem.pszText = "Tab 2";
m_cTabCtrl.InsertItem( 1, &TabCtrlItem );
TabCtrlItem.pszText = "Tab 3";
m_cTabCtrl.InsertItem( 2, &TabCtrlItem );
CTab1 *pTab1 = new CTab1;
pTab1->Create(IDD_TAB1_DIALOG, this);
CTab2 *pTab2 = new CTab2;
pTab2->Create(IDD_TAB2_DIALOG, this);
CTab3 *pTab3 = new CTab3;
pTab3->Create(IDD_TAB3_DIALOG, this);
CRect tabRect;
m_cTabCtrl.GetItemRect(0, &tabRect);
int x = tabRect.left;
int y = tabRect.top + 20;
pTab1->SetWindowPos(NULL, x, y, 0, 0,
SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
pTab2->SetWindowPos(NULL, x, y, 0, 0,
SWP_NOZORDER|SWP_NOSIZE|SWP_HIDEWINDOW);
pTab3->SetWindowPos(NULL, x, y, 0, 0,
SWP_NOZORDER|SWP_NOSIZE|SWP_HIDEWINDOW);
}
4. Then using the Class Wizard and using the tab control "Object ID", the
message handler TSN_SELCHANGE is added to the main dialog class. And edited
with this code:
static int nOldTab;
int nNewTab = m_cTabCtrl.GetCurSel();
if(nOldTab != nNewTab)
{
switch(nOldTab)
{
case 0 : pTab1->ShowWindow(SW_HIDE);
break;
case 1 : pTab2->ShowWindow(SW_HIDE);
break;
case 2 : pTab3->ShowWindow(SW_HIDE);
break;
}
switch(nNewTab)
{
case 0 : pTab1->ShowWindow(SW_SHOW);
break;
case 1 : pTab2->ShowWindow(SW_SHOW);
break;
case 2 : pTab3->ShowWindow(SW_SHOW);
break;
}
nOldTab = nNewTab;
}
m_cTabCtrl.SetCurFocus(nNewTab);
*pResult = 0;
------------------------------
These are the only steps and no other code or controls are involved. And
the error code is the pTab1, pTab2, pTab3 are undeclared identifiers. And an
error for "left of ->ShowWindow must point to class/struct/union.