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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?