程式語言 - High Level Assembly (HLA) - Win32 API (HLA v1.x) - Painting - Set Pixel



參考網站:
http://winapi.freetechsecrets.com/win32/
http://masm32.com/board/index.php?topic=3584.0
https://www.plantation-productions.com/Webster/
https://www.plantation-productions.com/Webster/Win32Asm/win32API.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.hla

program main;
      
#include("w.hhf")
#include("args.hhf")
#include("memory.hhf")
#include("strings.hhf")
      
static
    hWin:        dword;
    hInstance:   dword;
    CommandLine: string;
    defWndProc:  dword;
    
readonly
    szCaption: string:= "main";
     
procedure WndProc(hWnd:dword; uMsg:uns32; wParam:dword; lParam:dword); @stdcall;
var
    x: dword;
    y: dword;
    hdc: dword;
    ps: w.PAINTSTRUCT;

begin WndProc;
    if (uMsg == w.WM_PAINT) then
        w.BeginPaint(hWnd, ps);
        mov(eax, hdc);

        for(mov(0, y); y < 100; inc(y)) do
            for(mov(0, x); x < 100; inc(x)) do
                w.SetPixel(hdc, x, y, $ff);
            endfor;
        endfor;

        w.EndPaint(hWnd, ps);
        xor(eax, eax);
    elseif (uMsg == w.WM_CLOSE) then
        w.DestroyWindow(hWnd);
        xor(eax, eax);
    elseif (uMsg == w.WM_DESTROY) then
        w.PostQuitMessage(0);
        xor(eax, eax);
    else
        w.CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam);
    endif;
end WndProc;
     
procedure WinMain(hInst:dword; hPrevInst:dword; CmdLine:string; CmdShow:dword);
var
    msg: w.MSG;
      
begin WinMain;
    w.CreateWindowEx(w.WS_EX_LEFT, w.WC_DIALOG, szCaption,
        w.WS_OVERLAPPEDWINDOW | w.WS_VISIBLE, 0, 0, 300, 300, 0, 0, NULL, NULL);
    mov(eax, hWin);
     
    w.SetWindowLong(hWin, w.GWL_WNDPROC, &WndProc);
    mov(eax, defWndProc);

    forever
        w.GetMessage(msg, NULL, 0, 0);
        breakif(!eax);
      
        w.DispatchMessage(msg);
    endfor;
    mov(msg.wParam, eax);
end WinMain;
      
begin main;
    w.GetModuleHandle(NULL);
    mov(eax, hInstance);
    mov(arg.cmdLn(), CommandLine);
     
    WinMain(hInstance, NULL, CommandLine, w.SW_SHOWNORMAL);
     
    w.ExitProcess(eax);
end main;

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

完成