程式語言 - Netwide Assembler (NASM) - Win32 API (NASMX) - Dialog - Handle Event



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

一般在建立Windows視窗時,會註冊自定義的Class並且設定事件處理的副程式(callback),不過,目前範例使用的Class是系統預設的WC_DIALOG,因此,可以透過SetWindowLong()設定callback,這種替換callback的行為就稱為Subclass,意指為原本Class的Subclass,所以Subclass後,需要將原先callback的位址(SetWindowLong()的回傳值)保存下來,然後透過呼叫CallWindowProc()(傳入原先callback的位址),用來將處理控制權移交回原先視窗元件,所以當有多層Subclass的情形發生時,每次在處理事件時,記得判斷對應的Handle,如:在處理關閉事件時(WM_CLOSE),會先判斷目前的hWnd是否為當前的視窗hWnd,如果是當前視窗hWnd,才進行關閉的動作,避免關閉到其它視窗元件

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
hWin         dd 0
hInstance    dd 0
defWndProc   dd 0
CommandLine  dd 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_CLOSE
        invoke DestroyWindow, [argv(.hWnd)]
        break
    case dword WM_DESTROY
        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

    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 32~37:關閉視窗的順序為:主視窗收到WM_CLOSE事件時,呼叫DestroyWindow(),DestroyWindow()會自動將子視窗的相關資源也一併釋放掉(包含Resource描述的資源),系統接著會發送WM_DESTROY事件給主視窗,待主視窗收到WM_DESTROY事件時,呼叫PostQuitMessage()結束視窗,值得注意的地方是,SendMessage()是屬於Block方式呼叫(必須等待動作執行完畢才返回),而PostQuitMessage()則是Non-block方式呼叫
Line 39:將處理控制權移交給下一個視窗元件

完成