Draw text centered on printer page

Category: Printing

Date: 02-16-2022

Return to Index


 
'The following example draws the text both horizontally
'and vertically centered on the host printer page
 
'Primary Code:
Local x, y, w, h, w2, h2 As Long
Local sText AS String
sText = "PowerBASIC"              'text to print
XPrint Attach Default             'select a printer (default in this case)
XPrint Get Client TO w, h         'get print client size
XPrint Text Size sText TO w2, h2  'get text size
x = (w-w2) / 2                    'centered x-pos (left-right)
y = (h-h2) / 2                    'centered y-pos (top-bottom)
XPrint Set Pos (x, y)             'set print position to centered x/y
XPrint sText                      'draw the text on the printer
XPrint Close                      'sends info to printer, sends formfeed, detaches host printer
 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
Global hDlg as DWord
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 100,"Print", 50,10,100,20
   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 x, y, w, h, w2, h2 As Long
      Local sText AS String
      sText = "PowerBASIC"              'text to print
      XPrint Attach Default             'select a printer (default in this case)
      XPrint Get Client TO w, h         'get print client size
      XPrint Text Size sText TO w2, h2  'get text size
      x = (w-w2) / 2                    'centered x-pos (left-right)
      y = (h-h2) / 2                    'centered y-pos (top-bottom)
      XPrint Set Pos (x, y)             'set print position to centered x/y
      XPrint sText                      'draw the text on the printer
      XPrint Close                      'sends info to printer, sends formfeed, detaches host printer
   End If
End Function
 
'gbs_00211
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm