程式語言 - Netwide Assembler (NASM) - Win32 API - 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 "head.asm"

    segment .text
WndProc:
    push ebp
    mov ebp, esp
                  
    cmp dword [ebp + ARG2], WM_PAINT
    je .handle_paint
    cmp dword [ebp + ARG2], WM_CLOSE
    je .handle_close
    cmp dword [ebp + ARG2], WM_DESTROY
    je .handle_destroy
    jmp .handle_default
             
.handle_paint:
    push ps
    push dword [ebp + ARG1]
    call BeginPaint
    mov [hDC], eax
        
    push szFont
    push DEFAULT_PITCH | FF_DONTCARE
    push CLEARTYPE_QUALITY
    push CLIP_DEFAULT_PRECIS
    push OUT_OUTLINE_PRECIS
    push 0
    push false
    push false
    push false
    push FW_BOLD
    push 0
    push 0
    push 0
    push 48
    call CreateFont
    mov [hFont], eax
  
    push 0ff0000h
    push dword [hDC]
    call SetTextColor
      
    push TRANSPARENT
    push dword [hDC]
    call SetBkMode
  
    push dword [hFont]
    push dword [hDC]
    call SelectObject
      
    push DT_SINGLELINE | DT_CENTER | DT_VCENTER
    push ps + PAINTSTRUCT.rcPaint
    push MsgLen
    push szMsg
    push dword [hDC]
    call DrawText
  
    push ps
    push dword [ebp + ARG1]
    call EndPaint
           
    push dword [hFont]
    call DeleteObject
    xor eax, eax
    jmp .finish
             
.handle_close:
    push dword [ebp + ARG1]
    call DestroyWindow
    xor eax, eax
    jmp .finish
                  
.handle_destroy:
    push 0
    call PostQuitMessage
    xor eax, eax
    jmp .finish
                  
.handle_default:
    push dword [ebp + ARG4]
    push dword [ebp + ARG3]
    push dword [ebp + ARG2]
    push dword [ebp + ARG1]
    push dword [pDefWndProc]
    call CallWindowProc
                  
.finish:
    leave
    ret 16
                  
WinMain:
    push ebp
    mov ebp, esp
                   
    push 0
    push 0
    push 0
    push 0
    push 300
    push 300
    push 0
    push 0
    push WS_OVERLAPPEDWINDOW | WS_VISIBLE
    push szAppName
    push WC_DIALOG
    push WS_EX_LEFT
    call CreateWindowEx
    mov [hWin], eax
                  
    push WndProc
    push GWL_WNDPROC
    push dword [hWin]
    call SetWindowLong
    mov [pDefWndProc], eax
               
.loop:
    push 0
    push 0
    push 0
    push msg
    call GetMessage
    cmp eax, 0
    je .exit
                   
    push msg
    call DispatchMessage
    jmp .loop
                   
.exit:
    mov eax, [msg + MSG.wParam]
    leave
    ret 16
                   
_start:
    push 0
    call GetModuleHandle
    mov [hInstance], eax
                   
    call GetCommandLine
    mov [pCommand], eax
                   
    push SW_SHOWNORMAL
    push dword [pCommand]
    push 0
    push dword [hInstance]
    call WinMain
                   
    push eax
    call ExitProcess

Line 51~56:使用DrawText()顯示文字

完成