Another thread is covering printing richedit content. This one discusses printing text strings using XPrint, where the text strings contain $TAB characters.
I'll post the code in the Source Code forum as soon once this thread completes. I figure there are always improvements someone will suggest - something I welcome.
There are two basic approaches to printing with embedded TABs - expanding the TABs to spaces (fixed-width fonts) or setting the print POS (variable-width fonts) every time a TAB character is encountered.
This code works with variable width fonts.
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.
This took surprisingly little code. Here's the primary code involved:
This example uses a richedit control, whose default tab positions are set every 0.5 inches.
There are two critical parts to this approach.
1. The printer and richedit control must be set to the same font.
2. The Tab positions of the richedit must be used on the printer.
Break either of these rules and the printed result may not look as it did in the richedit control.
This approach does not print with the same formatting (variable text size, colors, ...) that printing with RTF provides. But it does offer an easy way to programmatically add text (such as headers/footers).
This is another case where I expected to see several solutions in the forums, and was surprised not to find the code. It would seem like a very common event - printing text with embedded tabs. Perhaps I just missed it - that seems to happen a lot, where the search terms I use don't always locate the discussion in the forum.
I'll post the code in the Source Code forum as soon once this thread completes. I figure there are always improvements someone will suggest - something I welcome.
There are two basic approaches to printing with embedded TABs - expanding the TABs to spaces (fixed-width fonts) or setting the print POS (variable-width fonts) every time a TAB character is encountered.
This code works with variable width fonts.
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.
This took surprisingly little code. Here's the primary code involved:
Code:
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
There are two critical parts to this approach.
1. The printer and richedit control must be set to the same font.
2. The Tab positions of the richedit must be used on the printer.
Break either of these rules and the printed result may not look as it did in the richedit control.
Code:
'Compilable Example: #Compile Exe #Dim All #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
This is another case where I expected to see several solutions in the forums, and was surprised not to find the code. It would seem like a very common event - printing text with embedded tabs. Perhaps I just missed it - that seems to happen a lot, where the search terms I use don't always locate the discussion in the forum.
Comment