作用:
定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换.本模式使得
算法可独立于使用它的客户而变化.
UML结构图:
解析:
简而言之一句话,Strategy模式是对算法的封装.处理一个问题的时候可能有多
种算法,这些算法的接口(输入参数,输出参数等)都是一致的,那么可以考虑采用
Strategy模式对这些算法进行封装,在基类中定义一个函数接口就可以了.
Context |
ContextInterface() |
Strategy |
AlgorithmInterface() |
ConcreateStrategyC |
AlgorithmInterface() |
ConcreateStrategyBH |
AlgorithmInterface() |
ConcreateStrategyA |
AlgorithmInterface() |
#define STRATEGY_H
class Strategy;
class Context
... {
public:
Context(Strategy * pStrategy);
~Context();
void ContextInterface();
private:
Strategy * m_pStrategy;
} ;
class Strategy
... {
public:
virtual ~Strategy() ...{}
virtual void AlgorithmInterface() = 0;
} ;
class ConcreateStrategyA: public Strategy
... {
public:
virtual ~ConcreateStrategyA() ...{}
virtual void AlgorithmInterface();
} ;
#endif
#include " Strategy.h "
Context::Context(Strategy * pStrategy)
: m_pStrategy(pStrategy)
... {
}
Context:: ~ Context()
... {
delete m_pStrategy;
m_pStrategy = NULL;
}
void Context::ContextInterface()
... {
if( NULL != m_pStrategy)
...{
m_pStrategy->AlgorithmInterface();
}
}
void ConcreateStrategyA::AlgorithmInterface()
... {
std::cout<<"AlgorithmInterface Implemented by ConcreateStrategyA ";
}
int main()
... {
Strategy * pStrategy = new ConcreateStrategyA();
Context * pContext = new Context(pStrategy);
pContext->ContextInterface();
delete pContext;
return 0;
}
一个复杂点的例子:
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__9E6E74A8_83F9_11D2_B751_50A250C10000__INCLUDED_)
#define AFX_STDAFX_H__9E6E74A8_83F9_11D2_B751_50A250C10000__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include < afxwin.h > // MFC core and standard components
#include < afxext.h > // MFC extensions
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include < afxcmn.h > // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
// {{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__9E6E74A8_83F9_11D2_B751_50A250C10000__INCLUDED_)
Filler.h
#define _FILLER
#include " stdafx.h "
// List of predefined constants
#ifndef SUCCESS
#define SUCCESS 0
#endif
#ifndef FAILURE
#define FAILURE -1
#endif
// Class CFiller for filling a rectangle
class CFiller
... {
// Constructor and Destructor
public :
CFiller();
virtual ~CFiller();
// Services
public :
INT SetFillerText( LPCSTR );
INT SetFillerRange( INT, INT );
INT SetFillerPos( INT );
INT GetFillerPos();
COLORREF SetFillerColor( COLORREF & );
COLORREF SetFillerBkColor( COLORREF & );
COLORREF SetFillerTextColor( COLORREF & );
virtual INT DoFill( CWnd *, const CRect & ) = 0;
// Attributes
protected :
CString m_csText;
INT m_nMinVal;
INT m_nMaxVal;
INT m_nPos;
COLORREF m_FillerColor;
COLORREF m_FillerBkColor;
COLORREF m_FillerTextColor;
} ;
// Class CLToRFiller for Left to Right Filler
class CLToRFiller : public CFiller
... {
// Constructor and Destructor
public :
CLToRFiller();
virtual ~CLToRFiller();
// Services
public :
virtual INT DoFill( CWnd *, const CRect & );
} ;
// Class CRToLFiller for Right to Left Filler
class CRToLFiller : public CFiller
... {
// Constructor and Destructor
public :
CRToLFiller();
virtual ~CRToLFiller();
// Services
public :
virtual INT DoFill( CWnd *, const CRect & );
} ;
#endif
Filler.cpp
#include " Filler.h "
// CFiller - Implementation
CFiller::CFiller()
... {
m_csText = _T("");
m_nMinVal = 0;
m_nMaxVal = 100;
m_nPos = 0;
m_FillerColor = RGB( 0, 0, 255 );
m_FillerBkColor = RGB( 255, 0, 0 );
m_FillerTextColor = RGB( 255, 255, 255 );
}
CFiller:: ~ CFiller()
... {
}
INT CFiller::SetFillerText( LPCSTR lpszText )
... {
// Step 1 - Set Filler Text
m_csText = lpszText;
return SUCCESS;
}
INT CFiller::SetFillerRange( INT nMinVal, INT nMaxVal )
... {
// Step 1 - Set MinVal and MaxVal after validating
if( nMaxVal <= nMinVal )
...{
return FAILURE;
}
m_nMinVal = nMinVal;
m_nMaxVal = nMaxVal;
return SUCCESS;
}
INT CFiller::GetFillerPos()
... {
// Step 1 - Return m_nPos
return m_nPos;
}
INT CFiller::SetFillerPos( INT nPos )
... {
// Step 1 - Validate pos before setting
if( nPos < m_nMinVal || nPos > m_nMaxVal )
...{
return FAILURE;
}
// Step 2 - Store old filler pos
INT nOldPos = m_nPos;
// Step 3 - Set m_nPos
m_nPos = nPos;
// Step 4 - Return old filler pos
return nOldPos;
}
COLORREF CFiller::SetFillerColor( COLORREF & rFillerColor )
... {
// Step 1 - Store old color
COLORREF OldFillerColor = m_FillerColor;
// Step 2 - Set m_FillerColor
m_FillerColor = rFillerColor;
// Step 3 - Return old filler color
return OldFillerColor;
}
COLORREF CFiller::SetFillerBkColor( COLORREF & rFillerBkColor )
... {
// Step 1 - Store old bk color
COLORREF OldFillerBkColor = m_FillerBkColor;
// Step 2 - Set m_FillerBkColor
m_FillerBkColor = rFillerBkColor;
// Step 3 - Return old filler bk color
return OldFillerBkColor;
}
COLORREF CFiller::SetFillerTextColor( COLORREF & rFillerTextColor )
... {
// Step 1 - Store old text color
COLORREF OldFillerTextColor = m_FillerTextColor;
// Step 2 - Set m_FillerTextColor
m_FillerTextColor = rFillerTextColor;
// Step 3 - Return old filler text color
return OldFillerTextColor;
}
// CLtoRFiller - Implementation
CLToRFiller::CLToRFiller() : CFiller()
... {
}
CLToRFiller:: ~ CLToRFiller()
... {
}
INT CLToRFiller::DoFill( CWnd * pWnd, const CRect & rRect )
... {
CRect Rect = rRect;
// Step 1 - Validate window pointer
ASSERT( IsWindow( pWnd->m_hWnd ) );
// Step 2 - Calculate Fill Area
INT nRight = 0;
nRight = ( rRect.Width() * m_nPos ) / ( m_nMaxVal - m_nMinVal );
CRect FillArea( rRect.left, rRect.top, nRight, rRect.bottom );
CRect RemainingArea( nRight, rRect.top, rRect.right, rRect.bottom );
CDC * pDC = pWnd->GetDC();
pDC->FillSolidRect( FillArea, m_FillerColor );
pDC->FillSolidRect( RemainingArea, m_FillerBkColor );
// Step 3 - Display text provided or the default %
if( m_csText.IsEmpty() == FALSE )
...{
pDC->SetBkMode( TRANSPARENT );
pDC->SetTextColor( m_FillerTextColor );
pDC->DrawText( m_csText, m_csText.GetLength(),
Rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER | DT_SINGLELINE );
}
else if( m_nPos > 0 )
...{
CHAR szText[ 512 ];
memset( szText, 0, sizeof( szText ) );
INT nPercent = ( INT )( ( ( double ) m_nPos / ( double ) m_nMaxVal ) * 100 );
sprintf( szText, "%d%%", nPercent );
pDC->SetBkMode( TRANSPARENT );
pDC->SetTextColor( m_FillerTextColor );
pDC->DrawText( szText, strlen( szText ),
Rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER | DT_SINGLELINE );
}
pWnd->ReleaseDC( pDC );
return SUCCESS;
}
// CRToLFiller - Implementation
CRToLFiller::CRToLFiller() : CFiller()
... {
}
CRToLFiller:: ~ CRToLFiller()
... {
}
INT CRToLFiller::DoFill( CWnd * pWnd, const CRect & rRect )
... {
CRect Rect = rRect;
// Step 1 - Validate window pointer
ASSERT( IsWindow( pWnd->m_hWnd ) );
// Step 2 - Calculate Fill Area
INT nLeft = 0;
nLeft = ( rRect.Width() * m_nPos ) / ( m_nMaxVal - m_nMinVal );
nLeft = rRect.Width() - nLeft;
CRect FillArea( nLeft, rRect.top, rRect.right, rRect.bottom );
CRect RemainingArea( rRect.left, rRect.top, nLeft, rRect.bottom );
CDC * pDC = pWnd->GetDC();
pDC->FillSolidRect( FillArea, m_FillerColor );
pDC->FillSolidRect( RemainingArea, m_FillerBkColor );
// Step 3 - Display text provided or the default %
if( m_csText.IsEmpty() == FALSE )
...{
pDC->SetBkMode( TRANSPARENT );
pDC->SetTextColor( m_FillerTextColor );
pDC->DrawText( m_csText, m_csText.GetLength(),
Rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER | DT_SINGLELINE );
}
else if( m_nPos > 0 )
...{
CHAR szText[ 512 ];
memset( szText, 0, sizeof( szText ) );
INT nPercent = ( INT )( ( ( double ) m_nPos / ( double ) m_nMaxVal ) * 100 );
sprintf( szText, "%d%%", nPercent );
pDC->SetBkMode( TRANSPARENT );
pDC->SetTextColor( m_FillerTextColor );
pDC->DrawText( szText, strlen( szText ),
Rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER | DT_SINGLELINE );
}
pWnd->ReleaseDC( pDC );
return SUCCESS;
}
ProgInd.h
//
#ifndef _PROGRESS_INDICATOR
#define _PROGRESS_INDICATOR
// Forward declaration for the class CFiller
class CFiller;
/**/ /////
// CProgressIndicator window
class CProgressIndicator : public CWnd
... {
// Construction
public:
CProgressIndicator( CFiller * = NULL );
// Attributes
protected:
CFiller * m_pFiller;
BOOL m_bCreated;
// Attributes
public:
// Operations
public:
CFiller * GetFiller();
INT SetFiller( CFiller * );
INT SetText( LPCSTR );
INT SetRange( INT, INT );
INT GetPos();
INT SetPos( INT );
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CProgressIndicator)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CProgressIndicator();
// Generated message map functions
protected:
//{{AFX_MSG(CProgressIndicator)
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
} ;
/**/ /////
#endif
ProgInd.cpp
//
#include " stdafx.h "
#include " ProgInd.h "
#include " Filler.h "
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/**/ /////
// CProgressIndicator
CProgressIndicator::CProgressIndicator( CFiller * pFiller ) : m_pFiller( pFiller )
... {
// Step 1 - Set m_bCreated to FALSE
m_bCreated = FALSE;
// Step 2 - Check and create m_pFiller
if( m_pFiller == NULL )
...{
m_pFiller = new CLToRFiller;
m_bCreated = TRUE;
}
// Step 3 - Validate m_pFiller
ASSERT( m_pFiller != NULL );
}
CProgressIndicator:: ~ CProgressIndicator()
... {
// Step 1 - Check and delete m_pFiller
if( m_bCreated == TRUE )
...{
delete m_pFiller;
}
}
BEGIN_MESSAGE_MAP(CProgressIndicator, CWnd)
// {{AFX_MSG_MAP(CProgressIndicator)
ON_WM_PAINT()
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/**/ /////
// CProgressIndicator methods
CFiller * CProgressIndicator::GetFiller()
... {
// Step 1 - Return m_pFiller
return m_pFiller;
}
INT CProgressIndicator::SetFiller( CFiller * pFiller )
... {
// Step 1 - Validate pFiller
ASSERT( pFiller != NULL );
// Step 2 - Check and delete m_pFiller
if( m_bCreated == TRUE )
...{
delete m_pFiller;
m_bCreated = FALSE;
}
// Step 3 - Set m_pFiller to the parameter passed
m_pFiller = pFiller;
return SUCCESS;
}
INT CProgressIndicator::SetText( LPCSTR lpszText )
... {
// Step 1 - Delegate and forward this to CFiller object
INT nRetVal = m_pFiller->SetFillerText( lpszText );
Invalidate();
return nRetVal;
}
INT CProgressIndicator::SetRange( INT nMinRange, INT nMaxRange )
... {
// Step 1 - Delegate and forward this to CFiller object
return m_pFiller->SetFillerRange( nMinRange, nMaxRange );
}
INT CProgressIndicator::GetPos()
... {
// Step 1 - Delegate and forward this to CFiller object
return m_pFiller->GetFillerPos();
}
INT CProgressIndicator::SetPos( INT nPos )
... {
// Step 1 - Delegate and forward this to CFiller object
INT nOldPos = m_pFiller->SetFillerPos( nPos );
Invalidate();
return nOldPos;
}
/**/ /////
// CProgressIndicator message handlers
void CProgressIndicator::OnPaint()
... {
CPaintDC dc(this); // device context for painting
// Step 1 - Get the client Rectangle
CRect Rect;
GetClientRect( & Rect );
// Step 2 - Call DoFill to do the filling, based on the current situation
m_pFiller->DoFill( this, Rect );
}
Strategy.h
//
#if !defined(AFX_STRATEGY_H__9E6E74A4_83F9_11D2_B751_50A250C10000__INCLUDED_)
#define AFX_STRATEGY_H__9E6E74A4_83F9_11D2_B751_50A250C10000__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include " resource.h " // main symbols
/**/ /////
// CStrategyApp:
// See Strategy.cpp for the implementation of this class
//
class CStrategyApp : public CWinApp
... {
public:
CStrategyApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CStrategyApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CStrategyApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
} ;
/**/ /////
// {{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STRATEGY_H__9E6E74A4_83F9_11D2_B751_50A250C10000__INCLUDED_)
Strategy.cpp
//
#include " stdafx.h "
#include " Strategy.h "
#include " StrategyDlg.h "
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/**/ /////
// CStrategyApp
BEGIN_MESSAGE_MAP(CStrategyApp, CWinApp)
// {{AFX_MSG_MAP(CStrategyApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
// }}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/**/ /////
// CStrategyApp construction
CStrategyApp::CStrategyApp()
... {
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/**/ /////
// The one and only CStrategyApp object
CStrategyApp theApp;
/**/ /////
// CStrategyApp initialization
BOOL CStrategyApp::InitInstance()
... {
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CStrategyDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
...{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
...{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
StrategyDlg.h
//
#if !defined(AFX_STRATEGYDLG_H__9E6E74A6_83F9_11D2_B751_50A250C10000__INCLUDED_)
#define AFX_STRATEGYDLG_H__9E6E74A6_83F9_11D2_B751_50A250C10000__INCLUDED_
#include " ProgInd.h "
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
/**/ /////
// CStrategyDlg dialog
class CStrategyDlg : public CDialog
... {
// Construction
public:
CStrategyDlg(CWnd* pParent = NULL); // standard constructor
virtual ~CStrategyDlg();
// Dialog Data
//{{AFX_DATA(CStrategyDlg)
enum ...{ IDD = IDD_STRATEGY_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CStrategyDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CStrategyDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnAbout();
afx_msg void OnStartfill();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
virtual void OnCancel();
// Attributes
protected :
CProgressIndicator * m_pProgInd1;
CProgressIndicator * m_pProgInd2;
} ;
// {{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STRATEGYDLG_H__9E6E74A6_83F9_11D2_B751_50A250C10000__INCLUDED_)
StrategyDlg.cpp
//
#include " stdafx.h "
#include " Strategy.h "
#include " StrategyDlg.h "
#include " Filler.h "
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/**/ /////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
... {
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum ...{ IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
} ;
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
... {
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange * pDX)
... {
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
// {{AFX_MSG_MAP(CAboutDlg)
// No message handlers
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/**/ /////
// CStrategyDlg dialog
CStrategyDlg::CStrategyDlg(CWnd * pParent /**/ /*=NULL*/ )
: CDialog(CStrategyDlg::IDD, pParent)
... {
//{{AFX_DATA_INIT(CStrategyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// Step 1 - Create two progress indicators
m_pProgInd1 = new CProgressIndicator; // Use default filler object
ASSERT( m_pProgInd1 != NULL );
m_pProgInd2 = new CProgressIndicator( new CRToLFiller );
ASSERT( m_pProgInd2 != NULL );
}
CStrategyDlg:: ~ CStrategyDlg()
... {
// Step 1 - Get Filler object and delete
CFiller * pRToLFiller = m_pProgInd2->GetFiller();
delete pRToLFiller;
// Step 2 - Do cleanup
delete m_pProgInd1;
delete m_pProgInd2;
}
void CStrategyDlg::DoDataExchange(CDataExchange * pDX)
... {
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStrategyDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStrategyDlg, CDialog)
// {{AFX_MSG_MAP(CStrategyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_BN_CLICKED(IDABOUT, OnAbout)
ON_BN_CLICKED(IDSTARTFILL, OnStartfill)
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/**/ /////
// CStrategyDlg message handlers
// 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 CStrategyDlg::OnPaint()
... {
if (IsIconic())
...{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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
...{
CDialog::OnPaint();
}
}
void CStrategyDlg::OnSysCommand(UINT nID, LPARAM lParam)
... {
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
...{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
...{
CDialog::OnSysCommand(nID, lParam);
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CStrategyDlg::OnQueryDragIcon()
... {
return (HCURSOR) m_hIcon;
}
BOOL CStrategyDlg::OnInitDialog()
... {
CDialog::OnInitDialog();
// Step 1 - 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)
...{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
...{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Step 2 - 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
// Step 3 - Call base class method to do the initialization
CDialog::OnInitDialog();
// Step 4 - Create Progress controls
CWnd * pWnd = GetDlgItem( IDC_PI_CTRL1 );
ASSERT(pWnd);
CRect r;
pWnd->GetWindowRect( r );
ScreenToClient( r );
VERIFY( m_pProgInd1->Create( NULL, "PI1", WS_CHILD | WS_VISIBLE | WS_BORDER,
r, this, IDC_PI_CTRL1 ) );
pWnd = GetDlgItem( IDC_PI_CTRL2 );
ASSERT(pWnd);
pWnd->GetWindowRect( r );
ScreenToClient( r );
VERIFY( m_pProgInd2->Create( NULL, "PI2", WS_CHILD | WS_VISIBLE | WS_BORDER,
r, this, IDC_PI_CTRL2 ) );
m_pProgInd2->SetText( "Please wait..." );
return TRUE;
}
void CStrategyDlg::OnTimer(UINT nIDEvent)
... {
// Step 1 - Set position for status controls
m_pProgInd1->SetPos( m_pProgInd1->GetPos() + 5 );
m_pProgInd2->SetPos( m_pProgInd2->GetPos() + 5 );
// Step 2 - Kill the timer, if needed
if( m_pProgInd1->GetPos() >= 100 )
...{
KillTimer(1);
}
CDialog::OnTimer(nIDEvent);
}
void CStrategyDlg::OnAbout()
... {
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CStrategyDlg::OnCancel()
... {
CDialog::OnCancel();
}
void CStrategyDlg::OnStartfill()
... {
// Step 1 - Reset positions when timer is newly started
m_pProgInd1->SetPos( 0 );
m_pProgInd2->SetPos( 0 );
// Step 2 - Start the timer
SetTimer(1, 100, NULL);
}