程式語言 - Flat Assembler (FASM) - Win32 API - Create Font



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

在Windows視窗程式設計中,當需要文字輸出顯示在視窗上時,一般都會使用自定義的字型,因為系統預設的字型太小,而自定義的字型,除了可以是粗體或者斜體,還可以設定自定義的長寬尺寸,司徒使用一個簡單例子來說明如何建立自定義的字型

main.asm

    include "head.asm"
 
    section ".text" code readable executable
proc WndProc hWnd, uMsg, wParam, lParam
    local hdc:DWORD
    local font:DWORD
    local ps:PAINTSTRUCT
          
    mov eax, [uMsg]
    cmp eax, WM_PAINT
    je .handle_paint
    cmp eax, WM_CLOSE
    je .handle_close
    cmp eax, WM_DESTROY
    je .handle_destroy
    invoke CallWindowProc, [pDefWndProc], [hWnd], [uMsg], [wParam], [lParam]
    jmp .finish
           
.handle_paint:
    lea eax, [ps]
    invoke BeginPaint, [hWnd], eax
    mov [hdc], eax
     
    invoke CreateFont, 48, 0, 0, 0, FW_BOLD, 0, 0, 0, 0, \
        OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, \
        CLEARTYPE_QUALITY, DEFAULT_PITCH or FF_DONTCARE, szFont
    mov [font], eax
 
    invoke SetTextColor, [hdc], 0ffh
    invoke SetBkMode, [hdc], TRANSPARENT
    invoke SelectObject, [hdc], [font]
    invoke TextOut, [hdc], 100, 100, szMsg, MsgLen
 
    lea eax, [ps]
    invoke EndPaint, [hWnd], eax
    invoke DeleteObject, [font]
    xor eax, eax
    jmp .finish
           
.handle_close:
    invoke DestroyWindow, [hWnd]
    xor eax, eax
    jmp .finish
               
.handle_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, 300, 300, 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

Line 24~26:創造一個大小48、粗體的Arial字型

完成