Date: 02-16-2022
Return to Index
created by gbSnippets
'Scintilla has very limited printing features, but the features it has are
'somewhat flexible because they involve printing to a display context - such
'as the DC to a graphic control or printer.
'Three features ot the drawn text can be controlled:
' - size
' - color
' - line wrap
'By default Scintilla prints using the current screen colors, but several
'color options are available:
' - invert
' - black on white
' - color on white
' - color on white (line numbers use their own BG color)
'Printing (drawing) text involves defining a rectangular area in which to
'print, the printable size of the page and a range of characers to print.
'Scintilla will format the text to fit the defined area, with optional line
'wrapping.
'Scintilla print output ignores all margins except a line margin. Markers,
'caret, and selections are hidden.
'Scintilla has not footer/header support, so you'll have to create those
'yourself in code. Since Scintilla fills the entire page with information,
'you must print any custom content after using the Scintilla print commands.
'Primary Code:
'You need a DC - a printer in this case
XPrint Get DC To hDCPrinter
R.hDC = hDCPrinter
R.hDCTarget = hDCPrinter
'All of the print information will be passed in a RangeToForm structure
Local R As RangeToFormat
'This code sets the printable area
R.rcPage.nLeft = MarginLeft
R.rcPage.nTop = MarginTop
R.rcPage.nRight = w - MarginRight
R.rcPage.nBottom = h - MarginBottom
'This code sets the print area (fits within rcPage)
R.rc.nLeft = R.rcPage.nLeft
R.rc.nTop = R.rcPage.nTop + 0.1 * h
R.rc.nRight = R.rcPage.nRight
R.rc.nBottom = R.rcPage.nBottom
'And the range of characters to print is defined this way
R.Chrg.cpmin = 0 'character range to print
R.Chrg.cpmax = 50 'character range to print
'To print, use this line
SendMessage hSci, %SCI_FormatRange, 1, VarPTR(R)
'Compilable Example: (Jose Includes)
'This example prints a header/footer, normal size and magnified text.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "scintilla_gb.inc"
%ID_Sci = 1000 : %ID_BtnA = 1001 : %ID_BtnB = 1002
Global hDlg, hSci, hLib, hDCPrinter As DWord
Function PBMain() As Long
hLib = LoadLibrary("SCILEXER.DLL")
Dialog New Pixels, 0, "Scintilla Example",300,300,300,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %ID_BtnA, "Print", 10,10,70,20, %WS_Child Or %WS_Visible
Control Add Button, hDlg, %ID_BtnB, "Print Big", 10,40,70,20, %WS_Child Or %WS_Visible
Control Add "Scintilla", hDlg, %ID_Sci, "", 100,10,180,130, %WS_Child Or %WS_Visible
Control Handle hDlg, %ID_Sci To hSci 'get handle to Scintilla window
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local txt As String
txt = "Select Case var$ 'first line" + $CrLf + "End Select 'last line" + Chr$(0)
Select Case CB.Msg
Case %WM_InitDialog
InitializeScintilla
PostMessage hSci, %SCI_SetSel, 0,0 'unselect initially
Case %WM_Command
Select Case CB.Ctl
Case %ID_BtnA : TestA
Case %ID_BtnB : TestB
End Select
Case %WM_Size
Control Set Size hDlg, %ID_Sci, Lo(Word, CB.lParam)-110, Hi(Word, CB.lParam)-20
Case %WM_Destroy
If hLib Then FreeLibrary hLib 'free the Scintilla library
End Select
End Function
Sub InitializeScintilla
Local txt As String
txt = "If x = 2 Then" + $CrLf + " 'do nothing" + $Crlf
txt = txt + "Else" + $crlf + " x = 0" + $crlf + "End If" + Chr$(0)
SendMessage hSci, %SCI_SetText, 0, StrPTR(txt)
SendMessage hSci, %SCI_SetMarginWidthN, 0, 20
End Sub
Sub TestA
PrintDemo
End Sub
Sub TestB
SendMessage hSci, %SCI_SetPrintMagnification, 10, 0 '10 pixels bigger
PrintDemo
End Sub
Sub PrintDemo
Local MarginLeft, MarginRight, MarginTop, MarginBottom, w, h As Long
Local R As RangeToFormat
XPrint Attach Default 'select a printer (default in this case)
XPrint Get Size To w,h
XPrint Get Margin To MarginLeft, MarginTop, MarginRight, MarginBottom
'header/footer
XPrint Set Pos (w/2 - 0.1*w, 0.05*h)
XPrint "Header" 'draw the text on the printer
XPrint Set Pos (w/2 - 0.1*w, 0.9*h)
XPrint "Footer"
'Scintilla printing on printer device context
XPrint Get DC To hDCPrinter
R.hDC = hDCPrinter
R.hDCTarget = hDCPrinter
'physically printable size
R.rcPage.nLeft = MarginLeft
R.rcPage.nTop = MarginTop
R.rcPage.nRight = w - MarginRight
R.rcPage.nBottom = h - MarginBottom
'rectangle in which to render the text (fits within rcPage)
R.rc.nLeft = R.rcPage.nLeft
R.rc.nTop = R.rcPage.nTop + 0.1 * h
R.rc.nRight = R.rcPage.nRight
R.rc.nBottom = R.rcPage.nBottom
R.Chrg.cpmin = 0 'character range to print
R.Chrg.cpmax = 50 'character range to print
SendMessage hSci, %SCI_FormatRange, 1, VarPTR(R)
XPrint Close 'sends info to printer, sends formfeed, detaches host printer
End Sub
'gbs_00647
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm