程式語言 - Flat Assembler (FASM) - Win32 API - Set Timer



參考資訊:
https://board.flatassembler.net/

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

main.asm

    include "head.asm"
   
    section ".text" code readable executable
proc WndProc hWnd, uMsg, wParam, lParam
    mov eax, [uMsg]
    cmp eax, WM_TIMER
    je .handle_timer
    cmp eax, WM_CLOSE
    je .handle_close
    cmp eax, WM_DESTROY
    je .handle_destroy
    invoke CallWindowProc, [pDefWndProc], [hWnd], [uMsg], [wParam], [lParam]
    jmp .finish
   
.handle_timer:
    inc dword [dwCnt]
    invoke wsprintf, szBuf, FMT_TIMER, [dwCnt]
    invoke SetWindowText, [hWnd], szBuf
    xor eax, eax
    jmp .finish
   
.handle_close:
    invoke DestroyWindow, [hWnd]
    xor eax, eax
    jmp .finish
    
.handle_destroy:
    invoke KillTimer, [hWnd], 1
    invoke PostQuitMessage, 0
    xor eax, eax
    jmp .finish
     
.finish:
    ret
endp
    
proc WinMain hInst, hPrevInst, CmdLine, CmdShow
    local msg:MSG
    
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, szName, \
        WS_OVERLAPPEDWINDOW or WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL
    mov [hWin], eax
    
    invoke SetWindowLong, [hWin], GWL_WNDPROC, WndProc
    mov [pDefWndProc], eax
 
    invoke SetTimer, [hWin], 1, 1000, NULL
@@:
    lea eax, [msg]
    invoke GetMessage, eax, NULL, 0, 0
    cmp eax, 0
    je @f
    lea eax, [msg]
    invoke DispatchMessage, eax
    jmp @b
@@:
    mov eax, [msg.wParam]
    ret
endp
    
start:
    invoke GetModuleHandle, NULL
    mov [hInstance], eax
     
    invoke GetCommandLine
    mov [pCommand], eax
     
    stdcall WinMain, [hInstance], NULL, [pCommand], SW_SHOWNORMAL
    invoke ExitProcess, eax

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

完成