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

Boxes Functions
plus
plus

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

Resize Box to Text Size

 Keywords:  Box Boxes BoxesUp BoxDrawRect BoxDrawText Resize Size Text Width Height Font Screen Resolution 1000 Virtual Coordinate GetTextExtentPoint32A X Y Upper Lower WinMetrics

Question:

I would like to ask if there is an elegant solution to adjusting the size of a box to accommodate a given string of text, when displayed on different screen resolutions and versions of windows. I have to support screen resolutions from 800x600 single screen to 1920 x 1280 per screen in a dual screen configuration (ie Winmetrics returns 3840 x 1280). I have also found that the default XP and Win 7 installs give slightly different box sizes in relation to the standard text font, running on the same hardware (I am testing on VMs and changing the VM screen res). I can build a table of required box sizes for each resolution and O/S but it would be nice to have a simpler way of doing this that effectively autosizes the box to accommodate the text strings displayed. What I'm using this for is to display a two line installation progress message as a system modal banner, so it needs to be as small as possible to avoid using too much screen area.

Answer:

The Box functions use the 1000x1000 virtual screen coordinates. The benefit of the 1000x1000 screen is that it helps users easily calculate relative screen positions regardless of screen resolution. The drawback is possible loss of precision, caused by screens with with resolutions greater than 1000 pixels to be scaled down to 1000.

Reference: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+Tutorials+Screen~Coordinates~Explained.txt

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