前面介绍了控件方式实现的向导模式和Tab模式。这里使用属性表和属性页的方式实现前面的功能,属性页用于与子页面窗口关联,属性表用于关联管理子页面窗口的父窗口。
1、首先新建一个对话框程序,并插入3个子窗口(作为分页窗口),ID分别为:IDD_PAGE1、IDD_PAGE2、IDD_PAGE3,3个窗口设置的属性主要:Caption、style:child、ID等。注意:我们不需要插入父窗口,因为属性表(CPropertySheet)就是MFC封装好的分页管理窗口。
2、为三个字对话框分别关联一个类,所关联类的父类为:CPropertyPage。
class CPage1 : public CPropertyPage
class CPage2 : public CPropertyPage
class CPage3 : public CPropertyPage
3、为三个对话框类添加虚函数:virtual BOOL OnSetActive();并添加如下代码:主要是设置向导模式下的按钮情况。
BOOL CPage1::OnSetActive() { CPropertySheet* ps = (CPropertySheet*)GetParent(); //you can't call SetWizardButtons before you call DoModal. //Enables or disables the Back, Next, or Finish button in a wizard property sheet(向导方式) ps->SetWizardButtons(PSWIZB_NEXT); return CPropertyPage::OnSetActive(); }
BOOL CPage2::OnSetActive() { CPropertySheet* ps = (CPropertySheet*)GetParent(); //you can't call SetWizardButtons before you call DoModal. //Enables or disables the Back, Next, or Finish button in a wizard property sheet(向导方式) ps->SetWizardButtons(PSWIZB_BACK|PSWIZB_NEXT); return CPropertyPage::OnSetActive(); }
BOOL CPage3::OnSetActive() { CPropertySheet* ps = (CPropertySheet*)GetParent(); //you can't call SetWizardButtons before you call DoModal. //Enables or disables the Back, Next, or Finish button in a wizard property sheet(向导方式) ps->SetWizardButtons(PSWIZB_BACK|PSWIZB_FINISH); return CPropertyPage::OnSetActive(); }
4、为主对话框添加两个按钮,分别是Wizard和Tab,用来测试两种分页:ID为:IDC_WIZARD、IDC_PROPERTY,并且为按钮添加单击消息响应函数:
Wizard按钮响应函数:
void CPropertySheetAndPageDlg::OnBnClickedWizard() { CPage1 p1; CPage2 p2; CPage3 p3; CPropertySheet ps; //Add pages to the property sheet in the left-to-right order you want them to appear. //When you add a property page using AddPage, the CPropertySheet is the parent of the CPropertyPage, //To gain access to the property sheet from the property page, call CWnd::GetParent. //Typically, you will call AddPage before calling DoModal or Create. ps.AddPage(&p1); ps.AddPage(&p2); ps.AddPage(&p3); ps.SetWizardMode();//设置为向导模式,默认是Tab模式 ps.DoModal(); }
Tab按钮响应函数:
void CPropertySheetAndPageDlg::OnBnClickedProperty() { CPage1 p1; CPage2 p2; CPage3 p3; CPropertySheet ps; //Add pages to the property sheet in the left-to-right order you want them to appear. //When you add a property page using AddPage, the CPropertySheet is the parent of the CPropertyPage, //To gain access to the property sheet from the property page, call CWnd::GetParent. //Typically, you will call AddPage before calling DoModal or Create. ps.AddPage(&p1); ps.AddPage(&p2); ps.AddPage(&p3); //ps.SetWizardMode();//默认是Tab模式,使用默认模式 ps.DoModal(); }
点击Wizard按钮:
点击Tab按钮: