Introduction to C++Learn C++

Learn To Use Printing In Modern C++ Windows Applications

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,

and we can get size of paper of the selected printer device as below;

and we can start drawing things to canvas of printer by using BeginDoc() method as here,

and we can start drawing all things to print, i.e,

finally we finish our templete and we can print it by using EndDoc() as below,

In safe mode, we can use all these above with try{ …} __finally{…} and we can safely draw to printer device as given below,

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

  1. Create a new C++ Builder VCL Application in C++ Builder
  2. Save All Unit and Project file into a folder, i.e. create a PrintingVCL folder and save there
  3. Drag a Button and a PrinterDialog from Tools Palette
  4. Double click to Button to create OnClick() event and add lines below ,

5. Run by F9, if your code doesn’t work full VCL code should be like this,

Printing in FMX Application

  1. Create a new C++ Builder FMX Application in C++ Builder
  2. Save All Unit and Project file into a folder, i.e. create a PrintingVCL folder and save there
  3. Drag a Button and a PrinterDialog from Tools Palette
  4. Double click to Button to create OnClick() event and add lines below ,

5. Run by F9, if your code doesn’t work full FMX code should be like this,

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Related posts
C++Learn C++Videos

Learn C++ With Design Patterns For Handling/Reporting Errors In C++: Parallel Algorithms And Executors by Mark Hoemmen (CPPCon 2020) Videos

C++Introduction to C++Learn C++

Learn To Use Break And Continue In Loops With C++

C++Introduction to C++Learn C++

Learn To Define And Use Arrays In C++

C++Learn C++Videos

Learn C++ With Back to Basics: Design Pattern by Mike Shah (CPPCon 2020) Video

en_USEnglish