程式語言 - Netwide Assembler (NASM) - Win32 API (NASMX) - 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 "c:\\nasmx\\inc\\nasmx.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
szName       declare(NASMX_TCHAR) NASMX_TEXT("main"),0
CommandLine  dd 0
hInstance    dd 0
 
NASMX_ISTRUC msg, MSG
NASMX_IENDSTRUC

NASMX_ISTRUC wndClass, WNDCLASS
NASMX_IENDSTRUC
 
    section .text
proc WndProc, dword hWnd, uint_t uMsg, dword wParam, dword lParam
    locals none
 
    switch dword [argv(.uMsg)]
    case dword WM_CLOSE
        invoke DestroyWindow, [argv(.hWnd)]
        break
    case dword WM_DESTROY
        invoke PostQuitMessage, 0
        break
    default
        invoke DefWindowProc, [argv(.hWnd)], [argv(.uMsg)], [argv(.wParam)], [argv(.lParam)]
    endswitch
endproc

proc WinMain, dword hInst, dword hPrevInst, dword CmdLine, dword CmdShow
    locals none

    mov dword [wndClass + WNDCLASS.lpfnWndProc], WndProc
    mov dword [wndClass + WNDCLASS.lpszClassName], szName
    invoke RegisterClass, wndClass
    invoke CreateWindowEx, WS_EX_LEFT, szName, szName, \
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, 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 23~29:關閉視窗的順序為:主視窗收到WM_CLOSE事件時,呼叫DestroyWindow(),DestroyWindow()會自動將子視窗的相關資源也一併釋放掉(包含Resource描述的資源),系統接著會發送WM_DESTROY事件給主視窗,待主視窗收到WM_DESTROY事件時,呼叫PostQuitMessage()結束視窗,值得注意的地方是,SendMessage()是屬於Block方式呼叫(必須等待動作執行完畢才返回),而PostQuitMessage()則是Non-block方式呼叫
Line 30~31:其餘事件由系統代為處理

完成