分类:
2008-10-13 16:46:27
//CDocIterator 类代码 // DocIter.h // class CDocIterator { protected: CPtrList m_doclist; CDocument* m_pDoc; POSITION m_pos; public: CDocIterator(); // get current doc CDocument* doc() { return m_pDoc; } // move to 1st doc: not needed after construct BOOL First() { m_pDoc = NULL; m_pos = m_doclist.GetHeadPosition(); return Next(); } // move to next doc BOOL Next() { if (m_pos) { m_pDoc = (CDocument*)m_doclist.GetNext(m_pos); } else { m_pDoc=NULL; } return m_pDoc != NULL; } // next, ++ style const CDocIterator& operator++(int) { Next(); return *this; } // for "for" loops BOOL operator()() { return m_pDoc != NULL; } }; DocIter.cpp // #include "stdafx.h" #include "DocIter.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // Constructor intitializes document list and goes to first doc. // CDocIterator::CDocIterator() { CWinApp* pApp = AfxGetApp(); POSITION pos1 = pApp->GetFirstDocTemplatePosition(); while (pos1) { CDocTemplate* ptempl = (CDocTemplate*)pApp->GetNextDocTemplate(pos1); POSITION pos2 = ptempl->GetFirstDocPosition(); while (pos2) { CDocument *pdoc; if ((pdoc=ptempl->GetNextDoc(pos2)) != NULL) { m_doclist.AddHead(pdoc); } } } First(); } //
//Implementing a Timer //////////////////////////////////////////////////////////////// // #include "stdafx.h" #include "doctime.h" #include "Doc.h" #include "DocIter.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNCREATE(CMyDoc, CDocument) BEGIN_MESSAGE_MAP(CMyDoc, CDocument) ON_COMMAND_EX(ID_APPTIMER, OnAppTimer) END_MESSAGE_MAP() int CMyDoc::g_nIDTimer = 0; int CMyDoc::g_nDocObj = 0; // timer proc called by windows whenever the timer clicks // void WINAPI MyTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { CDocIterator it; for (it.First(); it.doc(); it.Next()) { it.doc()->OnCmdMsg(ID_APPTIMER, CN_COMMAND, NULL, NULL); } } // constructor creates timer the first time a doc is created // CMyDoc::CMyDoc() { m_count = 0; if (g_nDocObj++ == 0) { g_nIDTimer = SetTimer(NULL,0,1000,MyTimerProc); } } // destructor kills timer when the last doc goes bye-bye // CMyDoc::~CMyDoc() { if (—g_nDocObj == 0) { KillTimer(NULL,g_nIDTimer); } } // Handle timer event: this is where you would do your background data // collection or whatever you want to do. This sample simply increments a // counter and updates the views. // BOOL CMyDoc::OnAppTimer(UINT nID) { m_count++; UpdateAllViews(NULL); return FALSE; } //
CmdTargList.h //////////////////////////////////////////////////////////////// // #pragma once // List of command targets. Targets can register and unregister, and // SendCommand lets you send a command (CN/WM_COMMAND) to all the targets // on the list. This is a general class that has nothing per se to do // with timers. // class CCmdTargetList { protected: CPtrList m_list; // the list public: // Register command target to receive commands sent to this list // void Register(CCmdTarget* pTarg) { m_list.AddHead(pTarg); } // Unregister command target from list // void Unregister(CCmdTarget* pTarg) { POSITION pos = m_list.Find(pTarg); if (pos) { m_list.RemoveAt(pos); } } // Send command to all command targets on this list // int SendCommand(UINT nID); }; CmdTargList.cpp //////////////////////////////////////////////////////////////// // #include "stdafx.h" #include "doctime.h" #include "CmdTargList.h" #include "AppTimer.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // Send a command to all command targets on this list // int CCmdTargetList::SendCommand(UINT nID) { int count=0; POSITION pos = m_list.GetHeadPosition(); while (pos) { CCmdTarget* pTarg = (CCmdTarget*)m_list.GetNext(pos); if (pTarg) { pTarg->OnCmdMsg(nID, CN_COMMAND, NULL, NULL); count++; } } return count; }
AppTimer.h //////////////////////////////////////////////////////////////// // #include "CmdTargList.h" ////////////////// // Timer object is a list of cmd targets to receive timer command/event. // class CAppTimer : public CCmdTargetList { protected: static void WINAPI CAppTimer::TimerProc(HWND, UINT, UINT_PTR, DWORD); UINT m_nIDTimer; // timer ID UINT m_nIDTimerCmd; // command ID to send when timer clicks public: CAppTimer(); ~CAppTimer(); void Init(int msec, UINT nIDCmd); }; // THE timer extern CAppTimer theTimer; AppTimer.cpp //////////////////////////////////////////////////////////////// // #include "stdafx.h" #include "resource.h" #include "AppTimer.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // global timer object CAppTimer theTimer; void WINAPI CAppTimer::TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { theTimer.SendCommand(theTimer.m_nIDTimerCmd); } CAppTimer::CAppTimer() { ASSERT(this==&theTimer); // only one global timer allowed } CAppTimer::~CAppTimer() { KillTimer(NULL, m_nIDTimer); } // Initialize: set timer and command ID to use // void CAppTimer::Init(int msec, UINT nIDCmd) { ASSERT(this==&theTimer); m_nIDTimerCmd = nIDCmd; m_nIDTimer = SetTimer(NULL, 0, msec, TimerProc); }