您的位置:首页 > 运维架构

CPropertySheet的基本用法

2017-06-26 00:43 375 查看

和别人讨论问题,要实现一个MFCDialog内的内嵌属性页,实现Tab的效果.

以前玩过,现在不记得怎么做,去查了msdn, 写个demo记录一下。

demo下载点

srcTestTabDlg.zip

编译环境:vs2010 on win10x64

效果



代码预览

代码是从msdn上找的,建立动态的属性表,属性表中加属性页。

// testTabDlgDlg.h : header file
//

#pragma once
#include "Page1.h"
#include "Page2.h"

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

// Dialog Data
enum { IDD = IDD_TESTTABDLG_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:
afx_msg void OnBnClickedOk();

private:
CPropertySheet* m_pSheet;
CPage1 m_Page1;
CPage2 m_Page2;
public:
afx_msg void OnDestroy();
afx_msg void OnSize(UINT nType, int cx, int cy);
};


// testTabDlgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testTabDlg.h"
#include "testTabDlgDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAboutDlg dialog used for App About

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

// Dialog Data
enum { IDD = IDD_ABOUTBOX };

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

// Implementation
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()

// CtestTabDlgDlg dialog

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

void CtestTabDlgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CtestTabDlgDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CtestTabDlgDlg::OnBnClickedOk)
ON_WM_DESTROY()
ON_WM_SIZE()
END_MESSAGE_MAP()

// CtestTabDlgDlg message handlers

BOOL CtestTabDlgDlg::OnInitDialog()
{
CDialogEx::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)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
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
m_pSheet = new CPropertySheet();

m_Page2.m_psp.dwFlags |= PSP_USETITLE;
m_Page2.m_psp.pszTitle = _T("Page2");
m_pSheet->AddPage(&m_Page2);

m_Page1.m_psp.dwFlags |= PSP_USETITLE;
m_Page1.m_psp.pszTitle = _T("Page1");
m_pSheet->AddPage(&m_Page1);

m_pSheet->Create(this, WS_CHILD);
m_pSheet->SetActivePage(0);
m_pSheet->ShowWindow(SW_SHOW);

HWND hWndSheet = ::GetDlgItem(m_hWnd, IDC_STATIC_SHEET);
RECT rt;
::GetWindowRect(hWndSheet, &rt);
m_pSheet->MoveWindow(0, 0, rt.right - rt.left, rt.bottom - rt.top, TRUE);

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

void CtestTabDlgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::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 CtestTabDlgDlg::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
{
CDialogEx::OnPaint();
}
}

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

void CtestTabDlgDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CDialogEx::OnOK();
}

void CtestTabDlgDlg::OnDestroy()
{
CDialogEx::OnDestroy();

// TODO: Add your message handler code here
if (NULL != m_pSheet) {
delete m_pSheet;
m_pSheet = NULL;
}
}

void CtestTabDlgDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: