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

Samples

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

Calculate Width of Given Text

 Keywords:  Calculate Fixed Width Text Column Font GetWindowLongA SetWindowLongA GetDC SelectObject GetTextExtentPoint32A GetTextExtentPoint32 Dialog Unit

AddExtender("wwctl44i.dll")
MSG_INIT = 0
MSG_TIMER = 1
RET_DO_DEFAULT = -1
WS_THICKFRAME = 262144 ; Style adds sizing border

#DefineFunction GetStyle(hWnd)
user32 = StrCat(DirWindows(1),"user32.dll")
GWL_STYLE = -16 ; offset for the Window Style value
ret = DllCall(user32,long:"GetWindowLongA",long:hWnd,long:GWL_STYLE)
Return ret
#EndFunction

#DefineFunction SetStyle(hWnd,nstyle)
user32 = StrCat(DirWindows(1),"user32.dll")
GWL_STYLE = -16 ; offset for the Window Style value
ret = DllCall(user32,long:"SetWindowLongA",long:hWnd,long:GWL_STYLE,long:nstyle)
Return ret
#EndFunction

; 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(DlgHandle,CtrlNum,Text)
WM_GETFONT = 49
ControlHandle = cWndbyid(DlgHandle,CtrlNum)
TextLen = StrCharCount(Text)
hFont = SendMessageA(ControlHandle,WM_GETFONT,0,0)

; Use dialog's DC
HDC = DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"GetDC",long:DlgHandle)

; Save current font
hOldFont = DllCall(StrCat(DirWindows(1),"GDI32.DLL"),long:"SelectObject",long:HDC,long:hFont)
lpsize = BinaryAlloc(8)
result = DllCall(StrCat(DirWindows(1),"GDI32.DLL"),long:"GetTextExtentPoint32A",long:HDC,lpstr:Text,long:TextLen,lpbinary:lpsize)
TextWidth = BinaryPeek4(lpsize,0)
BinaryFree(lpsize)

; Restore previous font
DllCall(StrCat(DirWindows(1),"GDI32.DLL"),long:"SelectObject",long:HDC,long:hOldFont)
DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"ReleaseDC",long:DlgHandle,long:hdc)

Return TextWidth
#EndFunction


; Convert text width value to dialog units
#DefineFunction TextWidthToDialogUnits(TextWidth,hPos)
Return Int(TextWidth * 1.0 / WinMetrics(0) * 1000) + hPos + (WinMetrics(7) * 2)
#EndFunction


; Get current dialog position
#DefineSubRoutine GetDialogPosition()
Pos = WinPlaceGet(@NORMAL,Title)
A = ItemExtract(1,Pos," ")
B = ItemExtract(2,Pos," ")
C = ItemExtract(3,Pos," ")
D = ItemExtract(4,Pos," ")
#EndSubRoutine


; Calculate number of columns that will fit in current dialog width
#DefineSubRoutine  GetNumCol()
GetDialogPosition()
RequiredDialogWidth = 0
NumCol = 0

While C>RequiredDialogWidth
   NumCol = NumCol + 1
   Text = MakeString(NumCol)
   cx = StrCharCount(Text) * TextWidth
   RequiredDialogWidth = TextWidthToDialogUnits(cx,A)
EndWhile

If RequiredDialogWidth>C
   NumCol = NumCol - 1
   Text = MakeString(NumCol)
EndIf

#EndSubRoutine


; Main dialog callback procedure
#DefineSubRoutine MainDlgProc(MainDlg_Handle,MainDlg_Message,MainDlg_ID,MainDlg_EventInfo,rsvd)
Switch MainDlg_Message

Case MSG_INIT

   DialogProcOptions(MainDlg_Handle,1002,4) ; Add system menus

   ; Add a "sizable" boarder
   nStyle = GetStyle(MainDlg_Handle)
   nStyle = nStyle | WS_THICKFRAME
   SetStyle(MainDlg_Handle,nStyle)

   ; Set initial dialog size based on starting text
   SampleChar = StrSub(Text,1,1) ; Assume a fixed-width font
   TextWidth = GetTextWidth(MainDlg_Handle,100,SampleChar)
   cx = StrCharCount(Text) * TextWidth
   GetDialogPosition()
   RequiredDialogWidth = TextWidthToDialogUnits(cx,A)
   WinPlaceSet(@NORMAL,Title,A:" ":B:" ":RequiredDialogWidth:" ":D)

   ; Set up timer loop
   DialogProcOptions(MainDlg_Handle,MSG_TIMER,10)

   Return RET_DO_DEFAULT

Case MSG_TIMER

   OldNumCol = NumCol
   GetNumCol()

   If NumCol!=OldNumCol Then DialogControlSet(MainDlg_Handle,001,4,Text)

   Return RET_DO_DEFAULT

EndSwitch
#EndSubRoutine


; Main dialog
#DefineSubRoutine MainDlg(NumCol,Font)
   Text = MakeString(NumCol)
   Title = "Test:"
   MainDlgFormat = `WWWDLGED,6.1`
   MainDlgCaption = Title
   MainDlgX = -1
   MainDlgY = -1
   MainDlgWidth = 200
   MainDlgHeight = 050
   MainDlgNumControls = 001
   MainDlgProcedure=`MainDlgProc`
   MainDlgFont = `DEFAULT`
   MainDlgTextColor = `DEFAULT`
   MainDlgBackground = `DEFAULT,DEFAULT`
   MainDlgConfig = 0
   MainDlg001 = `0,015,9999,011,VARYTEXT,Text,"",DEFAULT,3,DEFAULT,`:Font:`,"0|0|0",DEFAULT`
   ButtonPushed = Dialog("MainDlg")
#EndSubRoutine

colcount =10
fixedwidthfont = "Courier|7373|40|34"
MainDlg(colcount,fixedwidthfont)

Article ID:   W17776
Filename:   Calculate Width of Given Text.txt
File Created: 2010:03:22:10:59:12
Last Updated: 2010:03:22:10:59:12