Adding Barcode 3 of 9 to Your Applications


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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

          //barcode didn't fit in supplied rect dimensions;
          //rect was sized to fit

    EndPaint(hWnd, &ps);


Download source - 3 Kb


  • good

    Posted by bluedaisy3294 on 01/04/2012 01:13pm

    Thanks for your shaning.I've tried lots of online QR code generators and readers. These are by far my favorite tools for making and reading QR codes.I have found a free online barCode scanner online to make my own barcodes for products and it is really convenient and useful. I just want to share it with you, everybody, maybe you can try it someday http://www.keepautomation.com/

  • Adding Barcode 3 of 9 to Your Applications

    Posted by Legacy on 12/11/2003 08:00am

    Originally posted by: Alan Freeland

    Where can I find a table which includes the full codes for Code 39 character set which is listed in the function BC39_Decode.
    e.g case '1':code = 0x0121;break; what table of the BC39 set equates '1' to a value of 0X0121.


  • Bar Code Reader status

    Posted by Legacy on 08/02/2003 07:00am

    Originally posted by: somasekhar

    It is nice code to print our bar codes. Pl. help me out to find Barcode reader's reading error and device failure?

    • bar code

      Posted by jamcode on 04/04/2005 04:47pm

      Hi,help me with bar code from visual basic 6

  • We need a component

    Posted by Legacy on 06/05/2003 07:00am

    Originally posted by: Amour Rashid Hamad

    I am very interested in this code but I am unlucky I am not good at C++ so I cant use this code with C++.
    However I think you can help us by converting this code into dll.

    Please could you help us.
    with thanks.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date