Date: 02-16-2022
Return to Index
created by gbSnippets
'When text information contains TAB characters, the usually
'solution of expanding TABs to spaces doesn't work well except
'for fixed-width fonts. This solution works for proportional fonts.
'This snippet prints a single line. Another snippet covers multi-line text.
'Primary Code:
'The method is to parse the line on $tab characters, then print
'each element one at a time. After each element is printed, the
'x position is moved to the next tab location. Tab locations
'are predefined in this example to be on 0.5" boundaries.
Sub PrintSingleTextLineWithTabs (temp$)
Local i As Long, x As Single, y As Single
For i = 1 To ParseCount(temp$, $Tab)
XPrint Parse$(temp$,$Tab, i) ;
XPrint Get Pos To x,y
XPrint Set Pos (TabLoc((Fix(x/0.5)+1)),y)
Next i
XPrint
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."
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$
PrintSingleTextLineWithTabs (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 PrintSingleTextLineWithTabs (temp$)
Local i As Long, x As Single, y As Single
For i = 1 To ParseCount(temp$, $Tab)
XPrint Parse$(temp$,$Tab, i) ;
XPrint Get Pos To x,y
XPrint Set Pos (TabLoc((Fix(x/0.5)+1)),y)
Next i
XPrint
End Sub
'gbs_00301
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm