第1页 第2页
三、程序代码
//////////////////////////////////////////////////////////////// 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类,而是通过定义一个对话框作为其它对话框的容器,并使用一个链表实现了向导应用程序,它的优点是可以灵活地设置界面上的按钮的风格以及向导对话框上的界面,如添加位图显示等功能,能更加丰富向导应用程序的表达效果。
出处:
天极开发
(责任编辑:luwei)