应用程序向导是一种用来简化用户操作的程序,在Microsoft 的所有软件产品中都存在应用程序向导,例如Office2000 中的Web 页向导就是一个十分典型的应用程序向导,Visual C++提供的'App Wizard'也是一个应用程序向导。本实例给出了实现自己的应用程序向导的方法,程序编译运行后的界面效果如图一所示:
 图一、自定义向导程序界效果图 |
一、编程方法
在Visual C++编程中,可以使用MFC提供的类CPropertySheet和类CPropertyPage方便地编写一个向导程序。首先我们来介绍一下类CPropertySheet 和类CPropertyPage。属性页类CPropertyPage 是从类CDialog中派生出来的,具有Diaglog的基本性质,不过需要注意的是在将一个对话框模板关联到CpropertyPage类时对话框模板的样式必须设置为'Child'。类CPropertySheet 是一个属性表,它也代表一个窗体,相当一个容器,用来存放所有的属性页CpropertyPage,CpropertySheet类不是从Cdialog类派生出来的,但是该类对象可以进行普通对话框似的操作,如使用DoModal()函数显示属性表后,它就包含了'取消'、'上一步'、'下一步' 等基本按钮。为了将各个属性页添加到属性表中,可以调用CpropertySheet::AddPage( CPropertyPage *pPage )。需要读者朋友注意的一点是,一般情况下不直接使用CpropertySheet、CpropertyPage类,而是分别使用它们的子类,具体参见程序代码部分。
将属性页添加到属性表中之后,就需要协调它们的显示,也就是要决定某个属性页具体显示'取消、上一步、下一步、完成、帮助'等几个基本按钮中的哪些按钮。在属性表中的某一属性页为当前页时,会触发OnSetActive事件,所以只需要对每一个属性页重载该函数来处理相应的工作。例如,当显示第一页时,由于不存在'上一步',故在该属性页的OnSetActive()函数中需要添加如下代码:
CPropertySheet* pParent=(CPropertySheet*)GetParent(); // 获得属性表的指针 pParent->SetWizardButtons(PSWIZB_NEXT); // 设置属性表的显示按钮只为下一步 SetDlgItemText(IDC_TEXT1,'这是向导的第一步'); |
同样在显示中间页时应该设置成即有'上一步',也有'下一步',代码为:
CPropertySheet* pParent=(CPropertySheet*)GetParent(); pParent->SetWizardButtons(PSWIZB_NEXT|PSWIZB_BACK); SetDlgItemText(IDC_TEXT2,'这是向导的第二步'); |
最后在显示最后一页时只显示'完成'和'上一步',代码为:
CPropertySheet* pParent=(CPropertySheet*)GetParent(); pParent->SetWizardButtons(PSWIZB_FINISH|PSWIZB_BACK); SetDlgItemText(IDC_TEXT3,'这是向导的第三步'); |
从上面的代码可以看出,决定当前属性页使用哪个按钮关键是使用了CPropertySheet ::SetWizardButtons()函数,该函数的原型为:
| void SetWizardButtons( DWORD dwFlags ); |
参数dwFlags定义了属性页上具体显示那些导航按钮,该值是下列标志的组合:PSWIZB_BACK (Back button)、PSWIZB_NEXT( Next button)、PSWIZB_FINISH(Finish button)、PSWIZB_DISABLEDFINISH(Disabled Finish button)。
因为CpropertySheet、CPropertyPage类不是一个可修改的资源,所以在程序中会发现改变向导按钮的样式会很困难,例如不能在'上一步'、'下一步就'等按钮上添加图标等;也不能修改向导按钮的位置。为了实现一个性化向导的目的,我们可以不使用CPropertySheet类和CPropertyPage类,而自行设计一个向导程序。设计的基本思路是:采用标准的向导的工作方式,每一步就是一个对话框,向导本身也是一个对话框,用来容纳每步对话框;当点击'下一步'或'上一步'时,将相应的对话框定位到要显示的位置;因为向导一般都包含很多步,每一步对应一个页,为了管理这些页,我们可以创建一个链表来管理每一步的对话框。具体实现参见代码部分。
二、编程步骤
1、启动Visual C++6.0,生成一个基于对话框的应用程序,将该程序命名为'CustomWizard'
2、在程序的对话框模板中加入一个按钮用来启动向导,其ID设置为IDC_BENGINWIZ。另外加入一个集合框用来容纳向导中的每个对话框,并根据该模板定义类'Cwizard';
3、依次创建向导的每页的对话框资源,命名为IDD_STEP1、IDD_STEP2、IDD_STEP3,然后根据资源模板生成新的类;
4、添加代码,编译运行程序。
[/page]三、程序代码
//////////////////////////////////////////////////////////////// Wizard.h : header file #if !defined(AFX_WIZARD_H__1778A141_2519_11D6_9DF5_70D757C10000__INCLUDED_) #define AFX_WIZARD_H__1778A141_2519_11D6_9DF5_70D757C10000__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include 'Step1.h' #include 'Step2.h' #include 'Step3.h'
class CWizard : public CDialog { // Construction public: void SetWizButton(UINT uFlag); void ShowPage(UINT nPos); void AddPage(UINT nID); CWizard(CWnd* pParent = NULL); // standard constructor CRect rectPage; //每页显示的框 UINT nPageCount;//页的总数 UINT nCurrentPage; //显示的当前页 // Dialog Data //{{AFX_DATA(CWizard) enum { IDD = IDD_WIZARD }; // NOTE: the ClassWizard will add data members here //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CWizard) public: protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: typedef struct PAGELINK{ UINT nNum; CDialog* pDialog; struct PAGELINK* Next; }; PAGELINK* pPageLink; //用来链接所有的页 // Generated message map functions //{{AFX_MSG(CWizard) afx_msg void OnCancel(); afx_msg void OnPrev(); afx_msg void OnNext(); afx_msg void OnFinish(); virtual BOOL OnInitDialog(); afx_msg void OnDestroy(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; #endif
////////////////////////////////////////////////////////////////// // Wizard.cpp : implementation file #include 'stdafx.h' #include 'CustomWizard.h' #include 'Wizard.h' #include 'Step1.h'
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CWizard dialog
CWizard::CWizard(CWnd* pParent /*=NULL*/): CDialog(CWizard::IDD, pParent) { //{{AFX_DATA_INIT(CWizard) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT pPageLink=NULL; nPageCount=0; nCurrentPage=0; }
void CWizard::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CWizard) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP }
BEGIN_MESSAGE_MAP(CWizard, CDialog) //{{AFX_MSG_MAP(CWizard) ON_BN_CLICKED(IDC_PREV, OnPrev) ON_BN_CLICKED(IDC_NEXT, OnNext) ON_BN_CLICKED(IDC_FINISH, OnFinish) ON_BN_CLICKED(IDC_CANCEL, OnCancel) ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP()
//////////////////////////////////////////////////////////// CWizard message handlers void CWizard::AddPage(UINT nID) { struct PAGELINK* pTemp=pPageLink; //插入新生成的结点 struct PAGELINK* pNewPage=new struct PAGELINK; pNewPage->pDialog=new CDialog; ASSERT(pNewPage->pDialog->Create(nID,this)); // Is window created ASSERT(::IsWindow(pNewPage->pDialog->m_hWnd)); // 检查每页的样式 DWORD dwStyle = pNewPage->pDialog->GetStyle(); ASSERT((dwStyle & WS_CHILD) != 0); //子窗体 ASSERT((dwStyle & WS_BORDER) == 0); //无边界 //显示 pNewPage->pDialog->ShowWindow(SW_HIDE); pNewPage->pDialog->MoveWindow(rectPage); pNewPage->Next=NULL; pNewPage->nNum=++nPageCount; //计数器加1 if (pTemp) { while (pTemp->Next) pTemp=pTemp->Next; //移动链表末尾 pTemp->Next=pNewPage; } else pPageLink=pNewPage; //若是第一个接点 }
void CWizard::OnCancel() { // TODO: Add your control notification handler code here if (AfxMessageBox(IDS_QUIT,MB_OKCANCEL|MB_ICONQUESTION)==IDCANCEL) return; CDialog::OnCancel(); }
void CWizard::OnPrev() { // TODO: Add your control notification handler code here ShowPage(--nCurrentPage); }
void CWizard::OnNext() { // TODO: Add your control notification handler code here ShowPage(++nCurrentPage); }
void CWizard::OnFinish() { // TODO: Add your control notification handler code here AfxMessageBox('采用默认值完成向导'); CDialog::OnOK(); }
void CWizard::ShowPage(UINT nPos) { struct PAGELINK* pTemp=pPageLink; while(pTemp) { if (pTemp->nNum==nPos) { pTemp->pDialog->ShowWindow(SW_SHOW); } else //不显示 pTemp->pDialog->ShowWindow(SW_HIDE); pTemp=pTemp->Next; } if (nPos>=nPageCount) //最后一页 { nCurrentPage=nPageCount; SetWizButton(2); return; } if (nPos<=1) //首页 { nCurrentPage=1; SetWizButton(0); return; } //中间步 SetWizButton(1); }
void CWizard::SetWizButton(UINT uFlag) { GetDlgItem(IDC_CANCEL)->EnableWindow(TRUE); GetDlgItem(IDC_PREV)->EnableWindow(TRUE); GetDlgItem(IDC_NEXT)->EnableWindow(TRUE); GetDlgItem(IDC_FINISH)->EnableWindow(TRUE); switch(uFlag) { case 0: //第一步 GetDlgItem(IDC_PREV)->EnableWindow(FALSE); break; case 1: //中间步 break; case 2://最后一步 GetDlgItem(IDC_NEXT)->EnableWindow(FALSE); break; } }
BOOL CWizard::OnInitDialog() { CDialog::OnInitDialog(); //获得每页显示的范围 CRect Rect1; GetWindowRect(&Rect1); //获得主窗口的位置 int nCaption = ::GetSystemMetrics(SM_CYCAPTION); int nXEdge = ::GetSystemMetrics(SM_CXEDGE); int nYEdge = ::GetSystemMetrics(SM_CYEDGE); CRect Rect2; GetDlgItem(IDC_POS)->GetWindowRect(&Rect2); //获得框架的位置 Rect1.top=Rect1.top+nCaption+nYEdge; //相对坐标 Rect1.left=Rect1.left+2*nXEdge; //计算机位置 rectPage.top=Rect2.top-Rect1.top; rectPage.left=Rect2.left-Rect1.left; rectPage.bottom=Rect2.bottom-Rect1.top; rectPage.right=Rect2.right-Rect1.left; //添加要显示的页 AddPage(IDD_STEP1); AddPage(IDD_STEP2); AddPage(IDD_STEP3); //显示第一页 ShowPage(1); return TRUE; // return TRUE unless you set the focus to a control }
void CWizard::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here //依次消除每页 struct PAGELINK* pTemp=pPageLink; while(pTemp) { pTemp->pDialog->DestroyWindow(); pTemp=pTemp->Next; } } |
四、小结
本实例在实现向导应用程序的时候,没有使用CPropertySheet和CPropertyPage类,而是通过定义一个对话框作为其它对话框的容器,并使用一个链表实现了向导应用程序,它的优点是可以灵活地设置界面上的按钮的风格以及向导对话框上的界面,如添加位图显示等功能,能更加丰富向导应用程序的表达效果。
|