Wine >> C/C++

Set Timer


參考資訊:
1. win32
2. petzold
3. tutorial
4. examples_win32

Windows的Timer中斷週期是基於早期系統架構,因此,最短的Timer時間間隔為15ms,因此,即使Timer設定為1ms,觸發時間依舊為15ms

main.c

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>

int cnt = 0;
HWND hWin = NULL;
WNDPROC defWndProc = NULL;

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    char buf[255] = {0};

    switch (uMsg) {
    case WM_TIMER:
        sprintf(buf, "%d", cnt++);
        SetWindowText(hWnd, buf);
        break;
    case WM_CLOSE:
        DestroyWindow(hWnd);
        return 0;
    case WM_DESTROY:
        KillTimer(hWnd, 1);
        PostQuitMessage(0);
        return 0;
    }
    return CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    hWin = CreateWindow(WC_DIALOG, "main", 
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL);
    defWndProc = (WNDPROC)SetWindowLongPtr(hWin, GWLP_WNDPROC, (long int)WndProc);
    SetTimer(hWin, 1, 1000, NULL);

    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
    }
    ExitProcess(0);
    return 0;
}

Line 15~17:累加數值並且顯示在視窗標題
Line 23:不使用Timer,記得關閉Timer
Line 35:設定Timer為每秒(1000ms)觸發一次,ID=1

編譯、執行

$ winegcc main.c -o main
$ wine ./main.exe


返回上一頁