Adding Barcode 3 of 9 to Your Applications



Click here for a larger image.

Environment: VC6 Win32

There are barcode TrueType fonts available for printing 3 of 9 barcodes, but those fonts must be distributed with your final application to be sure they’re available on the end-user’s PC. They also require calculating and creating the proper size font for your barcode use. This code allows you to print any size and virtually any density 3 of 9 barcode from within your own code.

This code uses the bare Win32 API. MFC is NOT REQUIRED!

If you find you need to add barcode print functionality to your application, I offer this small C++ source code and header file consisting of three functions:

  • int BC39_Decode(char);
  • void BC39_Expand(int,char*);
  • int BC39_Draw(HDC,RECT*,char*,double,BOOL);

The first two functions, BC39_Decode() and BC39_Expand(), are used internally by BC39_Draw(); therefore, you only need to concern yourself with BC39_Draw().

In your source code, you only need to supply BC39_Draw() with five parameters to place a 3 of 9 barcode on your device context.

Parameter #1 is a handle to your device code context. This will normally be a printer DC because it doesn’t make a whole lot of sense to print barcodes to the screen except for print preview functionality.

Parameter #2 is a pointer to a RECT defining the area on the DC where the barcode will be placed.

Parameter #3 is character string of the data that will be barcoded. Barcode 3 of 9 uses the start and stop characters of ‘*’; these characters are automatically inserted as the first and last character of your supplied character string. If the supplied character string is greater than 30 characters, the barcode will not print.

Parameter #4 is a double value indicating the characters per inch of the barcode; for example, density. This is accurate when printing to a high DPI device such as a laser printer, but will not be accurate when displayed on the screen due to the relatively low DPI of the screen device.

Parameter #5 is a BOOLEAN value meaning the Horizontal (left to right) barcode is TRUE and the Vertical (top to bottom) barcode is FALSE.

BC39_Draw() will return 0 if the barcode could be fully displayed in the supplied rectangle (RECT) dimensions. If it wouldn’t fit, the rectangle is elongated to hold the barcode and the function will return a value of 1.

If there is ample room in the supplied rectangle to fully display the barcode, the barcode is horizontally centered in the rectangle for a horizontal printing barcode, and vertically centered in the rectangle for a vertical barcode.

Example usage:


#include “barcode39.h”
.
.
.

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT rt;

//define the rectangle on the dc where the barcode will
//be displayed

rt.top=60;
rt.left=20;
rt.right=500;
rt.bottom=100;

if(BC39_Draw(ps.hdc,&rt,”Test123″,10,TRUE))
{
//barcode didn’t fit in supplied rect dimensions;
//rect was sized to fit

MessageBeep(0);
}

EndPaint(hWnd, &ps);
break;

Downloads

Download source – 3 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read