The MASM Forum

General => The Campus => Topic started by: jimg on March 15, 2024, 02:28:38 AM

Title: GetTextExtentPoint32
Post by: jimg on March 15, 2024, 02:28:38 AM
I thought I knew how to use this stuff, but apparently not.

I'm trying to find out how many pixels it will take to print a line of text in a particular font.

This seems like exactly what GetTextExtentPoint32 was meant to do.

This is the code I'm using.  Full program attached.

I made a dialog and added two statics for testing.
I call the proc to make a new font, apply it to a static, and get the size of a line using that static.
The static is getting updated properly, I can see the text is the right size in the static on screen.
I use GetTextExtentPoint32 using each static for the source font.
I print out the size returned, and they are both the same.  88 long, and 16 high.
So it's not working.  What am I forgetting here?


.data
sizex POINT <?>
hfnt dd 0
lh   dd 0
sc1  dd 0
sc2  dd 0
buff db 128 dup (?)
Tahoma LOGFONT <-8,0,0,0,0,0,0,0,0,0,0,0,0,"Tahoma">
rrec RECT <?>
.code

DoTest proc id,newsize
   ; getnewfont
   mov eax,newsize
   mov Tahoma.lfHeight,eax
   inv CreateFontIndirect,addr Tahoma
   mov hfnt,eax

   inv GetDlgItem,hWin,id ; static control id,  101 or 102
   mov lh,eax ; save handle

   ; change font of status box
   inv SendDlgItemMessage,hWin,id,WM_SETFONT,hfnt,0

   inv GetClientRect,hWin,addr rrec ; make it show
   inv InvalidateRect,hWin,addr rrec,TRUE

   inv GetDC,lh
   mov sc1,eax
   inv SendMessage,lh,WM_GETFONT,0,0   ; test to make sure font updated properly
   .if eax != hfnt
      inv MessageBox,0,soff("bad setfont"),0,MB_TOPMOST
      jmp wrap
   .endif

   inv GetTextExtentPoint32,sc1,soff("This is a Test"),14,addr sizex  ; get the length of a line
   mov sc2,eax
   inv wsprintf,addr buff,soff("fonth=%i Size of line=%i,%i"),Tahoma.lfHeight,sizex.x,sizex.y
   mov eax,id
   add eax,2
   inv GetDlgItem,hWin,eax  ; there are two more static I'm writing the result into, id=103 and 104
   inv SendMessage,eax,WM_SETTEXT,0,addr buff 
wrap:
   ret
DoTest endp

invoke DoTest,101,-20
invoke DoTest,102,-45
   









Title: Re: GetTextExtentPoint32
Post by: jj2007 on March 15, 2024, 03:38:47 AM
It's a bit more complicated: instead of sending WM_SETFONT, use SelectObject, DC, hFont

See DoTest proc in attachment.

Title: Re: GetTextExtentPoint32
Post by: jimg on March 15, 2024, 04:54:29 AM
Thank a bunch! That works.  It's never as simple as is should be, is it.
Title: Re: GetTextExtentPoint32
Post by: NoCforMe on March 15, 2024, 07:39:39 AM
Meta note: I think this thread is a good candidate to be moved to the Windows API subforum.