📄 wizarddlg.cpp
字号:
// SL330DBInstallDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "WizardDlg.h"
// CWizardDlg 对话框
IMPLEMENT_DYNAMIC(CWizardDlg, CDialog)
CWizardDlg::CWizardDlg(LPCTSTR lpszTemplateName,
CWnd* pParent):CDialog(lpszTemplateName,pParent)
{
Init();
}
CWizardDlg::CWizardDlg(UINT nIDTemplate,
CWnd* pParent):CDialog(nIDTemplate,pParent)
{
Init();
}
CWizardDlg::~CWizardDlg()
{
}
void CWizardDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWizardDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWizardDlg, CDialog)
//{{AFX_MSG_MAP(CWizardDlg)
ON_WM_DESTROY()
ON_BN_CLICKED(ID_WIZFINISH, OnWizardFinish)
ON_BN_CLICKED(ID_WIZBACK, OnWizardBack)
ON_BN_CLICKED(ID_WIZNEXT, OnWizardNext)
ON_BN_CLICKED(IDCANCEL, OnCancel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWizardDlg message handlers
BOOL CWizardDlg::OnInitDialog()
{
CDialog::OnInitDialog();
ModifyStyleEx (0, WS_EX_CONTROLPARENT);
ASSERT(m_nPlaceholderID != 0); // Be sure to call SetPlaceholderID from
//将第一个页面加载并激活
SetFirstPage();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CWizardDlg::Init()
{
m_nPlaceholderID = 0;
}
BOOL CWizardDlg::DestroyPage(CWizardPage* pPage)
{
if (pPage->m_bCreated)
{
if(pPage->m_bActive)
{
if (pPage->OnKillingActive() == FALSE)
return FALSE;
}
pPage->OnDestroyPage();
pPage->DestroyWindow();
pPage->m_bCreated = FALSE;
}
//如果该Page页还未被创建,直接返回False
return TRUE;
}
CWizardPage* CWizardDlg::GetFirstPage()
{
//如果当前Page列表为空返回一个NULL
if(m_PageList.IsEmpty())
{
return NULL;
}
return (CWizardPage*)m_PageList.GetHead();
}
CWizardPage* CWizardDlg::GetLastPage()
{
//如果当前Page列表为空返回一个NULL
if(m_PageList.IsEmpty())
{
return NULL;
}
return (CWizardPage*)m_PageList.GetTail();
}
CWizardPage* CWizardDlg::GetActivePage() const
{
CWizardPage* pPage;
POSITION Pos = m_PageList.GetHeadPosition();
while (Pos)
{
pPage = (CWizardPage*)m_PageList.GetNext(Pos);
if (pPage->m_bActive)
{
return pPage;
}
}
return NULL;
}
//获取下一页面
CWizardPage* CWizardDlg::GetNextPage()
{
CWizardPage* pPage;
POSITION Pos = m_PageList.GetHeadPosition();
while (Pos)
{
pPage = (CWizardPage*)m_PageList.GetNext(Pos);
if (pPage->m_bActive)
{
if (Pos == NULL) return NULL;
return (CWizardPage*)m_PageList.GetAt(Pos);
}
}
return NULL;
}
void CWizardDlg::SetPlaceholderID(int nPlaceholderID)
{
m_nPlaceholderID = nPlaceholderID;
}
// returns TRUE if the new page is activated
BOOL CWizardDlg::ActivatePage(CWizardPage* pPage)
{
ASSERT(m_nPlaceholderID != 0);
ASSERT(pPage != NULL);
ASSERT(::IsWindow(m_hWnd));
//如果该Page页还未被Crate,创建该Page
if (pPage->m_bCreated == FALSE)
{
if (pPage->Create(pPage->m_nDialogID, this) == FALSE) return FALSE;
pPage->m_bCreated = TRUE;
pPage->m_pParent = this;
if (pPage->OnCreatePage() == FALSE) return FALSE;
}
//将当前页面设置为deactive
if (!DeactivatePage()) return FALSE;
CRect rect;
CWnd *pWnd = GetDlgItem(m_nPlaceholderID);
ASSERT(pWnd != NULL);
ASSERT(IsWindow(pWnd->m_hWnd) != FALSE);
pWnd->GetWindowRect(&rect);
ScreenToClient(&rect);
pPage->SetWindowPos(NULL, rect.left, rect.top, 0, 0,
SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );
pPage->EnableWindow(TRUE);
pPage->ShowWindow(SW_SHOW);
pPage->InvalidateRect(NULL);
pPage->UpdateWindow();
pPage->OnSettingActive();
pPage->m_bActive = TRUE;
return TRUE;
}
//将当前页面设置为Deactive,被激活页面取消激活态
BOOL CWizardDlg::DeactivatePage()
{
CWizardPage* pPage = GetActivePage();
if (pPage == NULL) return TRUE;
ASSERT(pPage->m_bCreated != FALSE);
if (!pPage->OnKillingActive()) return FALSE;
pPage->ShowWindow(SW_HIDE);
pPage->m_bActive = FALSE;
return TRUE;
}
void CWizardDlg::OnDestroy()
{
CWizardPage* pPage;
POSITION Pos = m_PageList.GetHeadPosition();
while (Pos)
{
pPage = (CWizardPage*)m_PageList.GetNext(Pos);
// this function could return FALSE, but if it happens here
// it is too late to do anything about it!
VERIFY(DestroyPage(pPage));
}
CDialog::OnDestroy();
}
//向该类的List中加入新的CWizardPage对象
//nID是你要添加的CWizardPage对象的ID
void CWizardDlg::AddPage(CWizardPage* pPage, UINT nID)
{
m_PageList.AddTail(pPage);
pPage->m_nDialogID = nID;
}
//根据ID将特定的页面激活
void CWizardDlg::SetActivePageByResource(UINT nResourceID)
{
CWizardPage* pPage = GetPageByResourceID(nResourceID);
if (pPage == NULL) return;
if (!DeactivatePage()) return;
ActivatePage(pPage);
}
//根据ID获取该CWizardPage对象
CWizardPage* CWizardDlg::GetPageByResourceID(UINT nResourceID)
{
CWizardPage* pPage;
POSITION Pos = m_PageList.GetHeadPosition();
while (Pos)
{
pPage = (CWizardPage*)m_PageList.GetNext(Pos);
if (pPage->m_nDialogID == nResourceID)
{
return pPage;
}
}
return NULL;
}
//将第一页加载并设为激活态
BOOL CWizardDlg::SetFirstPage()
{
CWizardPage* pPage = GetFirstPage();
//如果第一页FirstPage为空则直接返回,没有第一页可激活
if(NULL ==pPage)
{
return FALSE;
}
if (!DeactivatePage()) return FALSE;
EnableBack(FALSE);
if (m_PageList.GetCount() > 1)
{
EnableFinish(FALSE);
EnableNext(TRUE);
}
else //如果该向导只有一页
{
EnableFinish(TRUE);
EnableNext(FALSE);
}
if (ActivatePage(pPage)) return TRUE;
return FALSE;
}
//将下一页面激活
void CWizardDlg::SetNextPage()
{
CWizardPage* pPage = GetNextPage();
if (ActivatePage(pPage))
{
EnableBack(TRUE);
}
}
///////////////////////////////
//控件消息
// user pressed the Finish button
void CWizardDlg::OnWizardFinish()
{
CWizardPage* pPage;
pPage = GetActivePage();
//是否可以关闭当前页面
if (!pPage->OnKillingActive()) return;
//通知所有的Page已经完成操作
POSITION Pos = m_PageList.GetHeadPosition();
while (Pos)
{
pPage = (CWizardPage*)m_PageList.GetNext(Pos);
if (pPage->m_bCreated)
{
if (!pPage->OnWizardFinish())
{
// data validation failed for one of the pages so we can't close
return;
}
}
}
// The only reason this line would be needed is if you had controls
// place in your main wizard dialog outside of the wizard pages.
// In most "normal" implementations, this line does nothing
UpdateData(TRUE);
// close the dialog and return ID_WIZFINISH
CDialog::EndDialog(ID_WIZFINISH);
}
void CWizardDlg::OnWizardBack()
{
CWizardPage* pPage = GetActivePage();
ASSERT(pPage);
LRESULT lResult = pPage->OnWizardBack();
if (lResult == -1) return; // don't change pages
if (lResult == 0)
{
POSITION Pos = m_PageList.Find(pPage);
ASSERT(Pos);
m_PageList.GetPrev(Pos);
if (Pos == NULL) return; // the current page was the first page
pPage = (CWizardPage*)m_PageList.GetAt(Pos);
}
else
{
pPage = GetPageByResourceID((UINT)lResult);
if (pPage == NULL) return;
}
if (!ActivatePage(pPage)) return;
// if we are on the first page, then disable the back button
if (pPage == GetFirstPage())
{
EnableBack(FALSE);
EnableFinish(FALSE);
}
// enable the next button
EnableNext(TRUE);
pPage->OnSettedActive();
}
void CWizardDlg::OnWizardNext()
{
CWizardPage* pPage = GetActivePage();
ASSERT(pPage);
LRESULT lResult = pPage->OnWizardNext();
if (lResult == -1) return; // don't change pages
if (lResult == 0)
{
POSITION Pos = m_PageList.Find(pPage);
ASSERT(Pos);
m_PageList.GetNext(Pos);
if (Pos == NULL) return; // the current page was the last page
pPage = (CWizardPage*)m_PageList.GetAt(Pos);
}
else
{
pPage = GetPageByResourceID((UINT)lResult);
if (pPage == NULL) return;
}
if (!ActivatePage(pPage)) return;
//如果是最后一页
if (pPage == GetLastPage())
{
EnableNext(FALSE);
EnableFinish(TRUE);
}
EnableBack(TRUE);
pPage->OnSettedActive();
}
void CWizardDlg::EnableFinish(BOOL bEnable)
{
ASSERT(::IsWindow(m_hWnd));
CWnd* pWnd = GetDlgItem(ID_WIZFINISH);
ASSERT(pWnd); // You must have an ID_WIZFINISH on your dialog
if (pWnd)
{
pWnd->EnableWindow(bEnable);
}
}
void CWizardDlg::EnableCancel(BOOL bEnable)
{
ASSERT(::IsWindow(m_hWnd));
CWnd* pWnd = GetDlgItem(IDCANCEL);
ASSERT(pWnd); // You must have an IDCANCEL on your dialog
if (pWnd)
{
pWnd->EnableWindow(bEnable);
}
}
void CWizardDlg::EnableBack(BOOL bEnable)
{
ASSERT(::IsWindow(m_hWnd));
CWnd* pWnd = GetDlgItem(ID_WIZBACK);
ASSERT(pWnd); // You must have an ID_WIZBACK on your dialog
if (pWnd)
{
pWnd->EnableWindow(bEnable);
}
}
void CWizardDlg::EnableNext(BOOL bEnable)
{
ASSERT(::IsWindow(m_hWnd));
CWnd* pWnd = GetDlgItem(ID_WIZNEXT);
ASSERT(pWnd); // You must have an ID_WIZNEXT on your dialog
if (pWnd)
{
pWnd->EnableWindow(bEnable);
}
}
///////////////////////////////////////////////////////////////
// Functions to mimic the behavior of CPropertySheet
// returns the index of the currently active page
int CWizardDlg::GetActiveIndex() const
{
CWizardPage* pPage;
POSITION Pos = m_PageList.GetHeadPosition();
int nIndex = 0;
while (Pos)
{
pPage = (CWizardPage*)m_PageList.GetNext(Pos);
if (pPage->m_bActive)
{
return nIndex;
}
++nIndex;
}
return -1;
}
int CWizardDlg::GetPageIndex(CWizardPage* pPage) const
{
CWizardPage* pTestPage;
POSITION Pos = m_PageList.GetHeadPosition();
int nIndex = 0;
while (Pos)
{
pTestPage = (CWizardPage*)m_PageList.GetNext(Pos);
if (pTestPage == pPage)
{
return nIndex;
}
++nIndex;
}
return -1;
}
int CWizardDlg::GetPageCount()
{
int count = (int)m_PageList.GetCount();
return count;
}
// get a page based on it's placement index in the list
CWizardPage* CWizardDlg::GetPage(int nPage) const
{
POSITION Pos = m_PageList.FindIndex(nPage);
if (Pos == NULL) return NULL;
return (CWizardPage*)m_PageList.GetAt(Pos);
}
// activate a page based on its place in the list
BOOL CWizardDlg::SetActivePage(int nPage)
{
CWizardPage* pPage = GetPage(nPage);
if (pPage == NULL) return FALSE;
ActivatePage(pPage);
return TRUE;
}
BOOL CWizardDlg::SetActivePage(CWizardPage* pPage)
{
ActivatePage(pPage);
return TRUE;
}
// set the title of the main wizard window
void CWizardDlg::SetTitle(LPCTSTR lpszText)
{
ASSERT(::IsWindow(m_hWnd));
SetWindowText(lpszText);
}
void CWizardDlg::SetTitle(UINT nIDText)
{
CString s;
s.LoadString(nIDText);
SetTitle(s);
}
// set the text on the Finish button
void CWizardDlg::SetFinishText(LPCTSTR lpszText)
{
ASSERT(::IsWindow(m_hWnd));
CWnd* pWnd = GetDlgItem(ID_WIZFINISH);
ASSERT(pWnd); // You must have an ID_WIZFINISH on your dialog
if (pWnd)
{
pWnd->SetWindowText(lpszText);
}
}
void CWizardDlg::SetFinishText(UINT nIDText)
{
CString s;
s.LoadString(nIDText);
SetFinishText(s);
}
// user pressed the cancel button
void CWizardDlg::OnCancel()
{
CWizardPage* pPage;
POSITION Pos = m_PageList.GetHeadPosition();
while (Pos)
{
pPage = (CWizardPage*)m_PageList.GetNext(Pos);
if (pPage->m_bActive)
{
//调用该页面的退出函数
if (pPage->OnQueryCancel() == FALSE) return;
}
}
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -