Create Your First Project
This step-by-step C++ tutorial will guide you to build a complete application that analyzes a 1D barcode from an image and output the value and type the terminal.
- Launch VC++ (below project has been tested in VS2013).
- Create a new CLR Empty project and name it BXTutorial.
- Add Windows Form to this project.
- Right-click on the project and select Add > New Item.
- Select UI > Windows Form, its default name is MyForm.
- Set application entry point.
- Add below code to MyForm.cpp:
C++ |
Copy Code
|
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
BXTutorial::MyForm form;
Application::Run(%form);
}
|
- Right-click your project in the Solution Explorer and click Properties.
- Under Configuration Properties > Linker > Advanced, change Entry Point to "main" (without quotation marks).
- Under Configuration Properties > Linker > System, change SubSystem to Windows (/SUBSYSTEM/WINDOWS).
- Add ActiveX component to this project.
- Open the design view, right-click on the Toolbox panel and select Choose Items…
- On the dialog, go to the tab COM Components.
- Add Accusoft BarcodeXpress 13 and Accusoft ImagXpress 13 to your toolbox
- Drag the two controls to MyForm.
- Drag OpenFileDialog from Toolbox > Dialogs to MyForm.
- Select the component ImageXpress and set the property CtlAutoSize to ISIZE_BestFit.
- Add menu to MyForm
- From Toolbox > Menu&Toolbars, select MenuStrip and add to MyForm.
- Add a menu, "File", and sub menu, "Open", named mnuOpen.
- Add an additional sub menu, "Exit", named mnuExit.
- Double-click on Open and add the code below:
C++ |
Copy Code
|
private: System::Void mnuOpen_Click(System::Object^ sender, System::EventArgs^ e) {
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
axImagXpress1->FileName = openFileDialog1->FileName;
}
}
|
- From the design window, double-click on Exit and add the code below:
C++ |
Copy Code
|
private: System::Void mnuExit_Click(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
}
|
- Add button to MyForm.
- Add one button from Toolbox, set the name to btnReadBarcode and set Text to "Read 1D Barcode".
- Double-click on that button and add below code:
C++ |
Copy Code
|
private: System::Void btnReadBarcode_Click(System::Object^ sender, System::EventArgs^ e) {
// Set barcode type to BC_StyleUnknown value - Barcode Xpress engine will search for all 1D barcodes.
axBarcodeXpress1->SetBarcodeReaderType(BC_StyleUnknown);
// Set the read region of interest to all zero
// so the entire images is searched.
axBarcodeXpress1->ReaderAreaHeight = 0;
axBarcodeXpress1->ReaderAreaWidth = 0;
axBarcodeXpress1->ReaderAreaX = 0;
axBarcodeXpress1->ReaderAreaY = 0;
if (axImagXpress1->hDIB == 0)
{
MessageBox::Show("Error: You must select an image!");
return;
}
axBarcodeXpress1->AnalyzehDib(axImagXpress1->hDIB);
int numBC = axBarcodeXpress1->NumBarcodes;
String^ bcName;
String^ bcResult;
String^ result;
for (int i = 0; i < numBC; i++)
{
axBarcodeXpress1->GetBarcode(i);
bcName = axBarcodeXpress1->BarcodeCodeName;
bcResult = axBarcodeXpress1->BarcodeResult;
result += "#" + i.ToString() + " Type: " + bcName + " Value: " + bcResult + "\n";
}
MessageBox::Show(result);
}
|
- Registration.
- To register Barcode Xpress and ImagXpress, add the following code to MyForm.h:
C++ |
Copy Code
|
MyForm(void)
{
InitializeComponent();
// The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey method must be called to distribute the runtime. Note that the SolutionName, SolutionKey and OEMLicenseKey values shown below are only examples.
//axBarcodeXpress1->SetSolutionName("YourSolutionName");
//axBarcodeXpress1->SetSolutionKey(12345, 12345, 12345, 12345);
//axBarcodeXpress1->SetOEMLicenseKey("1.0.AStringForOEMLicensingContactAccusoftSalesForMoreInformation...");
//axImagXpress1->SetSolutionName("YourSolutionName");
//axImagXpress1->SetSolutionKey(12345, 12345, 12345, 12345);
//axImagXpress1->SetOEMLicenseKey("1.0.AStringForOEMLicensingContactAccusoftSalesForMoreInformation...");
}
|
- Run the project.
- Select File > Open and choose one image with a 1D barcode on it.
- Click the Read 1D Barcode button.
- A dialog with the barcode result displays.