The MASM Forum

General => The Campus => Topic started by: gelatine1 on June 27, 2014, 12:34:44 AM

Title: standard character size ?
Post by: gelatine1 on June 27, 2014, 12:34:44 AM
What is the standard height and width of any character when using the ExtTextOut function ? Like this:

invoke ExtTextOut, hdc,0,0,ETO_OPAQUE or ETO_NUMERICSLATIN,addr rect,[pmem],[bytesToWrite],NULL

Thanks in advance,
Jannes
Title: Re: standard character size ?
Post by: dedndave on June 27, 2014, 03:11:09 AM
i should have mentioned a few other functions when i mentioned ExtTextOut

one of the nice things about using ExtTextOut, is you get to control many aspects of the displayed text

before you call that function, you may select a font with SelectObject
windows fonts are a subject, all their own
but - it's probably best to use one of the user-selected fonts
you may find those with SystemParametersInfo and the SPI_GETNONCLIENTMETRICS flag
        .DATA?

ncm NONCLIENTMETRICS <>
; cbSize           dd ?
; iBorderWidth     dd ?
; iScrollWidth     dd ?
; iScrollHeight    dd ?
; iCaptionWidth    dd ?
; iCaptionHeight   dd ?
; lfCaptionFont    LOGFONT <>
; iSMCaptionWidth  dd ?
; iSMCaptionHeight dd ?
; lfSMCaptionFont  LOGFONT <>
; iMenuWidth       dd ?
; iMenuHeight      dd ?
; lfMenuFont       LOGFONT <>
; lfStatusFont     LOGFONT <>
; lfMessageFont    LOGFONT <>
;   lfHeight         dd ?              ;expansion of a LOGFONT structure...
;   lfWidth          dd ?
;   lfEscapement     dd ?
;   lfOrientation    dd ?
;   lfWeight         dd ?
;   lfItalic         db ?
;   lfUnderline      db ?
;   lfStrikeOut      db ?
;   lfCharSet        db ?
;   lfOutPrecision   db ?
;   lfClipPrecision  db ?
;   lfQuality        db ?
;   lfPitchAndFamily db ?
;   lfFaceName       db LF_FACESIZE dup(?) ;LF_FACESIZE = 32

        .CODE

main    PROC

        mov     ecx,sizeof NONCLIENTMETRICS
        mov     ncm.cbSize,ecx
        INVOKE  SystemParametersInfo,SPI_GETNONCLIENTMETRICS,ecx,offset ncm,NULL


at that point the NONCLIENTMETRICS structure will be filled in
there are 5 LOGFONT structures, but 3 are of primary interest
lfMenuFont, lfStatusFont, lfMessageFont - these are user-selected fonts (or part of current theme)
you may then use one of those to create a font handle
        INVOKE  CreateFontIndirect,offset ncm.lfMessageFont
        mov     hFontMsg,eax

if successful (and it probably will be), EAX will contain a font handle
i selected the font used for text inside message box's
font handles are GDI objects - when you are done using them you need to delete them
        INVOKE  DeleteObject,hFontMsg
you might put the code to create the font in the WM_CREATE handler
and the code to delete it might go in the WM_DESTROY handler

then, in WM_PAINT, before ExtTextOut
        INVOKE  SelectObject,hdc,hFontMsg
        mov     hFontHdc,eax

then draw your text
then restore the original hFont to the hDc
        INVOKE  SelectObject,hdc,hFontHdc

you may also use SetTextColor to select the color of the text
and SetBkColor to set the background color
these are both used in ways similar to selecting a font

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724947%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724947%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/windows/desktop/ff729175%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/ff729175%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/dd145037%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/dd145037%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/dd183500%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/dd183500%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/dd162957%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/dd162957%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/dd183539%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/dd183539%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/dd145093%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/dd145093%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/dd162964%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/dd162964%28v=vs.85%29.aspx)

holy crap !  he says   :icon_eek:
well - there are easier ways to get a font handle
use one of the "stock" fonts - you don't have to delete them, either   :biggrin:
http://msdn.microsoft.com/en-us/library/dd144925%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/dd144925%28v=vs.85%29.aspx)

you could also create a menu - and let the user select a font using ChooseFont
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646914%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646914%28v=vs.85%29.aspx)

ok - now that you know a little about fonts and font handles......
there are 2 main types of fonts, fixed-pitch or variable-pitch (true type)
if you use a fixed-pitch font, you can get the dimensions from the LOGFONT structure
if you use a variable-pitch font.......
you may select a font into an HDC, then use GetTextExtentPoint32 to get some idea how large it is
http://msdn.microsoft.com/en-us/library/dd144938%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/dd144938%28v=vs.85%29.aspx)
if you look on the left, there are a few related functions - but that's the one i generally use
Title: Re: standard character size ?
Post by: jj2007 on June 27, 2014, 08:58:51 PM
Jannes,

Don't believe it was Dave's intention to make you jump out of the window. He is a good guy, really. It's just Windows. Evil, complicated, confused and bloated. And yet, some of us have survived 8)
Title: Re: standard character size ?
Post by: dedndave on June 27, 2014, 10:45:34 PM
i guess, for writing a text editor, ChooseFont would be the way to go
it's actually pretty easy to implement

Bill Cravener has an example in \Masm32\Examples\Bill_Cravener\CommonDlgs

all you need is to add a menu.....
notice that the actual menu is defined in the RC (resource) file
it can be done with code, but there is little advantage to it
you define it in the RC file, then use LoadMenu or something similar
Title: Re: standard character size ?
Post by: Tedd on June 27, 2014, 11:36:29 PM
The size of the drawn characters depends on the font currently selected into the device context; width is a bigger problem as each character can have a different width, though the usual hack is to use the width of the character 'x' as an average for any calculations (it's generally accurate enough for sensible text).
As the default system font is ugly, I would use "SelectObject(hDC,GetStockObject(DEFAULT_GUI_FONT))" for a nicer default. Ideally, you should allow the user to select one through ChooseFont, but that can be left for later.
Then, to get font sizing information, use GetTextMetrics, which fills a TEXTMETRIC structure with the details for the currently selected font -- tmHeight is the minimum height required for a line to avoid any character overlaps; it's common (but not essential) to add an extra gap of 1--2 pixels between lines.
Title: Re: standard character size ?
Post by: gelatine1 on June 28, 2014, 11:40:48 PM
Thank you all very much for the explanation and help about the fonts! And thanks for the example of the menu and the rc file (although I don't understand much of it (yet)) :)