程式語言 - Netwide Assembler (NASM) - Win32 API - Single Document Interface (SDI) - Handle Event



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

當視窗有事件需要被處理時,系統會透過呼叫註冊的Callback副程式來處理,而這個Callback副程式就是在註冊Class時,設定給lpfnWndProc的事件處理副程式,因此,當這個事件處理副程式被呼叫時,可以處理自己感興趣的事件,而其餘事件則交給系統處理(透過呼叫DefWindowProc()),需要注意的是,例如:WM_CLOSE,就必須由該視窗自己處理,因為系統無法知道還有哪些額外的資源需要被釋放掉

main.asm

    %include "/opt/wadk/nasm/inc/windows.inc"
 
    section .data
hWin        DD 0
pCmdLine    DD 0
hInstance   DD 0
szAppName   DB "main",0

msg istruc MSG
    at .hwnd,    DD 0
    at .message, DD 0
    at .wParam,  DD 0
    at .lParam,  DD 0
    at .time,    DD 0
    at .pt,      DD 0, 0
iend

wndClass istruc WNDCLASS
    at .style,         DD 0
    at .lpfnWndProc,   DD 0
    at .cbClsExtra,    DD 0
    at .cbWndExtra,    DD 0
    at .hInstance,     DD 0
    at .hIcon,         DD 0
    at .hCursor,       DD 0
    at .hbrBackground, DD 0
    at .lpszMenuName,  DD 0
    at .lpszClassName, DD 0
iend

    section .text
WndProc:
    enter 0, 0
              
    cmp DWORD [ebp + ARG2], WM_CLOSE
    je .close
    cmp DWORD [ebp + ARG2], WM_DESTROY
    je .destroy
    jmp .default
         
.close:
    push DWORD [ebp + ARG1]
    call DestroyWindow
    xor eax, eax
    jmp .finish
              
.destroy:
    push 0
    call PostQuitMessage
    xor eax, eax
    jmp .finish
              
.default:
    push DWORD [ebp + ARG4]
    push DWORD [ebp + ARG3]
    push DWORD [ebp + ARG2]
    push DWORD [ebp + ARG1]
    call DefWindowProc
              
.finish:
    leave
    ret 16
              
WinMain:
    enter 0, 0
  
    mov DWORD [wndClass + WNDCLASS.lpfnWndProc], WndProc
    mov DWORD [wndClass + WNDCLASS.lpszClassName], szAppName
    push wndClass
    call RegisterClass
               
    push NULL
    push NULL
    push NULL
    push NULL
    push 240
    push 320
    push 0
    push 0
    push WS_OVERLAPPEDWINDOW | WS_VISIBLE
    push szAppName
    push szAppName
    push WS_EX_LEFT
    call CreateWindowEx
    mov [hWin], eax
              
.loop:
    push 0
    push 0
    push 0
    push msg
    call GetMessage
    cmp eax, 0
    je .exit
               
    push msg
    call DispatchMessage
    jmp .loop
               
.exit:
    mov eax, [msg + MSG.wParam]
    leave
    ret 16
               
_start:
    push 0
    call GetModuleHandle
    mov [hInstance], eax
               
    call GetCommandLine
    mov [pCmdLine], eax
               
    push SW_SHOWNORMAL
    push DWORD [pCmdLine]
    push 0
    push DWORD [hInstance]
    call WinMain
               
    push 0
    call ExitProcess

完成