MFC 定时器

定义定时器ID

#define TIMER_ID_CONTROL1    10000
#define TIMER_ID_CONTROL2    10001
#define TIMER_ID_CALLBACK    10002

 

 

设置定时器

    SetTimer(TIMER_ID_CONTROL1, 1*1000, NULL);//如果设置的等待时间比处理时间短,程序会出问题
    SetTimer(TIMER_ID_CONTROL2, 2*1000, NULL);
    SetTimer(TIMER_ID_CALLBACK, 3*1000, (TIMERPROC)TimerProc);

 

 

销毁定时器

    KillTimer(TIMER_ID_CONTROL1);
    KillTimer(TIMER_ID_CONTROL2);
    KillTimer(TIMER_ID_CALLBACK);

 

 

Ontimer

void CMFCTimerDlg::OnTimer(UINT_PTR nIDEvent)
{
    CString str;
    switch (nIDEvent)
    {
    case TIMER_ID_CONTROL1:
        {
            str.Format("CONTROL1 : %d", m_uiTimeClickControl1++);
            SetDlgItemText(IDC_EDIT1, str);
        }
        break;
    case TIMER_ID_CONTROL2:
        {
            str.Format("CONTROL2 : %d", m_uiTimeClickControl2++);
            SetDlgItemText(IDC_EDIT2, str);
        }
        break;
    }

    CDialog::OnTimer(nIDEvent);
}

 

回调例子

  static void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);


#include <TlHelp32.h> void CALLBACK CMFCTimerDlg::TimerProc( HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime ) { switch (idEvent) { case TIMER_ID_CALLBACK: { size_t uCount = 0; HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (INVALID_HANDLE_VALUE != hSnap) { PROCESSENTRY32 item; item.dwSize = sizeof(item); if (Process32First(hSnap, &item)) { CString str; do { str = item.szExeFile; if (0 == str.CompareNoCase("notepad.exe")) { uCount++; } } while (Process32Next(hSnap, &item)); } CloseHandle(hSnap); hSnap = INVALID_HANDLE_VALUE; } if (uCount < 2) { STARTUPINFO si; PROCESS_INFORMATION pi; memset(&pi, 0, sizeof(pi)); memset(&si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO);//设定结构的大小 si.dwFlags = STARTF_USESHOWWINDOW;//以最小化运行 si.wShowWindow = SW_SHOWMINNOACTIVE;//以最小化运行 CreateProcess(NULL, "notepad.exe", NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi); } } break; } }

 

posted on 2016-11-02 16:05  NoneButNow  阅读(236)  评论(0编辑  收藏  举报

导航