程式語言 - Flat Assembler (FASM) - Win32 API - Draw Rectangle



參考資訊:
https://board.flatassembler.net/

繪畫圖形需要透過SelectObject()去選擇Pen、Brush

main.asm

    include "head.asm"

    section ".text" code readable executable
proc WndProc hWnd, uMsg, wParam, lParam
    local hdc:DWORD
    local pen:DWORD
    local brush:DWORD
    local ps:PAINTSTRUCT
     
    mov eax, [uMsg]
    cmp eax, WM_PAINT
    je .handle_paint
    cmp eax, WM_CLOSE
    je .handle_close
    cmp eax, WM_DESTROY
    je .handle_destroy
    invoke CallWindowProc, [pDefWndProc], [hWnd], [uMsg], [wParam], [lParam]
    jmp .finish
      
.handle_paint:
    lea eax, [ps]
    invoke BeginPaint, [hWnd], eax
    mov [hdc], eax
     
    invoke CreatePen, PS_SOLID, 3, 0ffh
    mov [pen], eax
    invoke CreateSolidBrush, 0ff00h
    mov [brush], eax
  
    invoke SelectObject, [hdc], [pen]
    invoke SelectObject, [hdc], [brush]
    invoke Rectangle, [hdc], 10, 10, 200, 200
    
    lea eax, [ps]
    invoke EndPaint, [hWnd], eax
    invoke DeleteObject, [pen]
    invoke DeleteObject, [brush]
    xor eax, eax
    jmp .finish
      
.handle_close:
    invoke DestroyWindow, [hWnd]
    xor eax, eax
    jmp .finish
          
.handle_destroy:
    invoke PostQuitMessage, 0
    xor eax, eax
    jmp .finish
           
.finish:
    ret
endp
          
proc WinMain hInst, hPrevInst, CmdLine, CmdShow
    local msg:MSG
          
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, szName, \
        WS_OVERLAPPEDWINDOW or WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL
    mov [hWin], eax
          
    invoke SetWindowLong, [hWin], GWL_WNDPROC, WndProc
    mov [pDefWndProc], eax
       
@@:
    lea eax, [msg]
    invoke GetMessage, eax, NULL, 0, 0
    cmp eax, 0
    je @f
    lea eax, [msg]
    invoke DispatchMessage, eax
    jmp @b
@@:
    mov eax, [msg.wParam]
    ret
endp
          
start:
    invoke GetModuleHandle, NULL
    mov [hInstance], eax
           
    invoke GetCommandLine
    mov [pCommand], eax
           
    stdcall WinMain, [hInstance], NULL, [pCommand], SW_SHOWNORMAL
    invoke ExitProcess, eax

Line 32:畫一個長方形,起始點位置是(x=10, y=10),結束點位置是(x=200, y=200)

完成