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



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

Windows的視窗可以分成:Dialog、Single Document Interface(SDI)、Multiple Document Interface(MDI)三種類別,相對於SDI、MDI,Dialog類別是比較容易上手學習的一個類別,相當適合初次學習Windows視窗設計的使用者,而Windows視窗程式設計的核心就是圍繞着事件處理(處理Message),任何的視窗設定或者溝通都是以事件導向為原則,因此,寫出一個簡單的視窗框架,將有助於了解Windows視窗程式的運作

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
WinMain:
    enter 0, 0
  
    mov DWORD [wndClass + WNDCLASS.lpfnWndProc], DefWindowProc
    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

完成