Reputation: 1044
Is there any way to get the current progress of a timer, which is created by the SetTimer function?
Upvotes: 0
Views: 651
Reputation: 13109
Here's an implementation of Hans' comment. Clicking the button shows the time remaining until the next scheduled (expected) WM_TIMER message. By building in debug mode, I get a console to display my messages, since it's such a quick and easy (read: dirty) way of getting both a GUI and a console.
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"
Upvotes: 2