程式語言 - Netwide Assembler (NASM) - Win32 API - Draw Line



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

線的起始點位置是位於(x=0, y=0),使用者可以呼叫MoveToEx()設定新的起始點,而使用LineTo()就可以畫出一條直線,新的起始點則是線的結束位置

main.asm

    %include "/opt/wadk/nasm/inc/windows.inc"
 
    section .data
hWin        DD 0
pCmdLine    DD 0
hInstance   DD 0
pDefWndProc DD 0
szAppName   DB "main",0
hDC         DD 0
hPen        DD 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

ps istruc PAINTSTRUCT
    at .hdc,         DD 0
    at .fErase,      DD 0
    at .rcPaint,     DD 0, 0, 0, 0
    at .fRestore,    DD 0
    at .fIncUpdate,  DD 0
    at .rgbReserved, DB 0
iend

%macro CreateMyPen 2
    push %2
    push %1
    push PS_SOLID
    call CreatePen
%endmacro
     
%macro DrawMyLine 6
    push %2
    push %1
    call SelectObject
          
    push 0
    push %4
    push %3
    push %1
    call MoveToEx
          
    push %6
    push %5
    push %1
    call LineTo
%endmacro

    section .text
WndProc:
    enter 0, 0

    cmp DWORD [ebp + ARG2], WM_PAINT
    je .paint
    cmp DWORD [ebp + ARG2], WM_CLOSE
    je .close
    cmp DWORD [ebp + ARG2], WM_DESTROY
    je .destroy
    jmp .default
      
.paint:
    push ps
    push DWORD [ebp + ARG1]
    call BeginPaint
    mov [hDC], eax
    
    CreateMyPen 3, 0ffh
    mov [hPen + 0], eax
    CreateMyPen 3, 0ff00h
    mov [hPen + 4], eax
    CreateMyPen 3, 0ff0000h
    mov [hPen + 8], eax
  
    DrawMyLine DWORD [hDC], DWORD [hPen + 0], 10, 50, 250, 50
    DrawMyLine DWORD [hDC], DWORD [hPen + 4], 10, 100, 250, 100
    DrawMyLine DWORD [hDC], DWORD [hPen + 8], 10, 150, 250, 150
    
    push ps
    push DWORD [ebp + ARG1]
    call EndPaint
    
    push DWORD [hPen + 0]
    call DeleteObject
    push DWORD [hPen + 4]
    call DeleteObject
    push DWORD [hPen + 8]
    call DeleteObject
    xor eax, eax
    jmp .finish
      
.close:
    push DWORD [ebp + ARG1]
    call DestroyWindow
    xor eax, eax
    jmp .finish
           
.destroy:
    push 0
    call PostQuitMessage
    xor eax, eax
    jmp .finish
           
.default:
    push DWORD [ebp + ARG4]
    push DWORD [ebp + ARG3]
    push DWORD [ebp + ARG2]
    push DWORD [ebp + ARG1]
    push DWORD [pDefWndProc]
    call CallWindowProc
           
.finish:
    leave
    ret 16
 
WinMain:
    enter 0, 0
       
    push NULL
    push NULL
    push NULL
    push NULL
    push 240
    push 320
    push 0
    push 0
    push WS_OVERLAPPEDWINDOW | WS_VISIBLE
    push szAppName
    push WC_DIALOG
    push WS_EX_LEFT
    call CreateWindowEx
    mov [hWin], eax
      
    push WndProc
    push GWL_WNDPROC
    push DWORD [hWin]
    call SetWindowLong
    mov [pDefWndProc], 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

完成