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



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

Polygon()可以用來繪製多邊形,需要傳入的資訊是一個座標點陣列,用來描述有哪些點需要被連接

main.asm

    format PE GUI 4.0
    entry start
               
    include "c:\fasm\include\win32a.inc"
    include "c:\fasm\include\api\kernel32.inc"
    include "c:\fasm\include\api\gdi32.inc"
    include "c:\fasm\include\api\user32.inc"

    section ".idata" import data readable writeable
library \
    gdi32, "gdi32.dll", \
    user32, "user32.dll", \
    kernel32, "kernel32.dll"
  
    section ".data" data readable writeable
hWin        DD 0
hInstance   DD 0
pCommand    DD 0
pDefWndProc DD 0
szName      DB "main",0

    section ".text" code readable executable
proc WndProc hWnd, uMsg, wParam, lParam
    local hdc:DWORD
    local pen:DWORD
    local brush:DWORD
    local pt[3]:POINT
    local ps:PAINTSTRUCT
        
    mov eax, [uMsg]
    cmp eax, WM_PAINT
    je .paint
    cmp eax, WM_CLOSE
    je .close
    cmp eax, WM_DESTROY
    je .destroy
    invoke CallWindowProc, [pDefWndProc], [hWnd], [uMsg], [wParam], [lParam]
    jmp .finish
         
.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]
  
    mov [sizeof.POINT * 0 + pt.x], 150
    mov [sizeof.POINT * 0 + pt.y], 10
    mov [sizeof.POINT * 1 + pt.x], 50
    mov [sizeof.POINT * 1 + pt.y], 200
    mov [sizeof.POINT * 2 + pt.x], 250
    mov [sizeof.POINT * 2 + pt.y], 200
    lea eax, [pt]
    invoke Polygon, [hdc], eax, 3
       
    lea eax, [ps]
    invoke EndPaint, [hWnd], eax
    invoke DeleteObject, [pen]
    invoke DeleteObject, [brush]
    xor eax, eax
    jmp .finish
         
.close:
    invoke DestroyWindow, [hWnd]
    xor eax, eax
    jmp .finish
             
.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, 320, 240, 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

完成