再探MFC(二)多標籤對話框

包含控件頁的標籤式對話框,用戶可通過單擊鼠標在其間切換.

  • 屬性表
  • Tab控件
本篇我們只介紹使用屬性表實現多標籤頁對話框.這也是本系列文章的風格,即爲了儘可能的簡單化,只提供必要的,必要的只提供最通用的.

 

屬性表

 

屬性表的功能都合理地封裝在一對MFC,CPropertySheetCPropertyPage.CPropertySheet代表屬性表自身,是從CWnd派生出來的.CPropertyPage代表屬性表的頁,是從CDialog派生出來的.

 

和對話框一樣,屬性表可以是模式和無模式的.CPropertySheet::DoModal創建模式屬性表,CPropertySheet::Create創建無模式屬性表.

 

創建模式屬性表的步驟如下:

  1. 針對屬性表的每一頁創建一個對話框模板,定義頁的內容和特性.將對話框標題設置成您希望在屬性表頁上方標籤中顯現的標題.
  1. 針對屬性表的每一頁由CPropertyPage派生出一個類似對話框的類.其中包含通過DDXDDV與頁面中控件相聯繫的公用數據成員.
  1. CPropertySheet派生出一個屬性表類.將該屬性表類和第2步中得到的屬性表頁類實例化.利用CPropertySheet::AddPage將各頁按期望中的顯示順序添加到屬性表中.
  1. 調用屬性表的DoModal函數將屬性表顯示在屏幕上.

 

簡單示例代碼 


 

 

#pragma once

#include"resource.h"

 

// CFirstPage 對話框

 

class CFirstPage :public CPropertyPage

{

DECLARE_DYNAMIC(CFirstPage)

 

public:

CFirstPage();

virtual~CFirstPage();

 

// 對話框數據

enum{ IDD = IDD_PROPPAGE_FIRST };

 

protected:

virtualvoid DoDataExchange(CDataExchange* pDX);   // DDX/DDV 支持

 

DECLARE_MESSAGE_MAP()

};

 

 

// FirstPage.cpp :實現文件

//

 

#include"stdafx.h"

#include"MFCExplore.h"

#include"FirstPage.h"

#include"afxdialogex.h"

 

 

// CFirstPage 對話框

 

IMPLEMENT_DYNAMIC(CFirstPage,CPropertyPage)

 

CFirstPage::CFirstPage()

:CPropertyPage(CFirstPage::IDD)

{

 

}

 

CFirstPage::~CFirstPage()

{

}

 

voidCFirstPage::DoDataExchange(CDataExchange* pDX)

{

CPropertyPage::DoDataExchange(pDX);

}

 

 

BEGIN_MESSAGE_MAP(CFirstPage,CPropertyPage)

END_MESSAGE_MAP()

 

 

// CFirstPage 消息處理程序

 

CSecondPage類似於CFirstPage,.

 

#pragma once

#include"afxdlgs.h"

#include"FirstPage.h"

#include"SecondPage.h"

classCMyPropertySheet :

publicCPropertySheet

{

public:

CMyPropertySheet(LPCTSTRpszCaption, CWnd* pParentWnd = NULL);

~CMyPropertySheet();

//第一頁屬性頁

CFirstPagem_firstPage;

//第二頁屬性頁

CSecondPagem_secondPage;

};

 

#include"stdafx.h"

#include"MyPropertySheet.h"

 

 

CMyPropertySheet::CMyPropertySheet(LPCTSTRpszCaption, CWnd* pParentWnd)

:CPropertySheet(pszCaption,pParentWnd, 0)

{

AddPage(&m_firstPage);

AddPage(&m_secondPage);

}

 

 

CMyPropertySheet::~CMyPropertySheet()

{

}

 

 

CxxxApp::InitInstance()中創建模態屬性表.

CMyPropertySheetps(_T("Properties"));

m_pMainWnd =&ps;

INT_PTR nResponse =ps.DoModal();

 

參考自MFC Windows程序設計 8.4屬性表.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章