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,

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.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?