程式語言 - Netwide Assembler (NASM) - Win32 API (NASMX) - Painting - Draw Text



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

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

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
 
    false             equ 0
    CLEARTYPE_QUALITY equ 5
             
    section .data
szCaption    declare(NASMX_TCHAR) NASMX_TEXT("main"),0
szFont       declare(NASMX_TCHAR) NASMX_TEXT("Arial"),0
szMsg        declare(NASMX_TCHAR) NASMX_TEXT("Test"),0
dwMsg        equ ($ - szMsg) - 1
hWin         dd 0
hInstance    dd 0
defWndProc   dd 0
CommandLine  dd 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 hdc, DWORD
        local font, DWORD
    endlocals
           
    switch dword [argv(.uMsg)]
    case dword WM_PAINT
        invoke BeginPaint, [argv(.hWnd)], ps
        mov [var(.hdc)], eax
         
        invoke CreateFont, 48, 0, 0, 0, FW_BOLD, false, false, false, 0, \
            OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, \
            CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_DONTCARE, szFont
        mov [var(.font)], eax
 
        invoke SetTextColor, [var(.hdc)], 0xff0000
        invoke SetBkMode, [var(.hdc)], TRANSPARENT
        invoke SelectObject, [var(.hdc)], [var(.font)]
        invoke DrawText, [var(.hdc)], szMsg, dwMsg, ps + PAINTSTRUCT.rcPaint, DT_SINGLELINE | DT_CENTER | DT_VCENTER
        
        invoke EndPaint, [argv(.hWnd)], ps
        invoke DeleteObject, [var(.font)]
        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 49:使用DrawText()顯示文字

完成