i have written a program which contain 2 tabs sample tab1 and sample tab 2. i used mytabctrl.h and mytabctrl.cpp to build tabs. Now what i want to do is that when click the ok button in 1st tab i-2 sample tab1 it opens the 2nd tab i-e sample tab2. how to do this need help.
What I have tried:
header file of main dialog is maindialog.h
#include "MyTabCtrl.h"
class CTabExample1Dlg : public CDialog
{
public:
CTabExample1Dlg(CWnd* pParent = NULL);
enum { IDD = IDD_TABEXAMPLE1_DIALOG };
MyTabCtrl m_tbCtrl;
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
};
maindialog.cpp code is
#include "stdafx.h"
#include "TabExample1.h"
#include "TabExample1Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
CTabExample1Dlg::CTabExample1Dlg(CWnd* pParent )
: CDialog(CTabExample1Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTabExample1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TABCTRL1, m_tbCtrl);
}
BEGIN_MESSAGE_MAP(CTabExample1Dlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
BOOL CTabExample1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE);
m_tbCtrl.InitDialogs();
m_tbCtrl.InsertItem(0,"Sample Tab 1");
m_tbCtrl.InsertItem(1,"Sample Tab 2");
m_tbCtrl.ActivateTabDialogs();
return TRUE; }
void CTabExample1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CTabExample1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CTabExample1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
MyTabCtrl.cpp is
<pre lang="c++"><pre>
#include "stdafx.h"
#include "TabExample1.h"
#include "MyTabCtrl.h"
#include "MyDlg1.h"
#include "MyDlg2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
MyTabCtrl::MyTabCtrl()
{
m_DialogID[0] =IDD_DIALOG1;
m_DialogID[1] =IDD_DIALOG2;
m_Dialog[0] = new MyDlg1();
m_Dialog[1] = new MyDlg2();
m_nPageCount = 2;
}
MyTabCtrl::~MyTabCtrl()
{
}
void MyTabCtrl::InitDialogs()
{
m_Dialog[0]->Create(m_DialogID[0],GetParent());
m_Dialog[1]->Create(m_DialogID[1],GetParent());
}
BEGIN_MESSAGE_MAP(MyTabCtrl, CTabCtrl)
ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnSelchange)
END_MESSAGE_MAP()
void MyTabCtrl::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult)
{
ActivateTabDialogs();
*pResult = 0;
}
void MyTabCtrl::ActivateTabDialogs()
{
int nSel = GetCurSel();
if(m_Dialog[nSel]->m_hWnd)
m_Dialog[nSel]->ShowWindow(SW_HIDE);
CRect l_rectClient;
CRect l_rectWnd;
GetClientRect(l_rectClient);
AdjustRect(FALSE,l_rectClient);
GetWindowRect(l_rectWnd);
GetParent()->ScreenToClient(l_rectWnd);
l_rectClient.OffsetRect(l_rectWnd.left,l_rectWnd.top);
for(int nCount=0; nCount < m_nPageCount; nCount++){
m_Dialog[nCount]->SetWindowPos(&wndTop, l_rectClient.left,l_rectClient.top,l_rectClient.Width(),l_rectClient.Height(),SWP_HIDEWINDOW);
}
m_Dialog[nSel]->SetWindowPos(&wndTop,l_rectClient.left,l_rectClient.top,l_rectClient.Width(),l_rectClient.Height(),SWP_SHOWWINDOW);
m_Dialog[nSel]->ShowWindow(SW_SHOW);
}