Win06.ASM : DrawText 


;Win06.ASM : Display Using DrawText (February 25, 2025)
;===================================================================
;   The " %include " paths at the start of the file might need to be
;       changed to suit your own disk directory setup.
;   Command Lines:
;       Assembly: nasm -f win32 win06.asm -o win06.obj
;       GoLink.exe: golink.exe /entry:start win06.obj  kernel32.dll user32.dll gdi32.dll
;===========================================================================

%include '\nasm32\inc\win32\windows.inc'
%include '\nasm32\inc\win32\kernel32.inc'
%include '\nasm32\inc\win32\user32.inc'
%include '\nasm32\inc\nasm32.inc'
%include '\nasm32\inc\win32\gdi32.inc'	;needed for DrawText


[section .data]
 
;Define arrays to hold our instances of RECT, PAINTSTRUCT,WNDCLASSEX and MSG

RECTDraw times RECT_size db 0
ps times PAINTSTRUCT_size db 0
wcx times WNDCLASSEX_size db 0
msg times MSG_size db 0

;Messages

MsgAnyKey db "Press any key ",0
lMsgAnyKey equ $-MsgAnyKey -1

MsgDrawText db "This message brought to you by DrawText."
lMsgDrawText equ $-MsgDrawText -1

;Handles

hDC dd 0       ; Handle to the Device Context
hInstance dd 0
hWnd dd 0      

;Various labels

KeyHit dd 0
szTitle db 'win06 : NASM Window With Display Using DrawText',0x0
szClass db 'win06 Class',0x0

;========================== CODE  ===========================================

[section .text]

start:

proc win06
      invoke GetModuleHandleA, dword NULL
      mov [hInstance], eax
      invoke WinMain,dword [hInstance],dword NULL,dword NULL,dword SW_SHOWNORMAL
      invoke ExitProcess,dword NULL
      ret
endproc ;win06


proc WinMain ,hInst,hPinst,CmdLn,dwShow

;Block reserved for wcx [WNDCLASSEX] has no data yet. 

push dword WNDCLASSEX_size
pop dword [wcx + WNDCLASSEX.cbSize]

push dword argv(hInst)      
pop dword [wcx + WNDCLASSEX.hInstance]

push dword szClass
pop dword [wcx + WNDCLASSEX.lpszClassName]


push dword WndProc    ; Adr of WndProc
pop dword [wcx + WNDCLASSEX.lpfnWndProc]

push dword COLOR_BTNFACE +1
pop dword [wcx + WNDCLASSEX.hbrBackground]

push dword CS_VREDRAW +CS_HREDRAW
pop dword [wcx + WNDCLASSEX.style]


;Register the window class
      invoke RegisterClassExA, dword wcx

;Load window
    invoke CreateWindowExA,\
     WS_EX_OVERLAPPEDWINDOW,\
                    szClass,\
                    szTitle,\
          WS_OVERLAPPEDWINDOW +WS_VISIBLE ,\
            10, 120, 700, 232,\
                          NULL,\
                         NULL,\
     [wcx + WNDCLASSEX.hInstance],\
                        NULL

     mov [hWnd], eax

;Get the device context handle
	    invoke GetDC,dword [hWnd]
        mov dword [hDC],eax
	    invoke TextOutA,dword [hDC],0,10,MsgAnyKey,lMsgAnyKey

;Release the Device Context handle:
        invoke ReleaseDC, dword [hWnd], dword [hDC]

;Process Windows Messages

MsgGet:
    invoke GetMessageA, dword msg, dword NULL, dword NULL, dword NULL
    cmp eax,0
        jnz MsgProcess
        jmp MsgEnd
MsgProcess:
      invoke TranslateMessage, dword msg
      invoke DispatchMessageA, dword msg
      jmp MsgGet
MsgEnd:
   ret
endproc ;WinMain



proc WndProc, hWin, uMsg, wParam, lParam

IsDestroy?:
	    cmp dword argv(uMsg),dword WM_DESTROY ;Window SYSMENU "x" click (or
		    jnz  IsKey?
		jmp wpExit

IsKey?:
	    cmp dword argv(uMsg),dword WM_KEYDOWN	;WM_KEYDOWN for non-alpha keys.
    	    jz wpIsEsc?
		jmp DefWndProc

;Key pressed
wpIsEsc?:
	    cmp dword argv(wParam), VK_ESCAPE   ; ESC Key ?
    	    jz near wpEsc
	    jmp wpDrawText    ;Any key other than ESC will trigger display.

;Escape key hit
wpEsc:
		invoke SendMessageA, argv(hWin),WM_CLOSE,NULL,NULL
        xor eax,eax
		ret

wpDrawText:
	    invoke GetDC,dword argv(hWin)
    	mov dword [hDC],eax

;Set the rectangle coods. DrawText will use this RECT as the drawing location.
	    mov dword [RECTDraw +RECT.left],0
    	mov dword [RECTDraw +RECT.top],40
	    mov dword [RECTDraw +RECT.right],430
    	mov dword [RECTDraw +RECT.bottom],100

        invoke DrawTextA,dword [hDC],MsgDrawText,lMsgDrawText,RECTDraw,DT_LEFT

;Release the Device Context handle:
        invoke ReleaseDC, dword argv(hWin), dword [hDC]
		xor eax,eax
		ret

DefWndProc:
    invoke DefWindowProcA, dword argv(hWin), dword argv(uMsg), dword argv(wParam), dword argv(lParam)
    ret

;Exit procecure

wpExit:
    invoke PostQuitMessage,dword NULL
    xor eax,eax
ret

endproc     ;WndProc
;==============================================

;mcamember February 25, 2025