In C++ Builder, it is easy to use full features of the attached Printer Device by using RTL (Run Time Library) features is that both FMX (FireMonkey) and VCL frameworks provide similar coding and both are capable to use printer dialog which allows users to select and set up printer device.
The printer property ( TPrinter for VCL or TPrinter for FMX ) is used to manage any printing performed by an application. You can obtain an instance of TPrinter by calling the global VCL and FMX Printer function. The printer is declared in the printer unit and the Printer class is included in Printers.hpp header.
TPrinter class uses a TCanvas property which is identical to the Canvas of Form or Bitmap. This means that anything that can be drawn on a Printer, as well as on a form or bitmap. To print any graphical shapes, call the BeginDoc() method followed by whatever canvas graphics you want to print and send the job to the printer by calling the EndDoc() method. During this process we can the all drawing methods like DrawLine(), DrawRect(), FillRect(), DrawEllips(), TextOut() methods, and more which are described before on this Quickly Learn How To Use Canvas Drawing In C++ On Windows topic.
We can execute Printer Dialog by using PrintDialog (TPrintDialog) component as below,
1 2 3 |
PrintDialog1->Execute(); |
and we can get size of paper of the selected printer device as below;
1 2 3 4 |
int W=Printer()->PageWidth; int H=Printer()->PageHeight; |
and we can start drawing things to canvas of printer by using BeginDoc() method as here,
1 2 3 |
Printer()->BeginDoc(); |
and we can start drawing all things to print, i.e,
1 2 3 |
Printer()->Canvas->Rectangle( 100, 100, 200, 200); |
finally we finish our templete and we can print it by using EndDoc() as below,
1 2 3 |
Printer()->EndDoc(); |
In safe mode, we can use all these above with try{ …} __finally{…} and we can safely draw to printer device as given below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if (PrintDialog1->Execute()) { Printer()->BeginDoc(); try { // draw things into Printer()->Canvas } __finally { Printer()->EndDoc(); } } |
Officially, Printing in VCL applications is described well here and Printing from a FireMonkey Application is explained well here. There is a good topic about using this Printer method with image printing example in David.I’s blogpost here too.
In this post, we will give a very simple example to use printer device in your C++ Builder applications. We will draw a draw rectangle in accordance with the the size of Canvas of your Printer device.
Printing in VCL Application
- Create a new C++ Builder VCL Application in C++ Builder
- Save All Unit and Project file into a folder, i.e. create a PrintingVCL folder and save there
- Drag a Button and a PrinterDialog from Tools Palette
- Double click to Button to create OnClick() event and add lines below ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
void __fastcall TForm1::Button1Click(TObject *Sender) { if (PrintDialog1->Execute()) { Printer()->BeginDoc(); try { Printer()->Canvas->Rectangle( 50, 50, Printer()->PageWidth-50, Printer()->PageHeight-50); Printer()->Canvas->Rectangle( 100, 100, 200, 200); Printer()->Canvas->TextOut( 100, 120, L"Dear Developer," ); } __finally { Printer()->EndDoc(); } } |
5. Run by F9, if your code doesn’t work full VCL code should be like this,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <vcl.h> #pragma hdrstop #include "Printing_to_Printer_VCL_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if (PrintDialog1->Execute()) { Printer()->BeginDoc(); try { Printer()->Canvas->Rectangle( 50, 50, Printer()->PageWidth-50, Printer()->PageHeight-50); Printer()->Canvas->Rectangle( 100, 100, 200, 200); Printer()->Canvas->TextOut( 100, 120, L"Dear Developer," ); } __finally { Printer()->EndDoc(); } } } |
Printing in FMX Application
- Create a new C++ Builder FMX Application in C++ Builder
- Save All Unit and Project file into a folder, i.e. create a PrintingVCL folder and save there
- Drag a Button and a PrinterDialog from Tools Palette
- Double click to Button to create OnClick() event and add lines below ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
void __fastcall TForm1::Button1Click(TObject *Sender) { if (PrintDialog1->Execute()) { Printer()->BeginDoc(); try { Printer()->Canvas->Rectangle( 50, 50, Printer()->PageWidth-50, Printer()->PageHeight-50); Printer()->Canvas->Rectangle( 100, 100, 200, 200); Printer()->Canvas->TextOut( 100, 120, L"Dear Developer," ); } __finally { Printer()->EndDoc(); } } |
5. Run by F9, if your code doesn’t work full FMX code should be like this,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <fmx.h> #pragma hdrstop #include "Printing_to_Printer_FMX_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if (PrintDialog1->Execute()) { Printer()->BeginDoc(); try { Printer()->Canvas->Rectangle( 50, 50, Printer()->PageWidth-50, Printer()->PageHeight-50); Printer()->Canvas->Rectangle( 100, 100, 200, 200); } __finally { Printer()->EndDoc(); } } } |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition