程式語言 - MASM32 - Set Timer



參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32

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

main.asm

    .386
    .model flat,stdcall
    option casemap:none
       
include c:\masm32\include\msvcrt.inc
include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\user32.inc
       
includelib c:\masm32\lib\msvcrt.lib
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib
       
    .data
szCaption   BYTE "main",0
hWin        DWORD 0
hInstance   DWORD 0
pCmdLine    DWORD 0
defWndProc  DWORD 0
cnt         DWORD 0
buf         BYTE 255 dup(0)
fmt         BYTE "%d",0
       
    .code
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    .if uMsg == WM_TIMER
        inc cnt
        invoke crt_sprintf, offset buf, offset fmt, cnt
        invoke SetWindowText, hWnd, offset buf
        xor eax, eax
        ret
    .elseif uMsg == WM_CLOSE
        invoke DestroyWindow, hWnd
        xor eax, eax
        ret
    .elseif uMsg == WM_DESTROY
        invoke KillTimer, hWnd, 1
        invoke PostQuitMessage, 0
        xor eax, eax
        ret
    .endif
     
    invoke CallWindowProc, defWndProc, hWnd, uMsg, wParam, lParam
    ret
WndProc endp
      
WinMain proc hInst:DWORD, hPrevInst:DWORD, CmdLine:DWORD, CmdShow:DWORD
    local msg:MSG
       
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, offset szCaption, WS_OVERLAPPEDWINDOW or WS_VISIBLE, 0, 0, 320, 240, NULL, NULL, NULL, NULL
    mov hWin, eax
      
    invoke SetWindowLong, hWin, GWL_WNDPROC, WndProc
    mov defWndProc, eax
  
    invoke SetTimer, hWin, 1, 1000, NULL

@@:
    invoke GetMessage, addr msg, NULL, 0, 0
    cmp eax, 0
    je @f
    invoke DispatchMessage, addr msg
    jmp @b

@@:
    mov eax, msg.wParam
    ret
WinMain endp
     
start:
    invoke GetModuleHandle, NULL
    mov hInstance, eax
       
    invoke GetCommandLine
    mov pCmdLine, eax
       
    invoke WinMain, hInstance, NULL, pCmdLine, SW_SHOWDEFAULT
    invoke ExitProcess, 0
end start

完成