程式語言 - Wine - C/C++ - Set Timer



參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32
http://masm32.com/board/index.php?topic=3584.0
https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles

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

KillTimer需要指定要Kill的Timer ID

main.c

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
 
static HWND hWin = NULL;
static WNDPROC defWndProc = NULL;
 
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static int cnt = 0;
    char buf[255] = { 0 };
 
    switch (uMsg) {
    case WM_TIMER:
        sprintf(buf, "Cnt: %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, 320, 240, 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);
    }

    return 0;
}

編譯、執行

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