FASM >> Assembly (x86) >> Win32 API >> Painting

Draw Text


參考資訊:
1. fasm

使用TextOut()顯示文字時,只有X、Y參數可以用來設定顯示的位置,當文字長度超過顯示區域時,就需要拆解文字,包含置中顯示也是需要花費額外的計算,如果遇到這些問題,建議使用DrawText()顯示文字,DrawText()提供更多選項使用,包含多行顯示、置中顯示,使用者只需要傳入顯示範圍即可

main.asm

    format PE GUI 4.0
    entry start
                
    include "c:\fasm\include\win32a.inc"
               
    section ".idata" import data readable writeable
library user, "user32.dll", kernel, "kernel32.dll", gdi, "gdi32.dll"
          
import gdi,                              \
    Pie,              "Pie",             \
    Arc,              "Arc",             \
    Chord,            "Chord",           \
    LineTo,           "LineTo",          \
    Polygon,          "Polygon",         \
    Ellipse,          "Ellipse",         \
    TextOut,          "TextOutA",        \
    SetPixel,         "SetPixel",        \
    MoveToEx,         "MoveToEx",        \
    SetBkMode,        "SetBkMode",       \
    Rectangle,        "Rectangle",       \
    CreatePen,        "CreatePen",       \
    CreateFont,       "CreateFontA",     \
    DeleteObject,     "DeleteObject",    \
    SelectObject,     "SelectObject",    \
    SetTextColor,     "SetTextColor",    \
    CreateSolidBrush, "CreateSolidBrush"
          
import user,                             \
    FillRect,        "FillRect",         \
    EndPaint,        "EndPaint",         \
    SetTimer,        "SetTimer",         \
    DrawText,        "DrawTextA",        \
    wsprintf,        "wsprintfA",        \
    KillTimer,       "KillTimer",        \
    BeginPaint,      "BeginPaint",       \
    MessageBox,      "MessageBoxA",      \
    GetMessage,      "GetMessageA",      \
    SetScrollPos,    "SetScrollPos",     \
    DestroyWindow,   "DestroyWindow",    \
    SetWindowText,   "SetWindowTextA",   \
    SetWindowLong,   "SetWindowLongA",   \
    SetScrollRange,  "SetScrollRange",   \
    CreateWindowEx,  "CreateWindowExA",  \
    CallWindowProc,  "CallWindowProcA",  \
    DispatchMessage, "DispatchMessageA", \
    PostQuitMessage, "PostQuitMessage"
               
import kernel,                           \
    ExitProcess,     "ExitProcess",      \
    GetCommandLine,  "GetCommandLineA",  \
    GetModuleHandle, "GetModuleHandleA"
 
    CLEARTYPE_QUALITY   equ 5
    DT_VCENTER          equ 4h
    DT_CENTER           equ 1h
    DT_SINGLELINE       equ 20h
                
    section ".data" data readable writeable
szCaption   db "main",0
szFont      db "Arial",0
szMsg       db "Test",0
dwMsg       =  ($ - szMsg) - 1
hWin        dd 0
hInstance   dd 0
CommandLine dd 0
defWndProc  dd 0
           
    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, [defWndProc], [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], 0ff0000h
    invoke SetBkMode, [hdc], TRANSPARENT
    invoke SelectObject, [hdc], [font]

    lea eax, [ps.rcPaint]
    invoke DrawText, [hdc], szMsg, dwMsg, eax, DT_SINGLELINE or DT_CENTER or DT_VCENTER
 
    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, 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
            
@@:
    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 [CommandLine], eax
                
    stdcall WinMain, [hInstance], NULL, [CommandLine], SW_SHOWNORMAL
    invoke ExitProcess, eax

Line 98~99:使用DrawText()顯示文字

Makefile

export WINEPREFIX=/home/user/.wine_amd64

TARGET=main
MYWINE=box86 wine
FASM32=/home/user/.wine_amd64/drive_c/fasm

all:
	${MYWINE} ${FASM32}/fasm.exe ${TARGET}.asm

run:
	${MYWINE} ${TARGET}.exe

clean:
	rm -rf ${TARGET}.exe

編譯、執行

$ make
$ make run


返回上一頁