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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Get A Substring of a Wide String in C++

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

How To Access Individual Character Elements Of A C++ String

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

The Right Way To Access Character Elements Of A Wide String

C++Language FeatureLearn C++

You NEED to Learn To Use JSON (JavaScript Object Notation)

en_USEnglish