Click here to Skip to main content
11,929,232 members (60,246 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked

A C++ Barcode Library

, 29 Mar 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
An easy-to-use barcode library file for Win32 and MFC programmers

Screenshot of Main Dialog


The Library file presented is Barcode.h under Lib directory contains inline C++ code for:

  • Code39
  • Code93
  • CodeI2of5
  • Code128A
  • Code128B
  • Code128C

The library file is targeted to Win32, so it can be used by both MFC and Win32 programs. 
All classes are almost the same from the user's point of view, only 2 functions are needed to be used.

  1. Encode...()
  2. Draw...(...)

For example, class for Code39 is Barcode39:

Call default constructor first, then call the following 2 functions:

BOOL Encode39(const char*pszCodeIn);
void Draw39(HDC hDC,int iX,int iY0,int iY1,const COLORREF clrBar,
			const COLORREF clrSpace,const int iPenW); 
Parameters of the above functions:
  • pszCodeIn: string to be encoded, i.e. "1234567890"
  • iX: start horizontal position to draw the barcode
  • iY0,iY1: start and end vertical positions to draw the barcode
  • clrBar, clrSpace: colors of bar and space
  • iPenW: width of pen to draw the barcode

Array for Bars and Spaces

A byte array is defined in BarcodeBase class as BYTE ia_Buf[4096];
Each byte of the array represents a bar or space, bits of the byte are encoded in the format:
First 6 bits are not used.
Last bit is flagged for bar or space.
Last second bit is flagged for wide or narrow.

So, value of the byte means:

  • 0x00 = a narrow space
  • 0x01 = a narrow bar
  • 0x02 = a wide space
  • 0x03 = a wide bar

Note: Class BarcodeEan13 is special: Last third bit is used by the class to indicate if the bar (space) is longer than others.

Sample Program

The sample is a dialog based MFC program. The main dialog is in borderless style, when edit-box Move (refer to picture below) gets focus, user can use arrow keys of up, down, left and right to move main dialog. This feature allows you to compare barcode drawn on main dialog with other barcode programs easily. You also can use mouse to drag main dialog by clicking on it as clicking on caption of normal windows.



  • OnTop/OnNormal: Set the program as top-most-window or not
  • Close: Close the program
  • Help: Display this HTML file
  • Print: Print barcode


  • Style: Barcode styles or symbologies
  • Map: Map-mode used
  • Move: Move main dialog by arrow keys once it gets focus
  • Code: String to be encoded
  • X,Y,Height: For print positions, unit is in logic points
  • Pen width: Width of one bar or space

Please note

Some MapModes (such as MM_HIENGLISH) cannot be displayed properly on screen, you have to test them by printing.


This sample and library file are my test program for barcode, which doesn't include second part: dynamical bitmap of barcode generated from encoding.

Why do I test barcode with bitmap? Here is the answer:
As we know, there are three methods for printing:

  1. By drawing such as LineTo()
  2. By Bitmap such as BitBlt()
  3. By fonts such as TextOut()

Barcode must be printed clearly with high qualities. The only way to print a good barcode is by fonts. If you try to use the code for commercial purposes, the result is very disappointing because this program does not use fonts.


  1. Barcode serial articles by Mr. Neil Van Eps
  2. Articles by Mr. Lam Do Thanh


  • 29th March, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Australia Australia

Please visit our Download Home to obtain many interesting software for free ...

You may also be interested in...

Comments and Discussions

GeneralCode128 / FNC3 Pin
tremblay.simon29-Nov-12 3:59
membertremblay.simon29-Nov-12 3:59 
QuestionThanks a lot Pin
Alex_118-Nov-12 15:01
memberAlex_118-Nov-12 15:01 
Questiondo have sample for qrcode? Pin
rick_012113-Feb-11 22:08
memberrick_012113-Feb-11 22:08 
QuestionHow to read barcode and display the numbers for Barcode39?S.O.S ! Pin
ttarantula9-Jul-08 0:21
memberttarantula9-Jul-08 0:21 
GeneralBarcode reader photo detector Pin
edcornj3-May-08 4:20
memberedcornj3-May-08 4:20 
GeneralThank to share the program Pin
Shi Jun17-Sep-07 16:48
memberShi Jun17-Sep-07 16:48 
QuestionDo you have library to decode barcode image? Pin
Pham Tuan21-Sep-06 22:08
memberPham Tuan21-Sep-06 22:08 
GeneralNotice from author Pin
includeh1029-Mar-06 17:27
memberincludeh1029-Mar-06 17:27 
GeneralRe: Notice from author Pin
VladaTzar25-Aug-09 1:20
memberVladaTzar25-Aug-09 1:20 
QuestionLibrary file is missing? Pin
delphidab29-Mar-06 10:32
memberdelphidab29-Mar-06 10:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 29 Mar 2006
Article Copyright 2006 by includeh10
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid