简体   繁体   English

Windows上的C语言计时器

[英]Timer in C language on Windows

I am making a Quiz program. 我正在制作测验程序。 A question is displayed and a timer starts. 显示问题并启动计时器。 The user has to answer the question in 30 seconds. 用户必须在30秒内回答问题。 If he doesn't answer the question during the 30 seconds, the timer ends and the next question is displayed. 如果他在30秒钟内未回答问题,计时器将结束并显示下一个问题。 The problem is that all the TIMER programs i have come across stop the program ie user won't be able to input his answer during that timer 问题是我遇到的所有TIMER程序都会停止该程序,即用户将无法在该计时器期间输入答案

The correct way to do this is to not use threads or 0.1-second sleeps. 正确的方法是使用线程或0.1秒的睡眠。

You should get a little Windows application from some tutorial (you recognize a typical example Windows application by a WinMain() instead of a main() ). 您应该从一些教程中获得一个小的Windows应用程序(通过WinMain()而不是main()来识别典型的示例Windows应用程序)。

You can then add these functions: 然后,您可以添加以下功能:

SetTimer and KillTimer SetTimerKillTimer

Here's a full working example: It creates a window with a text-input, gives it focus and sets a timer to 1 second. 这是一个完整的工作示例:它创建一个带有文本输入的窗口,使其具有焦点并将计时器设置为1秒。 When the timer expires it gets the text from the text-input and shows it in a dialog-box. 计时器到期时,它将从文本输入中获取文本,并将其显示在对话框中。

#include <windows.h>

HWND hwndMain = 0;
HWND hwndEdit = 0;
#define idTimer 123
VOID CALLBACK TimerProc(HWND w, UINT msg, UINT_PTR e, DWORD t) {
  Beep(2000, 10);
  KillTimer(hwndMain, idTimer);
  char tmp[100];
  GetWindowText(hwndEdit, tmp, 100);
  MessageBox(0, tmp, "you entered:", 0);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
{
  #define id_note 50

    switch(msg) {
      case WM_CREATE:
        hwndMain = hwnd;
        hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",
        "",WS_CHILD|WS_VISIBLE,
        0,0,80,20,hwnd,(HMENU)id_note,GetModuleHandle(0), 0);
        SetFocus(hwndEdit);
        SetTimer(hwndMain, idTimer, 1000, TimerProc);
      break;
      case WM_CLOSE:
          DestroyWindow(hwnd);
      break;
      case WM_DESTROY:
          PostQuitMessage(0);
      break;
      default:
        return DefWindowProc(hwnd, msg, w, l);
    }
    return 0;
}

const char g_szClassName[] = "myWindowClass";
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    memset(&wc, 0, sizeof(WNDCLASSEX));
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(0, IDI_APPLICATION);

    RegisterClassEx(&wc);

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        0, 0, hInstance, 0);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, 0, 0, 0) > 0) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM