01.jpg

 

本範例使用的是Visual Studio 2015 - MFC Dialog架構.

Step 1.加入Tab Control元件

在MainDialog表單中加入Tab Control元件.

宣告變數:

CTabCtrl m_TabCtrl;

02.jpg

Step 2.建立Tab Control的標籤

在MainDialog.cpp的OnInitDialog()裡,插入標籤

    m_TabCtrl.InsertItem(0, _T("標籤1"));
    m_TabCtrl.InsertItem(1, _T("標籤2"));
    m_TabCtrl.InsertItem(2, _T("標籤3"));
 

Step 3.建立3個Dialog

增加新的Dialog1,Dialog2,Dialog3, 並產生類別.

並且更改其屬性

Border->無

Style->Child

 

Step 4.建立連結,設定及宣告

在MainDialog.h

#include "Dialog1.h"
#include "Dialog2.h"
#include "Dialog3.h"
 

宣告標籤的Dialog

protected:

    CDialog1 m_Dialog1;

    CDialog2 m_Dialog2;

    CDialog3 m_Dialog3;

然後在MainDialog.cpp onini裡建立

    CRect rect;
    m_TabCtrl.GetClientRect(&rect);

    m_Dialog1.Create(IDD_DIALOG1, &m_TabCtrl);
    m_Dialog1.SetWindowPos(NULL, 5, 25, rect.Width() - 10, rect.Height() - 30, SWP_SHOWWINDOW | SWP_NOZORDER);


    m_Dialog2.Create(IDD_DIALOG2, &m_TabCtrl);
    m_Dialog2.SetWindowPos(NULL, 5, 25, rect.Width() - 10, rect.Height() - 30, SWP_SHOWWINDOW | SWP_NOZORDER);


    m_Dialog3.Create(IDD_DIALOG3, &m_TabCtrl);
    m_Dialog3.SetWindowPos(NULL, 5, 25, rect.Width() - 10, rect.Height() - 30, SWP_SHOWWINDOW | SWP_NOZORDER);


Step 5.切換標籤並對應相對的Dialog

根據標籤OnTcnSelchangeTab事件,來顯示相對應的Dialog

void CWindowsDlg::OnTcnSelchangeTab(NMHDR *pNMHDR, LRESULT *pResult)
{
    // TODO: 在此加入控制項告知處理常式程式碼
    *pResult = 0;

 

    if (m_pwndShow != NULL)
    {
        m_pwndShow->ShowWindow(SW_HIDE);
    }
 

    int nIndex = m_TabCtrl.GetCurSel();
    switch (nIndex)
    {
    case 0:
        m_Dialog1.ShowWindow(SW_SHOW);
        //m_Dialog2.ShowWindow(SW_HIDE);
        //m_Dialog3.ShowWindow(SW_HIDE);
        m_pwndShow = &m_Dialog1;
        break;
    case 1:
        //m_Dialog1.ShowWindow(SW_HIDE);
        m_Dialog2.ShowWindow(SW_SHOW);
        //m_Dialog3.ShowWindow(SW_HIDE);
        m_pwndShow = &m_Dialog2;
        break;
    case 2:
        //m_Dialog1.ShowWindow(SW_HIDE);
        //m_Dialog2.ShowWindow(SW_HIDE);
        m_Dialog3.ShowWindow(SW_SHOW);
        m_pwndShow = &m_Dialog3;
        break;
    }

}

Step 6.建立指標對應相對的Dialog

在MainDialog.h宣告CWnd 類別變數:

protected:

CWnd* m_pwndShow; 

在MainDialog.cpp的OnInitDialog()裡

m_Dialog1.Create(IDD_DIALOG1, &m_TabCtrl);後

設定指標m_pwndShow = &m_Dialog1;

//m_Dialog2.Create(IDD_DIALOG2, &m_TabCtrl);後

//設定指標m_pwndShow = &m_Dialog2;

//m_Dialog3.Create(IDD_DIALOG3, &m_TabCtrl);後

//設定指標m_pwndShow = &m_Dialog3;

 

創用 CC 授權條款
Microsoft Visual Studio - MFC Tab Control組織架構建立Peng Yi Hsing製作,以創用CC 姓名標示-非商業性-禁止改作 3.0 台灣 授權條款釋出。

arrow
arrow

    30sec 發表在 痞客邦 留言(0) 人氣()