gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:36:36 26 4
gpt4 key购买 nike

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

最佳答案

这是 Hans 评论的一个实现。单击该按钮会显示距离下一个预定(预期)WM_TIMER 消息的剩余时间。通过在 Debug模式下构建,我得到了一个控制台来显示我的消息,因为这是一种既快速又简单(阅读:肮脏)的方式来获取 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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com