程式語言 - Netwide Assembler (NASM) - Win32 API (NASMX) - Painting - Set Pixel



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

屏幕的最小顯示單位是像素,像素由紅色(Red)、綠色(Green)、藍色(Blue)、Alpha等四個顏色組成,因此,在呼叫CreateWindow()時,傳入的解析度,如:300x300,代表該視窗(有效區域)的X軸有300個像素,而Y軸則是有300個像素,這個300x300像素的區域是可以用來繪製任何東西,WM_PAINT是處理視窗重新繪畫的事件,繪畫的相關處理都需要在這個事件完成,比較特別的是,Windows視窗將繪圖的許多東西抽象化,最基本的需求是:一個DC(Device Context)和一個BITMAP,DC可以想像成是一個畫台,而BITMAP則是一片畫布(Buffer),DC有了Buffer就可以畫上任何東西並將其顯示在視窗上

main.asm

    %include "c:\\nasmx\\inc\\nasmx.inc"
    %include "c:\\nasmx\\inc\\win32\\msvcrt.inc"
    %include "c:\\nasmx\\inc\\win32\\windows.inc"
    %include "c:\\nasmx\\inc\\win32\\gdi32.inc"
    %include "c:\\nasmx\\inc\\win32\\user32.inc"
    %include "c:\\nasmx\\inc\\win32\\kernel32.inc"
    
    entry start
    
    section .data
hWin         dd 0
hInstance    dd 0
defWndProc   dd 0
CommandLine  dd 0
szCaption    declare(NASMX_TCHAR) NASMX_TEXT("main"),0
     
NASMX_ISTRUC msg, MSG
NASMX_IENDSTRUC

NASMX_ISTRUC ps, PAINTSTRUCT
NASMX_IENDSTRUC
     
    section .text
proc WndProc, dword hWnd, uint_t uMsg, dword wParam, dword lParam
    locals
        local x, DWORD
        local y, DWORD
        local hdc, DWORD
    endlocals
  
    switch dword [argv(.uMsg)]
    case dword WM_PAINT
        invoke BeginPaint, [argv(.hWnd)], ps
        mov [var(.hdc)], eax

        xor eax, eax
        mov [var(.y)], eax
.ylp:
        xor eax, eax
        mov [var(.x)], eax
.xlp:
        invoke SetPixel, [var(.hdc)], [var(.x)], [var(.y)], 0ffh
        inc dword [var(.x)]
        cmp dword [var(.x)], 100
        jb .xlp
        inc dword [var(.y)]
        cmp dword [var(.y)], 100
        jb .ylp

        invoke EndPaint, [argv(.hWnd)], ps
        break
    case dword WM_CLOSE
        invoke DestroyWindow, [argv(.hWnd)]
        break
    case dword WM_DESTROY
        invoke PostQuitMessage, 0
        break
    default
        invoke CallWindowProc, [defWndProc], [argv(.hWnd)], [argv(.uMsg)], [argv(.wParam)], [argv(.lParam)]
    endswitch
endproc
     
proc WinMain, dword hInst, dword hPrevInst, dword CmdLine, dword CmdShow
    locals none
      
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, szCaption, \
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL
    mov [hWin], eax
     
    invoke SetWindowLong, [hWin], GWL_WNDPROC, WndProc
    mov [defWndProc], eax
 
    do
        invoke DispatchMessage, msg
        invoke GetMessage, msg, NULL, NULL, NULL
    while eax, !=, 0
      
    mov eax, dword [msg + MSG.wParam]
endproc
      
proc start, dword argc, dword argv
    locals none
      
    invoke GetModuleHandle, NULL
    mov [hInstance], eax
      
    invoke GetCommandLine, NULL
    mov [CommandLine], eax
      
    invoke WinMain, [hInstance], NULL, [CommandLine], SW_SHOWNORMAL
    invoke ExitProcess, NULL
endproc

Line 32~51:處理繪畫事件
Line 33~34:取得視窗的DC,該DC已經有Buffer可以使用,因此,可以直接在上面繪製任何東西
Line 36~48:透過SetPixel()畫出一個正方形,顏色是紅色
Line 50:結束繪製

完成