Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

XPrinting Text with Embedded Tab Characters (Uses Set POS)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin XPrinting Text with Embedded Tab Characters (Uses Set POS)

    Here's an approach to printing text strings using XPrint, where the text strings contain $TAB characters. Code is given for single and multi-line text.

    There are two basic approaches to printing with embedded TABs - expanding the TABs to spaces or setting the print POS to the next TAB location every time a TAB character is encountered.

    The example parses the line on $tab characters, then prints each element one at a time. After each element is printed, the x position is moved to the next tab location.

    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.


    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
    
    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
    Last edited by Gary Beene; 20 Aug 2009, 12:24 PM.
Working...
X