Win07.ASM : ARIAL Font/New Background Color
;Win07.ASM : ARIAL Font: SetBkgColor (GetSysColor / Manual COLORREF ) (May 10, 2024)
;=========================================================================
;NOTES:
; 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 win07.asm -o win07.obj
; Linking:golink.exe /entry:start win07.obj kernel32.dll user32.dll gdi32.dll
;===========================================================================
; This sample replaces the System font with Arial using TextOut.
; It also modifies the background color via SetBkColor
;===========================================================================
%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'
[section .data]
;--------------------------
;MESSAGES
;----------------------
;
MsgSysFont db "This is the system font. Black text on white background. Press a key to see Arial",0
lMsgSysFont equ $-MsgSysFont -1
MsgArial db "This is Arial, 15 point, COLOR_BTNFACE background (same as client area) ",0
lMsgArial equ $-MsgArial -1
MsgArialBold db "This is Arial, 15 point,BOLD, with fixed value COLORREF (Red/Green/Blue) =0040a080h .",0
lMsgArialBold equ $-MsgArialBold -1
;---------------
;HANDLES
;----------------
hDC dd 0 ; Handle to the Device Context
hInstance dd 0
hWnd dd 0 ;
hFont dd 0
hDefaultFont dd 0 ;Original font handle in device context.
;-----------------------
;VARIOUS LABELS
;------------------------
szTitle db 'win07 : NASM Window ARIAL Font Display Using TextOut',0x0
szClass db 'win07 Class',0x0
ArialName db "ARIAL",0x0
;------------------
;STRUCS
;------------------
wcx times WNDCLASSEX_size db 0
msg times MSG_size db 0
ps times PAINTSTRUCT_size db 0
;===============================================
[section .text]
start: ;This label is for golink.exe.
proc win07
invoke GetModuleHandleA, dword NULL
mov [hInstance], eax
invoke WinMain,dword [hInstance],dword NULL,dword NULL,dword SW_SHOWNORMAL
invoke ExitProcess,dword NULL
ret
endproc ;win07
;---------------------------------------
proc WinMain ,hInst,hPinst,CmdLn,dwShow
;Load WNDCLASSEX
;Memory block reserved for wcx [WNDCLASSEX] has no data yet.
;A different method of populating memory slots.
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]
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
;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
;================= WndProc ====================
proc WndProc, hWin, uMsg, wParam, lParam
Is_Key?:
cmp dword argv(uMsg),dword WM_KEYDOWN
jnz IsDestroy?
jmp IsKey
IsDestroy?:
cmp dword argv(uMsg),dword WM_DESTROY ;Window SYSMENU "x" click (or
jnz IsPaint?
jmp IsDestroy
IsPaint?:
cmp dword argv(uMsg),dword WM_PAINT
jnz DefWndProc
;BeginPaint
invoke BeginPaint, dword argv(hWin), dword ps
mov dword [hDC], eax
invoke TextOutA,dword [hDC],0,10,MsgSysFont,lMsgSysFont
;Release the Device Context handle:
invoke EndPaint, dword argv(hWin), dword ps
mov dword [hDC],0
xor eax,eax
ret
IsKey:
cmp argv(wParam), dword VK_ESCAPE ; ESC Key ?
jz IsDestroy
;Paint the next two lines in Arial with each a different color.
;First of two.
;Get a handle to a DC
invoke GetDC,dword argv(hWin)
mov dword [hDC],eax
;Set the background color to the client background from WNDCLASSEX
invoke GetSysColor,COLOR_BTNFACE ; +1 not needed here-see Win07.rtf.
invoke SetBkColor,dword [hDC],eax
;Set the text font
invoke CreateFontA,15,6,0,0,500,0,0,0,0,0,0,0,0,ArialName
mov dword [hFont],eax
;Select the font into the DC
invoke SelectObject, dword [hDC],dword [hFont]
mov dword [hDefaultFont], eax
;Display the Arial/Client background message.
invoke TextOutA,dword [hDC],0,30,MsgArial,lMsgArial
;Second of two.
;Change the font to BOLD
invoke CreateFontA,15,6,0,0,900,0,0,0,0,0,0,0,0,ArialName
mov dword [hFont],eax
;Change the font associated with the DC.
;Same thing except different.
invoke SelectObject, dword [hDC],dword [hFont]
;
;This time hard code a COLORREF value of 0088ff00h.
;COLORREF : bits 0-7 =red value
; bits 8-15 =green value
; bits 16-23 =blue value
; bits 24=31 =0 (not used)
xor eax,eax ;Clear the register
mov ah,040h ; Move 40h into bits 8-15 (00 00 40 00)
shl eax,8 ; Shift 40 into bits 16-23 (00 40 00 00)
mov ah,0a0h ; Bits 8-15 =0ah
mov al,080h ; Bits 0-7 =80h
; Final COLORREF =0040a080h
;Set background color to COLORREF.
invoke SetBkColor,dword [hDC],eax
;Display Arial/COLORREF =0040a080h
invoke TextOutA,dword [hDC],0,50,MsgArialBold,lMsgArialBold
;Restore the default font
invoke SelectObject, dword [hDC],dword [hDefaultFont]
invoke DeleteObject, dword [hFont]
;Release the Device Context handle:
invoke ReleaseDC, dword argv(hWin), dword [hDC]
mov dword [hDC],0
jmp DefWndProc
IsDestroy:
invoke PostQuitMessage,dword NULL
xor eax,eax
ret
DefWndProc:
invoke DefWindowProcA, dword argv(hWin), dword argv(uMsg), dword argv(wParam), dword argv(lParam)
ret
endproc ;WndProc
;mcamember May 10, 2024