如何获取使用SetTimer()制作的计时器的进度

时间:2014-10-31 15:53:52

标签: c++ mfc

有没有办法获取由SetTimer函数创建的计时器的当前进度?

1 个答案:

答案 0 :(得分:1)

这是Hans'的实施。评论。单击该按钮可显示下一个计划的(预期的)WM_TIMER消息之前的剩余时间。通过在调试模式下构建,我得到一个控制台来显示我的消息,因为它是一种快速简单(读取:脏)的方式来获取GUI和控制台。

<强>的main.cpp

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"

HINSTANCE hInst;

DWORD tickCountInitial;
DWORD timerInterval = 10000;
DWORD nextTimerFired;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
        {
            tickCountInitial = GetTickCount();
            SetTimer(hwndDlg, 666, timerInterval, NULL);
            nextTimerFired = tickCountInitial + timerInterval;
        }
        return TRUE;

        case WM_TIMER:
        {
            DWORD tickCountCurrent = GetTickCount();
            printf("Ticks elapsed: %d\n", tickCountCurrent - tickCountInitial);
            nextTimerFired = tickCountCurrent + timerInterval;
            MessageBeep(MB_OK);
        }
        return 0;

        case WM_CLOSE:
        {
            EndDialog(hwndDlg, 0);
        }
        return TRUE;

        case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
                case IDC_BUTTON1:
                {
                    DWORD tickCountCurrent = GetTickCount();
                    printf("Ticks till next WM_TIMER message: %d\n", nextTimerFired - tickCountCurrent);
                }
                break;
            }
        }
        return TRUE;
    }
    return FALSE;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

<强> RESOURCE.H

#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define DLG_MAIN                                100
#define IDC_BUTTON1                             40000

<强> RESOURCE.RC

// Generated by ResEdit 1.6.2
// Copyright (C) 2006-2014
// http://www.resedit.net

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"




//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOG 0, 0, 186, 95
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
    PUSHBUTTON      "Check Remaining", IDC_BUTTON1, 41, 32, 104, 31, 0, WS_EX_LEFT
}



//
// Manifest resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
1                  RT_MANIFEST    ".\\manifest.xml"