程式語言 - Netwide Assembler (NASM) - Win32 API (NASMX) - Set Timer



參考資訊:
https://masm32.com/board/index.php
https://www.nasm.us/xdoc/2.13rc9/html/nasmdoc0.html

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

main.asm

    %include "c:\\nasmx\\inc\\nasmx.inc"
    %include "c:\\nasmx\\inc\\win32\\msvcrt.inc"
    %include "c:\\nasmx\\inc\\win32\\windows.inc"
    %include "c:\\nasmx\\inc\\win32\\user32.inc"
    %include "c:\\nasmx\\inc\\win32\\kernel32.inc"
  
    entry start
  
    section .data
hWin         dd 0
hInstance    dd 0
defWndProc   dd 0
CommandLine  dd 0
cnt          dd 0
buf          times 255 db 0
fmt          db "%d",0
szCaption    declare(NASMX_TCHAR) NASMX_TEXT("main"),0
   
NASMX_ISTRUC msg, MSG
    NASMX_AT hwnd,    NULL
    NASMX_AT message, NULL
    NASMX_AT wParam,  NULL
    NASMX_AT lParam,  NULL
    NASMX_AT time,    NULL
    NASMX_ISTRUC pt, POINT
        NASMX_AT x, 0
        NASMX_AT y, 0
    NASMX_IENDSTRUC
NASMX_IENDSTRUC
   
    section .text
proc WndProc, dword hWnd, uint_t uMsg, dword wParam, dword lParam
    locals none

    switch dword [argv(.uMsg)]
    case dword WM_TIMER
        inc dword [cnt]
        invoke sprintf, buf, fmt, [cnt]
        invoke SetWindowText, [argv(.hWnd)], buf
        break
    case dword WM_CLOSE
        invoke DestroyWindow, [argv(.hWnd)]
        break
    case dword WM_DESTROY
        invoke KillTimer, [argv(.hWnd)], 1
        invoke PostQuitMessage, 0
        break
    default
        invoke CallWindowProc, [defWndProc], [argv(.hWnd)], [argv(.uMsg)], [argv(.wParam)], [argv(.lParam)]
    endswitch
endproc
   
proc WinMain, dword hInst, dword hPrevInst, dword CmdLine, dword CmdShow
    locals none
    
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, szCaption, \
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL
    mov [hWin], eax
   
    invoke SetWindowLong, [hWin], GWL_WNDPROC, WndProc
    mov [defWndProc], eax

    invoke SetTimer, [hWin], 1, 1000, NULL
   
    do
        invoke DispatchMessage, msg
        invoke GetMessage, msg, NULL, NULL, NULL
    while eax, !=, 0
    
    mov eax, dword [msg + MSG.wParam]
endproc
    
proc start, dword argc, dword argv
    locals none
    
    invoke GetModuleHandle, NULL
    mov [hInstance], eax
    
    invoke GetCommandLine, NULL
    mov [CommandLine], eax
    
    invoke WinMain, [hInstance], NULL, [CommandLine], SW_SHOWNORMAL
    invoke ExitProcess, NULL
endproc

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

完成