有什么方法可以获取由SetTimer函数创建的计时器的当前进度吗?

最佳答案

这是汉斯评论的实现。单击按钮显示到下一个计划的(预期的)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);
}


资源.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"

关于c++ - 如何获得使用SetTimer()制作的计时器的进度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26678666/

10-11 23:12