QPrn Component

Qprn is a replacement for the QPrinter-object. I tried to stay as near as possible at the original syntax,
but it was not possible and meaningful at every command.
Please note that some printers do not support Draw and other graphic commands .
Before printing, you usually should call PrintDialog, so the user may choose his printer.
But it's also possible to print without calling PageDialog or PrintDialog, then we use the windows-default.
Windows delivers all papersizes in millimeters, so I decided to handle all sizes and margins in millimeters.
Nevertheless, the user sees the PageSetup-Dialog like its choosen in the controlpanel.
If the user has choosen inch, the program will convert if necessary.

I took QPageSetup version 1.3 to begin (original code D. Glodt, modified by John Kelly 8/2005)
added big parts from QPrint.inc (David Burkley, Oct. 24th, 2010, Email: burkleyd@yahoo.com),
from Printer.bas (programmed by John White (danilo_yoingco@yahoo.co.uk)) and from
PrinterDialog.inc (don't know who made this) and played a lot with all that to find a working
balance. Thank you for all your work, making this possible !

Version 1.0 , Nov. 01, 2010

V. Dobiasch (vdobiasch@2ya.de), donated to Public Domain

Version 1.1 , Nov. 04, 2010

BkMode added for transparent background at TEXTOUT and TEXTRECT, RicheditOut Header and Footer added

Version 1.2 , Mar. 19, 2011

Bugfixes, PrintPreview added (inspired by David Burkley), HeaderMiddle, HeaderRight, FooterMiddle, FooterRight added


QPrn Properties

Field

Type

R/W

Default





BkMode

LONG

W

OPAQUE


Background mix mode for TEXTOUT and TEXTRECT.
Valid: OPAQUE and TRANSPARENT

BrushHatch

LONG

W

HS_SOLID


BrushHatch defines the pattern for filling graphics like FILLRECT, ROUNDRECT and ELLIPSE.

BrushStyle

LONG

W

BS_HATCHED


BrushStyle defines the style for filling graphics like FILLRECT, ROUNDRECT and ELLIPSE. For transparency use BS_NULL, otherwise BS_HATCHED.

Copies

INTEGER

RW

1


Number of copies to print.

CopyMode

INTEGER

W

SRCCOPY


Only used for STRETCHDRAW.

Collate

BOOLEAN

RW

False

DefaultPrinter

STRING

R


Defaultprinter of the system.

DefaultSource

SHORT

RW

DMBIN_AUTO


Depends on your printer.

DisableMargins

BOOLEAN

W

False


Disables the Margin-fields in PageDialog.

DisableOrientation

BOOLEAN

W

False


Disables the Orientation-fields in PageDialog.

DisablePaper

BOOLEAN

W

False


Disables the Paper-selections in PageDialog.

DisablePrinter

BOOLEAN

W

True


Disables the Printer-selection in PageDialog.

Duplex

SHORT

RW

DMDUP_SIMPLEX


For printing on both sides of the paper, use DMDUP_HORIZONTAL or DMDUP_VERTICAL, depending on the capabilities of your printer.

Footer

STRING

W



The Footer is printed on the left side of the last line of the page when using RichEditOut or PrintPreview. %PAGE% is replaced with the pagenumber, %PAGES% is replaced with the pagecount.

FooterMiddle

STRING

W



FooterMiddle is printed in the middle of the last line of the page when using RichEditOut or PrintPreview. %PAGE% is replaced with the pagenumber, %PAGES% is replaced with the pagecount.

FooterRight

STRING

W



FooterRight is printed on the right side of the last line of the page when using RichEditOut or PrintPreview. %PAGE% is replaced with the pagenumber, %PAGES% is replaced with the pagecount.

Font

QFONT

W


FromPage

LONG

RW

1


For the pagenums of the PrintDialog.

Header

STRING

W



The Header is printed on the left side of the first line of the page when using RichEditOut or PrintPreview. %PAGE% is replaced with the pagenumber, %PAGES% is replaced with the pagecount.

HeaderMiddle

STRING

W



HeaderMiddle is printed in the middle of the first line of the page when using RichEditOut or PrintPreview. %PAGE% is replaced with the pagenumber, %PAGES% is replaced with the pagecount.

HeaderRight

STRING

W



HeaderRight is printed on the right side of the first line of the page when using RichEditOut or PrintPreview. %PAGE% is replaced with the pagenumber, %PAGES% is replaced with the pagecount.

JobID

LONG

R



Only valid while printing (between BEGINDOC and ENDDOC).

MarginBottom

LONG

RW



The bottom margin in mm of/for the PagesetupDialog.

MarginBottomPix

LONG

R



The bottom margin in pixels, calculated out of MarginBottom for the current printer.

MarginLeft

LONG

RW



The left margin in mm of/for the PagesetupDialog.

MarginLeftPix

LONG

R



The left margin in pixels, calculated out of MarginLeft for the current printer.

MarginRight

LONG

RW



The right margin in mm of/for the PagesetupDialog.

MarginRightPix

LONG

R



The right margin in pixels, calculated out of MarginRight for the current printer.

MarginTop

LONG

RW



The top margin in mm of/for the PagesetupDialog.

MarginTopPix

LONG

R



The top margin in pixels, calculated out of MarginTop for the current printer.

MaxPages

LONG

W

9999

MeterInch

LONG

R

PSD_INHUNDREDTHSOFMILLIMETERS


Indicates whether the user works with millimeters (PSD_INHUNDREDTHSOFMILLIMETERS) or in inch (PSD_INTHOUSANDTHSOFINCHES).

Orientation

BOOLEAN

RW



Orientation determines if the print job prints vertically or horizontally on a page.
Valid orientation values are:
0 = poPortrait (Vertical print)
1 = poLandscape (Horizontal print)

PageHeight

LONG

RW



The pageheight in mm of/for the PagesetupDialog.

PageHeightPix

LONG

R



The pageheight in pixels, calculated out of PageHeight for the current printer.

PageWidth

LONG

RW



The pagewidth in mm of/for the PagesetupDialog.

PageWidthPix

LONG

R



The pagewidth in pixels, calculated out of PageWidth for the current printer.

PaperSize

WORD

RW



For details see the DMPAPER_ -constants.

ParentHandle

LONG

W

0


There is no need to set ParentHandle. But if you want to have the dialogs where your mainwindow is, you may set it to your MainForm.Handle.

PenStyle

LONG

W

PS_SOLID


PenStyle defines the style of the lines for graphics like LINE, FILLRECT, ROUNDRECT and ELLIPSE.

PenSize

LONG

W

12


PenSize defines the thickness of the lines for graphics like LINE, FILLRECT, ROUNDRECT and ELLIPSE. For some PenStyles like PS_DASH, PS_DOT, PS_DASHDOT, PS_DASHDOTDOT PenSize must be 1.

PrintColor

SHORT

RW

DMCOLOR_COLOR


Use DMCOLOR_MONOCHROME for printing black&white.

PrinterName

STRING

RW



Name of the current Printer. Shows the default printer when starting.

PrintQuality

SHORT

RW

DMRES_DRAFT


Try also DMRES_HIGH, DMRES_MEDIUM and DMRES_LOW, depending on the capabilities of your printer.

prt_DC

LONG

R

0


This is the DeviceContext for printing.
You may use this to define your own printing methods which are missing right now.

Title

STRING

RW



Title is the text that appears in the Print Manager and on network header pages.

ToPage

LONG

RW

9999


For the pagenums of the PrintDialog.


   QPrn Methods

Method

Type

Description

Params





Initialise

FUNCTION () AS BOOLEAN

Initialises the component to the Defaultprinter. This is called when you DIM Qprn.

0

PageDialog

FUNCTION () AS BOOLEAN

Shows the PageSetup-Dialog, Result = True means success, False means Error.

0

PrintDialog

FUNCTION (STYLE AS LONG) AS BOOLEAN

Shows the PrintDialog, Result = True means success, False means Error.
Style is (PD_ALLPAGES Or PD_DISABLEPRINTTOFILE OR PD_HIDEPRINTTOFILE OR PD_USEDEVMODECOPIESANDCOLLATE), called PD_Default.

1

BeginDoc

FUNCTION () AS BOOLEAN

Starts a new document to print.

0


BeginDoc sends a print job to the printer.

Abort

FUNCTION () AS BOOLEAN

Abort printing the document, Result = True means success, False means Error.

0

EndDoc

FUNCTION () AS BOOLEAN

Nothing is printed until this is called.

0


EndDoc will start the print job.

NewPage

FUNCTION () AS BOOLEAN

Start printing on a new page.

0

Chord

FUNCTION (x1%, y1%, x2%, y2%, r1%, r2%, r3%, r4%, c%, fill%) AS BOOLEAN

Draw & Fill a Chord.

10

Circle

FUNCTION(x1%, y1%, x2%, y2%, c%, fill%) AS BOOLEAN

Draw & Fill a Circle or a Ellipse.

6

Draw

FUNCTION(x%, y%, BMP) AS BOOLEAN

Draw a Bitmap.

3

FillRect

FUNCTION(x1%, y1%, x2%, y2%, c%, fill%) AS BOOLEAN

Draws & Fills a rectangle.

6

Line

FUNCTION (x1%, y1%, x2%, y2%, c%) AS BOOLEAN

Draws a line.

5

RicheditOut

FUNCTION (QRichedit, mode%, Header$, Footer$) AS BOOLEAN

Prints a QRichedit. Mode 0 calculates how many pages you need (without printing), Mode 1 prints all, Mode 3 prints the selected text. Header and Footer is used as described earlier.

2

RoundRect

FUNCTION(x1%, y1%, x2%, y2%, x3%, y3%, c%, fill%) AS BOOLEAN

Draws & Fills a rounded rectangle.

8

StretchDraw

FUNCTION (x%, y%, w%, h%, BMP) AS BOOLEAN

Draw BMP and stretch to fit inside Rect x y w h.

5

TextHeight

FUNCTION (Text$) AS LONG

Returns the height of Text$ in pixels.

1

TextWidth

FUNCTION (Text$) AS LONG

Returns the width of Text$ in pixels.

1

TextRect

FUNCTION (x1%, y1%, x2%, y2%, S$, format%, fc%, bc%) AS BOOLEAN

Write text, and clip within region. Format may be DT_LEFT, DT_CENTER, DT_RIGHT ...

8

TextOut

FUNCTION (x%, y%, S$, fc%, bc%) AS BOOLEAN

Writes text to printer.

5

PrintPreview

SUB (hForm AS LONG , hSource AS LONG , SourceLen AS LONG)

Shows the PrintPreview-Form. Needs the handle of the parentform, the handle of the QRichedit-component and the length of the Qrichedit-text.

0


PrintPreview is shown with the position and size of the parentform.
Before calling PrintPreview you may alter the following resources of the form:
PreviewForm AS QFORM Window for displaying the preview.
CtrlPanel AS QPANEL Toolbar for holding all the buttons.
CtrlPrint AS QCOOLBTN Button for printing the preview pages.
CtrlPage AS QCOOLBTN Button for page setup dialog.
CtrlExit AS QCOOLBTN Button for exit previewform.
CtrlBreak1 AS QPANEL Breakline between the buttons.
CtrlFirst AS QCOOLBTN Button for jumping to the First page (of documents with multiple pages).
CtrlPrevious AS QCOOLBTN Button for viewing the Previous page (of documents with multiple pages).
CtrlLabel AS QLABEL Label for the field Page 'X of XX'.
CtrlEdit AS QEDIT Edit for the field Page 'X of XX'.
CtrlOfText AS STRING Text 'of' for the field Page 'X of XX' (for multi-language-use).
CtrlGoto AS QCOOLBTN Button for jumping to the page indicated by the field Page 'X of XX'.
CtrlNext AS QCOOLBTN Button for viewing the Next page (of documents with multiple pages).
CtrlLast AS QCOOLBTN Button for jumping to the Last page (of documents with multiple pages).
CtrlBreak2 AS QPANEL Breakline between the buttons.
CtrlBig AS QCOOLBTN Button for making the form bigger to show as much as possible of the page.
ViewScrollbox AS QSCROLLBOX Container for holding the components that are used for displaying a preview page (allows scrolling).
ChildPanel AS QPANEL Component that will be used for displaying the page.
CancelKey AS BOOLEAN Cancel the Preview by pressing Esc allowed ? (Default is true)
If you '$INCLUDE <RapidQ2.inc>', the preview-sheet may be scrolled with the mousewheel in all four directions.


   QPrn Example

$OPTION EXPLICIT
$INCLUDE "rapidq2.inc"
$INCLUDE "QPrn.inc"
DIM MyPrinter AS QPrn
DECLARE SUB MainForm_OnClose DECLARE SUB mfOpen_OnClick DECLARE SUB mpPageSetup_OnClick DECLARE SUB mpPreview_OnClick DECLARE SUB mpPrintDialog_OnClick DECLARE SUB mpPrintGraphics_OnClick
DIM OpenDialog AS QOPENDIALOG 'Necessary "Global" string variable DIM strFilename AS STRING
CREATE MainForm AS QFORM Caption = " QPrn - Demo" Width = 640 Height = 480 Left = (SCREEN.Width \ 2) - (MainForm.Width \ 2) Top = (SCREEN.Height \ 2) - (MainForm.Height \ 2) OnClose = MainForm_OnClose CREATE MainMenu AS QMAINMENU CREATE mFile AS QMENUITEM Caption = " File " CREATE mfOpen AS QMENUITEM Caption = "Open" OnClick = mfOpen_OnClick END CREATE CREATE mfLine AS QMENUITEM Caption = "-" END CREATE CREATE mfExit AS QMENUITEM Caption = "Exit" OnClick = MainForm_OnClose END CREATE END CREATE CREATE mPrint AS QMENUITEM Caption = " Print " CREATE mpPageSetup AS QMENUITEM Caption = "Page Setup" OnClick = mpPageSetup_OnClick END CREATE CREATE mpPreviewSetup AS QMENUITEM Caption = "Print-Preview" OnClick = mpPreview_OnClick END CREATE CREATE mpLine1 AS QMENUITEM Caption = "-" END CREATE CREATE mpPrintDialog AS QMENUITEM Caption = "Print Dialog" OnClick = mpPrintDialog_OnClick END CREATE CREATE mpPrintGraphics AS QMENUITEM Caption = "Print Graphics-Demo" OnClick = mpPrintGraphics_OnClick END CREATE END CREATE END CREATE CREATE RichEdit AS QRICHEDIT Align = alClient Wordwrap = False ScrollBars = ssBoth Font.Name = "Courier New" Font.Size = 10 END CREATE END CREATE
IF MyPrinter.PrinterName = "" THEN 'If there are no printers... disable the menus! 'If you don't a "page fault" will occur 'when the end-user selects one of them. mpPageSetup.Enabled = False mpPrintDialog.Enabled = False mPrint.Enabled = False END IF
SetWindowLong(MainForm.Handle , GWL_HWNDPARENT , HWND_DESKTOP) SetWindowLong(Application.Handle , GWL_HWNDPARENT , MainForm.Handle)
MyPrinter.parenthandle = MainForm.Handle MainForm.ShowModal
SUB MainForm_OnClose Application.Terminate END SUB
SUB mfOpen_OnClick IF OpenDialog.Execute THEN strFilename = OpenDialog.FileName RichEdit.LoadFromFile(strFilename) END IF END SUB
SUB mpPageSetup_OnClick MyPrinter.PageDialog END SUB
SUB mpPreview_OnClick IF strFilename = "" THEN strFilename = "NoName" MyPrinter.Title = strFilename MyPrinter.PrintPreview(MainForm.handle, RichEdit.handle, len(RichEdit.text)) END SUB
SUB mpPrintDialog_OnClick IF strFilename = "" THEN strFilename = "NoName" MyPrinter.richeditout(RichEdit , 0) 'Calculate pages' MyPrinter.Title = strFilename DEFLNG Mode = MyPrinter.PrintDialog(IIF(RichEdit.SelLength = 0 , PD_Default OR PD_noselection , 0)) IF Mode THEN MyPrinter.begindoc MyPrinter.richeditout(RichEdit , IIF(Mode = 1 , 3 , 1)) 'Mode 3 for printing selected text, 1 for all MyPrinter.enddoc END IF END SUB
SUB mpPrintGraphics_OnClick MyPrinter.Title = "Graphics-Demo" MyPrinter.begindoc MyPrinter.Font.AddStyles fsBold MyPrinter.Font.AddStyles fsItalic MyPrinter.Font.Size = 20 MyPrinter.pensize = 15 MyPrinter.penstyle = PS_Dashdot MyPrinter.brushhatch = HS_Cross MyPrinter.Line(MyPrinter.MarginLeftPix , MyPrinter.MarginTopPix , MyPrinter.MarginRightPix , MyPrinter.MarginTopPix , &H00FF00) MyPrinter.Circle(1000 , 1000 , 1200 , 1200 , &H00FF00 , &HFF0000) MyPrinter.RoundRect(1000 , 1300 , 1200 , 1500 , 150 , 120 , &H00FF00 , &HFF0000) MyPrinter.brushhatch = HS_Solid MyPrinter.Chord(1000 , 1600 , 1600 , 1900 , 1000 , 0 , 1500 , 1200 , &H00FF00 , &HFF0000) MyPrinter.FillRect(1000 , 2000 , 1200 , 2200 , &H00FF00 , &HFF0000) MyPrinter.TextOut(100 , 150 , "Test" , 0 , clBlue) MyPrinter.Font.DelStyles fsItalic MyPrinter.Font.Color = &HFF0000 MyPrinter.TextOut(3000 , 3000 , "TestTest" , - 1 , clBlack) MyPrinter.TextRect(2000 , 2500 , 3000 , 2800 , " Test blabla " , DT_CENTER , clGreen , clRed) DIM bit AS QBITMAP OpenDialog.Caption = "Which Graphic to print ..." OpenDialog.Filter = "Bitmap-Files|*.bmp|All files|*.*" IF OpenDialog.Execute THEN bit.BMP = OpenDialog.FileName MyPrinter.Stretchdraw(1000 , 2300 , 1400 , 2700 , bit) MyPrinter.Draw(1000 , 2800 , bit) END IF MyPrinter.enddoc END SUB