Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: Box Boxes BoxesUp BoxDrawRect BoxDrawText Resize Size Text Width Height Font Screen Resolution 1000 Virtual Coordinate GetTextExtentPoint32A X Y Upper Lower WinMetrics
Here is my undebugged code attempt:
; Replace with code to generate text to display from real data #DefineFunction MakeString(NumCol) Str = "" For i = 1 To NumCol Str = Str:"0123456789 " Next Return StrTrim(Str) #EndFunction ; Calculate width of given text #DefineFunction GetTextWidth(txtstring) 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. TextWidth = BinaryPeek4(lpsize, 0 ) ;Specifies the rectangle's height. The units depend on which function uses this. TextHeight = BinaryPeek4(lpsize, 4 ) ;Release Device Context DllCall ( StrCat(DirWindows(1),'USER32.DLL'),long:'ReleaseDC',lpnull,long:hdc ) Return TextWidth #EndFunction ; Calculate height of given text #DefineFunction GetTextHeight(txtstring) 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 height. The units depend on which function uses this. TextHeight = BinaryPeek4(lpsize, 4 ) ;Release Device Context DllCall ( StrCat(DirWindows(1),'USER32.DLL'),long:'ReleaseDC',lpnull,long:hdc ) Return TextHeight #EndFunction ; Generate sample text colcount = 10 str = MakeString(colcount) ; Calculate width of text in pixels pixelwidth = GetTextWidth( str ) pixelheight = GetTextHeight( str ) ; Calculate/Convert to Box corrdinates ScreenX=WinMetrics(0) ScreenY=WinMetrics(1) ;Convert to 1000x100 virtual coordinates boxlowerx = pixelwidth *1000/ScreenX+10 ; Consider box frame boxlowery = pixelheight*1000/ScreenY+40 ; Consider titlebar height ;Display Box BoxesUp('0,0,': boxlowerx :',':boxlowery, @NORMAL) BoxDrawRect(1, '0,0,1000,1000', 0 ) BoxDrawText(1, '0,0,1000,1000', str, @TRUE, 32) TimeDelay(4)
Article ID: W17685
Filename: Resize Box to Text Size.txt
File Created: 2014:07:18:09:50:34
Last Updated: 2014:07:18:09:50:34