How to write in C++Builder Firemonkey. Make a barcode.

Posted by on in Programming

Make a barcode with C++Builder Firemonkey.

This time, I used DEKO-san library.

When unpacking, there is a BARCODE.PAS file inside.

This BARCODE.PAS file needs to be rewritten a bit.

That's pretty simple. Just change String to AnsiString.


Next, I will make a new project. The project is C++Builder Firemonkey.

Add BARCODE.PAS to the project.

BARCODE.hpp will be created when building.And then design the screen.

In the button click event, and create a bar code.

This BARCODE.hpp supports many formats. But here we will only process Code128.

void __fastcall TForm1::Button1Click(TObject *Sender)
	///Convert character strings to bar-codes.
	f_bcode	= Make_Code128(Edit1->Text, Edit2->Text[1]).c_str();

Edit1->Text is the character string A-Z. Edit2->Text[1] is the start code.

I am requesting the drawing of the bar code part at the timer event.


void __fastcall TForm1::Timer1Timer(TObject *Sender)


It is a barcode drawing part.

void __fastcall TForm1::Layout1Paint(TObject *Sender, TCanvas *Canvas, const TRectF &ARect)

	Canvas->Fill->Kind	= TBrushKind::Solid;
	Canvas->Fill->Color	= TAlphaColors::White;
	Canvas->FillRect(ARect,0,0,AllCorners,1);///Make the Layout1 background white.
	if ( f_bcode.size() > 0 )
		int _pos = 0;
		for (char s : f_bcode)
			std::string _str = {s};
			int _width = std::stoi(_str);
			int _linew = (static_cast<int>(_width/2)+1)*3;
			if ((_width % 2) == 0)
				_pos = _pos + _linew;
				for (int i = 0; i < _linew; i++)
					Canvas->Stroke->Kind = TBrushKind::Solid;
					Canvas->StrokeThickness  = 1;
					Canvas->Stroke->Color = TAlphaColorRec::Navy;
					Canvas->DrawLine(TPointF(_pos + 30, 0), TPointF(_pos + 30, ARect.Bottom), 1);



When running in this, the bar code is displayed.


Because I used AnsiString, mobile can not correspond.Windows only.

Someday, I hope that DEKO-san is me correspond. It's Firemonkey version.




Gold User, No rank,
Delphi / C++Builder blogger


Check out more tips and tricks in this development video: