Date: 02-16-2022
Return to Index
created by gbSnippets
'When text information contains TAB characters, the usual
'solution of expanding TABs to spaces doesn't work well except
'for fixed-width fonts. This solution works for proportional fonts
'and support multiple lines as well.
'Primary Code:
'The method is to parse the text into lines, then parse each line on
'$tab characters. Each element is then printed one at a time.
'After each element is printed, the x position is moved to the next
'tab location. Default Tab locations of every 0.5" are used.
Sub PrintMultiLineTextWithTabs (temp$)
Local i As Long, x As Single, y As Single, j as long
Dim D(ParseCount(temp$,$crlf)-1) as String
Parse temp$, D(), $crlf
For j = 0 to UBound(D)
For i = 1 To ParseCount(D(j), $Tab)
XPrint Parse$(D(j),$Tab, i) ;
XPrint Get Pos To x,y
XPrint Set Pos (TabLoc((Fix(x/0.5)+1)),y)
Next i
XPrint
Next J
End Sub
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "RichEdit.inc"
#Include "CommCtrl.inc"
Global hDlg as Dword, hRichEdit as Dword, TabLoc() as Single, hFont as Dword
%ID_RichEdit = 500
Function PBMain() As Long
Local style&, buf$
buf$ = "This is" + $Tab + $Tab + "an example" + $Tab + "of a string with an embedded tab character."
buf$ = buf$ + $crlf + "2This is" + $Tab + "2an example" + $Tab + "2of a string with an embedded tab character."
style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
Dialog New Pixels, 0, "Test Code",300,300,550,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Print", 30,10,140,20
LoadLibrary("riched32.dll") : InitCommonControls
Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,20,40,510,100, style&, %WS_EX_ClientEdge
Control Handle hDlg, %ID_RichEdit To hRichEdit
Font New "Comic Sans MS", 10, 0 To hFont
Control Set Font hDlg, %ID_RichEdit, hFont
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Local temp$, w as Single, h as Single, i as Long
ReDim TabLoc(50) 'tab locations
For i = 0 To 50 : TAbLoc(i) = i * 0.5 : Next i '0.5" tab locations
XPrint Attach Default
XPrint Set Font hFont
SetPrinterScaleToInches w,h 'w,h are return values, not used in this example
Control Get Text hDlg, %ID_RichEdit To temp$
PrintMultiLineTextWithTabs (temp$) 'temp$ is text string with tabs
XPrint Close
End If
End Function
Sub SetPrinterScaleToInches (ncWidth!, ncHeight!)
Local x&, y&
XPrint Get Client TO ncWidth!, ncHeight! 'Retrieve the client size (printable area) of the printer page
XPrint Get PPI TO x&, y& 'Retrieve the resolution (points per inch) of the attached printer
ncWidth! = ncWidth!/x& 'Width in inches of the printable area
ncHeight! = ncHeight!/y& 'Height in inches of the printable area
XPrint Scale (0,0)-(ncWidth!,ncHeight!) 'Set scale to inches
End Sub
Sub PrintMultiLineTextWithTabs (temp$)
Local i As Long, x As Single, y As Single, j as long
Dim D(ParseCount(temp$,$crlf)-1) as String
Parse temp$, D(), $crlf
For j = 0 to UBound(D)
For i = 1 To ParseCount(D(j), $Tab)
XPrint Parse$(D(j),$Tab, i) ;
XPrint Get Pos To x,y
XPrint Set Pos (TabLoc((Fix(x/0.5)+1)),y)
Next i
XPrint
Next J
End Sub
'gbs_00302
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm