mfc tab control switch tabs

Posted by MRM on Stack Overflow See other posts from Stack Overflow or by MRM
Published on 2012-07-31T18:32:31Z Indexed on 2012/11/27 23:08 UTC
Read the original article Hit count: 522

Filed under:
|
|

I created a simple tab control that has 2 tabs (each tab is a different dialog). The thing is that i don't have any idea how to switch between tabs (when the user presses Titlu Tab1 to show the dialog i made for the first tab, and when it presses Titlu Tab2 to show my other dialog). I added a handler for changing items, but i don't know how should i acces some kind of index or child for tabs.

Tab1.h and Tab2.h are headers for dialogs that show only static texts with the name of the each tab.

There may be an obvious answer to my question, but i am a real newbie in c++ and MFC.

This is my header:

// CTabControlDlg.h : header file
//

#pragma once
#include "afxcmn.h"
#include "Tab1.h"
#include "Tab2.h"


// CCTabControlDlg dialog
class CCTabControlDlg : public CDialog
{
// Construction
public:
   CCTabControlDlg(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
enum { IDD = IDD_CTABCONTROL_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
   HICON m_hIcon;

  // Generated message map functions
  virtual BOOL OnInitDialog();
  afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
  afx_msg void OnPaint();
  afx_msg HCURSOR OnQueryDragIcon();
  DECLARE_MESSAGE_MAP()
public:
  CTabCtrl m_tabcontrol1;

  CTab1 m_tab1;
  CTab2 m_tab2;
  afx_msg void OnTcnSelchangeTabcontrol(NMHDR *pNMHDR, LRESULT *pResult);
};

And this is the .cpp:

// CTabControlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CTabControl.h"
#include "CTabControlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
 public:
   CAboutDlg();

 // Dialog Data
  enum { IDD = IDD_ABOUTBOX };

  protected:
  virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

 // Implementation
 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()


 // CCTabControlDlg dialog




 CCTabControlDlg::CCTabControlDlg(CWnd* pParent /*=NULL*/)
   : CDialog(CCTabControlDlg::IDD, pParent)
 {
   m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 }

 void CCTabControlDlg::DoDataExchange(CDataExchange* pDX)
 {
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_TABCONTROL, m_tabcontrol1);
 }

 BEGIN_MESSAGE_MAP(CCTabControlDlg, CDialog)
  ON_WM_SYSCOMMAND()
  ON_WM_PAINT()
  ON_WM_QUERYDRAGICON()
  //}}AFX_MSG_MAP
  ON_NOTIFY(TCN_SELCHANGE, IDC_TABCONTROL,         &CCTabControlDlg::OnTcnSelchangeTabcontrol) 
    END_MESSAGE_MAP()


   // CCTabControlDlg message handlers

  BOOL CCTabControlDlg::OnInitDialog()
  {
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    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);
    }
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);         // Set big icon
SetIcon(m_hIcon, FALSE);        // Set small icon

// TODO: Add extra initialization here

CTabCtrl* pTabCtrl = (CTabCtrl*)GetDlgItem(IDC_TABCONTROL);

m_tab1.Create(IDD_TAB1, pTabCtrl);

TCITEM item1;
item1.mask      = TCIF_TEXT | TCIF_PARAM;
item1.lParam    = (LPARAM)& m_tab1;
item1.pszText   = _T("Titlu Tab1");
pTabCtrl->InsertItem(0, &item1);

//Pozitionarea dialogului
CRect rcItem;
pTabCtrl->GetItemRect(0, &rcItem);
m_tab1.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER );

m_tab1.ShowWindow(SW_SHOW);

// al doilea tab
m_tab2.Create(IDD_TAB2, pTabCtrl);
TCITEM item2;
item2.mask      = TCIF_TEXT | TCIF_PARAM;
item2.lParam    = (LPARAM)& m_tab1;
item2.pszText   = _T("Titlu Tab2");
pTabCtrl->InsertItem(0, &item2);

//Pozitionarea dialogului
//CRect rcItem;
pTabCtrl->GetItemRect(0, &rcItem);
m_tab2.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER );

m_tab2.ShowWindow(SW_SHOW);

return TRUE;  // return TRUE  unless you set the focus to a control
  }

  void CCTabControlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  {
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
}
else
{
    CDialog::OnSysCommand(nID, lParam);
}
    }

  // If you add a minimize button to your dialog, you will need the code below
  //  to draw the icon.  For MFC applications using the document/view model,
  //  this is automatically done for you by the framework.

  void CCTabControlDlg::OnPaint()
  {
if (IsIconic())
{
    CPaintDC dc(this); // device context for painting

    SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()),   0);

    // Center icon in client rectangle
    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;

    // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
}
else
{
    CDialog::OnPaint();
}
  }

  // The system calls this function to obtain the cursor to display while the user drags
  //  the minimized window.
  HCURSOR CCTabControlDlg::OnQueryDragIcon()
  {
   return static_cast<HCURSOR>(m_hIcon);
  }


  void CCTabControlDlg::OnTcnSelchangeTabcontrol(NMHDR *pNMHDR, LRESULT *pResult)
  {
// TODO: Add your control notification handler code here
*pResult = 0;

   }

© Stack Overflow or respective owner

Related posts about c++

Related posts about mfc