WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Calculate Text width and height

keyword: Calculate width height text font string GetTextExtentPoint32A

;Calculate the width and height of text string
txtstring = 'Hello'
txtlen = StrCharCount( txtstring )
;Get Device Context
HDC=DllCall(StrCat(DirWindows(1),'USER32.DLL'),long:'GetDC',lpnull)

;Computes the width and height of the specified string of text.
;BOOL GetTextExtentPoint32(
;  HDC hdc,           // handle to DC
;  LPCTSTR lpString,  // text string
;  int c,      // characters in string
;  LPSIZE lpSize      // string size );
lpsize = BinaryAlloc( 8 )
result = DllCall ( StrCat(DirWindows(1),'GDI32.DLL'),long:'GetTextExtentPoint32A',long:HDC,lpstr:txtstring,long:txtlen, lpbinary:lpsize)
;Specifies the rectangle's width. The units depend on which function uses this.
cx = BinaryPeek4(lpsize, 0 )
;Specifies the rectangle's height. The units depend on which function uses this.
cy = BinaryPeek4(lpsize, 4 )

;Release Device Context
DllCall ( StrCat(DirWindows(1),'USER32.DLL'),long:'ReleaseDC',lpnull,long:hdc )

note = 'The rectangle dimensions stored in this structure may correspond to viewport extents, window extents, text extents, bitmap dimensions, or the aspect-ratio filter for some extended functions.'
Pause('GetTextExtentPoint32A', note : @CRLF :'Width: ': cx : @CRLF : 'Height: ': cy)

Article ID:   W17908
Filename:   Calculate Text width and height.txt
File Created: 2008:11:25:12:12:30
Last Updated: 2008:11:25:12:12:30