使用定时器3

时间:2021-08-23 00:11:16

三、程序代码

/////////////////////////////////////////// MyTimer.h: interface for the CMyTimer class.
#if !defined(AFX_MYTIMER_H__D97674D1_B221_49CD_9637_4CBA8C3180CE__INCLUDED_)
#define AFX_MYTIMER_H__D97674D1_B221_49CD_9637_4CBA8C3180CE__INCLUDED_
#include <afxtempl.h>
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMyTimer;
typedef CMap<UINT,UINT,CMyTimer*,CMyTimer*> CTimerMap;
class CMyTimer
{
 public:
  //设置定时器,nElapse表示时间间隔,sz表示要提示的内容
  void SetMyTimer(UINT nElapse,CString sz);
  //销毁该实例的定时器
  void KillMyTimer();
  //保存该实例的定时器标志值
  UINT m_nTimerID;
  //静态数据成员要提示的内容
  CString szContent;
  //静态数据成员,映射表类,用于保存所有的定时器信息
  static CTimerMap m_sTimeMap;
  //静态成员函数,用于处理定时器的消息
  static void CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);

  CMyTimer();
  virtual ~CMyTimer();
};
#endif

///////////////////////////////////// MyTimer.cpp: implementation of the CMyTimer class.
#include "stdafx.h"
#include "TimerDemo.h"
#include "MyTimer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CTimerMap CMyTimer::m_sTimeMap;

CMyTimer::CMyTimer()
{
 m_nTimerID = 0;
}

CMyTimer::~CMyTimer()
{}

void CALLBACK CMyTimer::MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
 CString sz;
 sz.Format("%d号定时器:%s",idEvent,m_sTimeMap[idEvent]->szContent);
 AfxMessageBox(sz);
}

void CMyTimer::SetMyTimer(UINT nElapse,CString sz)
{
 szContent = sz;
 m_nTimerID = SetTimer(NULL,NULL,nElapse,MyTimerProc);
 m_sTimeMap[m_nTimerID] = this;
}

void CMyTimer::KillMyTimer()
{
 KillTimer(NULL,m_nTimerID);
 m_sTimeMap.RemoveKey(m_nTimerID);
}

//////////////////////////////////////////////////////// TimerDemoDlg.h : header file
#if !defined(AFX_TIMERDEMODLG_H__83D29A02_A119_4900_AB57_D6D011547F90__INCLUDED_)
#define AFX_TIMERDEMODLG_H__83D29A02_A119_4900_AB57_D6D011547F90__INCLUDED_
#include "Person.h" // Added by ClassView
#include "MyTimer.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////// CTimerDemoDlg dialog
class CTimerDemoDlg : public CDialog
{
 // Construction
 public:
  CMyTimer m_myTimer3;
  CMyTimer m_myTimer2;
  CMyTimer m_myTimer1;
  CPerson m_Person;
  CTimerDemoDlg(CWnd* pParent = NULL); // standard constructor
  //{{AFX_DATA(CTimerDemoDlg)
   enum { IDD = IDD_TIMERDEMO_DIALOG };
   // NOTE: the ClassWizard will add data members here
  //}}AFX_DATA
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CTimerDemoDlg)
 protected:
  virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  //}}AFX_VIRTUAL
  // Implementation
 protected:
  HICON m_hIcon;
  // Generated message map functions
  //{{AFX_MSG(CTimerDemoDlg)
  virtual BOOL OnInitDialog();
  afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
  afx_msg void OnPaint();
  afx_msg HCURSOR OnQueryDragIcon();
  afx_msg void OnButton1();
  afx_msg void OnTimer(UINT nIDEvent);
  afx_msg void OnButton2();
  afx_msg void OnButton3();
  afx_msg void OnButton8();
  afx_msg void OnButton9();
  afx_msg void OnButton10();
  afx_msg void OnButton11();
  afx_msg void OnButton12();
  afx_msg void OnButton13();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};
#endif

//////////////////////////////////////////// TimerDemoDlg.cpp : implementation file
#include "stdafx.h"
#include "TimerDemo.h"
#include "TimerDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CTimerDemoDlg::CTimerDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTimerDemoDlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(CTimerDemoDlg)
 // 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);
}

void CTimerDemoDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CTimerDemoDlg)
 // NOTE: the ClassWizard will add DDX and DDV calls here
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTimerDemoDlg, CDialog)
 //{{AFX_MSG_MAP(CTimerDemoDlg)
  ON_WM_SYSCOMMAND()
  ON_WM_PAINT()
  ON_WM_QUERYDRAGICON()
  ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  ON_WM_TIMER()
  ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
  ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
  ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
  ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
  ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
  ON_BN_CLICKED(IDC_BUTTON13, OnButton13)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CTimerDemoDlg::OnInitDialog()
{
 CDialog::OnInitDialog();
 // 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);
  }
 }
 SetIcon(m_hIcon, TRUE); // Set big icon
 SetIcon(m_hIcon, FALSE); // Set small icon
 return TRUE; // return TRUE unless you set the focus to a control
}

void CTimerDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  CDialog::OnSysCommand(nID, lParam);
 }
}

void CTimerDemoDlg::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();
 }
}

HCURSOR CTimerDemoDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}

void CTimerDemoDlg::OnButton1()
{
 m_Person.szMotto = "我的座右铭是:奋斗加运气等于成功!";
 m_Person.GetMottoStaic(m_Person.pThis);
}

void CTimerDemoDlg::OnTimer(UINT nIDEvent)
{
 switch(nIDEvent)
 {
  case 1:
   CTime m_SysTime = CTime::GetCurrentTime();
   SetDlgItemText(IDC_STATIC_TIME,m_SysTime.Format("%Y年%m月%d日 %H:%M:%S"));
   break;
 }
 CDialog::OnTimer(nIDEvent);
}

void CTimerDemoDlg::OnButton2()
{
 SetTimer(1,1000,NULL);
}

void CTimerDemoDlg::OnButton3()
{
 KillTimer(1);
 SetDlgItemText(IDC_STATIC_TIME,"电子钟定时器被关闭了 ->");
}

void CTimerDemoDlg::OnButton8()
{
 m_myTimer1.SetMyTimer(5000,"该起床了!");
}

void CTimerDemoDlg::OnButton9()
{
 m_myTimer2.SetMyTimer(10000,"该工作了!");
}

void CTimerDemoDlg::OnButton10()
{
 m_myTimer3.SetMyTimer(15000,"该睡觉了!");
}

void CTimerDemoDlg::OnButton11()
{
 m_myTimer1.KillMyTimer();
}

void CTimerDemoDlg::OnButton12()
{
 m_myTimer2.KillMyTimer();
}

void CTimerDemoDlg::OnButton13()
{
 m_myTimer3.KillMyTimer();
}


  四、小结

  通过以上的介绍,大家应该知道如何在静态成员函数中访问非静态数据成员和非静态成员函数,并了解了如何在非窗口类中使用定时器。当然这只是解决这个问题的一种方法,相信还有更好的解决办法。这个种方法有一定的灵活性,可以在很多地方用到,例如网络程序中的连接超时以及定时刷新等需要自己来控制,就可以使用这种方法。

 

上一页  1  2  3