程式語言 - MASM32 - Painting - Draw Line



參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32

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

main.asm

    .386
    .model flat,stdcall
    option casemap:none
          
include c:\masm32\include\msvcrt.inc
include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\gdi32.inc
include c:\masm32\include\user32.inc
          
includelib c:\masm32\lib\msvcrt.lib
includelib c:\masm32\lib\gdi32.lib
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib
          
    .data
szCaption   db "main",0
hWin        dd 0
hInstance   dd 0
CommandLine dd 0
defWndProc  dd 0
          
    .code
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    local hdc:dword
    local pen[3]:dword
    local ps:PAINTSTRUCT
   
    .if uMsg == WM_PAINT
        invoke BeginPaint, hWnd, addr ps
        mov hdc, eax
   
        invoke CreatePen, PS_SOLID, 3, 0ffh
        mov pen[0], eax
        invoke CreatePen, PS_SOLID, 3, 0ff00h
        mov pen[4], eax
        invoke CreatePen, PS_SOLID, 3, 0ff0000h
        mov pen[8], eax

        invoke SelectObject, hdc, pen[0]
        invoke MoveToEx, hdc, 10, 100, NULL
        invoke LineTo, hdc, 250, 100
  
        invoke SelectObject, hdc, pen[4]
        invoke MoveToEx, hdc, 10, 150, NULL
        invoke LineTo, hdc, 250, 150
  
        invoke SelectObject, hdc, pen[8]
        invoke MoveToEx, hdc, 10, 200, NULL
        invoke LineTo, hdc, 250, 200
  
        invoke EndPaint, hWnd, addr ps
        invoke DeleteObject, pen[0]
        invoke DeleteObject, pen[4]
        invoke DeleteObject, pen[8]
        xor eax, eax
        ret
    .elseif uMsg == WM_CLOSE
        invoke DestroyWindow, hWnd
        xor eax, eax
        ret
    .elseif uMsg == WM_DESTROY
        invoke PostQuitMessage, 0
        xor eax, eax
        ret
    .endif
        
    invoke CallWindowProc, defWndProc, hWnd, uMsg, wParam, lParam
    ret
WndProc endp
         
WinMain proc hInst:DWORD, hPrevInst:DWORD, CmdLine:DWORD, CmdShow:DWORD
    local msg:MSG
          
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, offset szCaption, 
        WS_OVERLAPPEDWINDOW or WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL
    mov hWin, eax
         
    invoke SetWindowLong, hWin, GWL_WNDPROC, WndProc
    mov defWndProc, eax
@@:
    invoke GetMessage, addr msg, NULL, 0, 0
    cmp eax, 0
    je @f
    invoke DispatchMessage, addr msg
    jmp @b
@@:
    mov eax, msg.wParam
    ret
WinMain endp
        
start:
    invoke GetModuleHandle, NULL
    mov hInstance, eax
          
    invoke GetCommandLine
    mov CommandLine, eax
          
    invoke WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
    invoke ExitProcess, eax
end start

Line 33~50:產生三支Pen並且畫出三條直線,需要注意的是,同一時間只能選擇一支Pen

完成