TBarCodeDLL-Board the Barcode Bandwagon, Page 2
Your First Barcode App
The demo program is nice, but let's see some source code and how it really works. Breaking it down to the simplest level, we will look at Tec-It's simplest provided C++ application. I'm a command-line kind of guy, so I used the following to compile and link in one step:
cl TBCDLLSample.cpp /Z7 /link /DEBUG TBarCode6.lib gdi32.lib user32.lib
Then, when I ran the app, the first thing it complained about was not finding TBarCode6.OCX. I located a copy of the OCX in the redist directory and punched in the OCX as follows:
regsvr32 Tbarcode6.ocx
This program is the smallest useful sample you can write. It performs the following tasks:
- Creates a Code128 barcode
- Draws the barcode onto a bitmap
- Burns the bitmap into a .JPG file
The following section goes through several areas of the program and makes some notes on what is happening in each:
1 // TBCDLLSample.cpp: Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5 #include <Windows.h>
6 #include <WTypes.h>
7
8 // include the header files for TBARCODE DLL
9 #define TECIT_DLLIMPORT
10 #include "TBarCodeDLL.h"
11 #undef TECIT_DLLIMPORT
12
13
14 /// include the tbarcode5.lib file to the linker section
15
16 //////////////////////////////////////////////////////////////////
17 // TBarCode DLL Sample
18 //////////////////////////////////////////////////////////////////
19 int main(int argc, char* argv[]) {
20 ERRCODE eCode = S_OK;
21 LOGFONTA* lf = NULL; // used to change font of barcode
22 t_BarCode* pBC;
23 char* szText;
24 INT nModules;
25
26 printf("TBarCode DLL Sample\n");
27 printf("-------------------\n\n");
28 printf("This program will create a sample barcode and save it as \"c:\\bitmap.jpg\".\n");
29 printf("Barcode type: Code128\n");
30 printf("Encoded data: HELLO WORLD\n\n");
31
32 // License at startup of your application; get your license key from http://www.tec-it.com/order/
33 // BCLicenseMe( "Licensee String", eKindOfLicense, 1, "Your Key", eProductID); // for enums refer to TECBCEnum.h
34
35 // allocate memory for internal barcode structure
36 BCAlloc (&pBC);
37
38 // set the barcode type to Code 128
39 // refer to TECBCEnum.h (or the documentation)
40 eCode = BCSetBCType (pBC, eBC_Code128);
41
42 if (eCode == S_OK) {
43 // set the barcode data to "HELLO WORLD"
44 szText = "HELLO WORLD";
45 eCode = BCSetText(pBC, szText, strlen(szText));
46
47 // change the font
48 lf = BCGetLogFont(pBC);
49 lf->lfHeight = 10;
50 strcpy(lf->lfFaceName, "Arial"); // face name must not exceed 31 characters
51
52 // Use this commands if you want to change the module width:
53 // BCSetModWidth (pBC, "254"); // Set module width to 0.254 mms
54 }
55
56 // check if the input data contains valid characters (for the selected barcode type)
57 if (eCode == S_OK)
58 eCode = BCCheck(pBC);
59
60 // compute the check digits (depends on barcode and CD-method)
61 if (eCode == S_OK)
62 eCode = BCCalcCD(pBC);
63
64 // prepare the internal barcode info-structure to be drawn
65 if (eCode == S_OK)
66 eCode = BCCreate(pBC);
67
68 // get number of horizontal barcode elements
69 if (eCode == S_OK)
70 nModules = (INT) BCGetCountModules (pBC);
71
72 // draw barcode to device context
73 if (eCode == S_OK) {
74 RECT rect;
75 HDC dc;
76 HBITMAP BM;
77
78 dc = CreateCompatibleDC(NULL);
79 BM = CreateCompatibleBitmap (dc, nModules * 2, 150); // area of Pixels
80 SelectObject ( dc, BM );
81
82 rect.left = 0;
83 rect.bottom = 0;
84 rect.right = 5000;
85 rect.top = 3000;
86
87 SetMapMode(dc, MM_HIMETRIC);
88 OffsetRect(&rect,100, 100);
89
90 eCode = BCDraw(pBC, dc, &rect); // draw barcode into device context
91 DeleteObject (BM);
92 DeleteDC(dc);
93 }
94
95 // save barcode to image file
96 if (eCode == S_OK) {
97 // use number of horizontal elements as for the x dimension of the image
98 // this avoids aliasing effects and ensures readability of barcode!
99 eCode = BCSaveImage(pBC, "c:\\bitmap.jpg", eIMJpg, nModules, 150, 96, 96);
100 }
101
102 // show error message on demand
103 if (eCode != S_OK) {
104 // if an error occurred, display more info about the error
105 char szErr[32] = {0};
106 //ZeroMemory(szText, sizeof(szText));
107 BCGetErrorText(eCode, szErr, sizeof(szErr));
108 MessageBox(NULL, szErr, "Error creating barcode:", MB_OK);
109 }
110
111 // Important: frees the allocated memory of the barcode
112 BCFree(pBC);
113
114 printf("done.\n");
115 return eCode;
116 }
Boost to Human Productivity
TBarCode DLL and OCX solve a complex problem with a simple interface. If you need to print reports or stickers that can be scanned in the field to verify the presence of a part or piece of inventory, the lowly barcode can be a big boost to human productivity and cut down on data-entry errors and operator fatigue.About the Author
Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.
