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


Comments

  • http://www.keepautomation.com/

    Posted by bluedaisy3294 on 01/04/2012 05:15am

    ***** here

    Reply
  • good

    Posted by bluedaisy3294 on 01/04/2012 05:13am

    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/

    Reply
  • Adding Barcode 3 of 9 to Your Applications

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

    Originally posted by: Alan Freeland

    David,
    
    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.

    Thanks.

    Reply
  • Bar Code Reader status

    Posted by Legacy on 08/02/2003 12: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 09:47am

      Hi,help me with bar code from visual basic 6

      Reply
    Reply
  • We need a component

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

    Originally posted by: Amour Rashid Hamad

    Hello;
    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.

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

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds