Monday, October 25, 2010

Barcode Printing With VC++ - MFC

Description:Printing Barcode Labels is as easy as printing normal text. Look at the following code.
"Free 3 of 9" font is used to print the bar code of a given text. This font can be downloaded as free from internet.

Importantly, a "*" character has to be added at the begining and end of the text for which you want to print the barcode (see the corresponding line of code). This is required for bar code scanner to identify start and end characters of the text while scanning.
Following code is compiled in VC++ - MFC environment. This can also be done similarly in other environments like C#, VB, etc. using print fucntions.

void PrintLabel(int nX, int nY, CString strLable)
    CPrintDialog dlg(FALSE);
    CDC dc;
    CDC* pDC = dc.FromHandle(dlg.m_pd.hDC);

    CFont fntBarCode,fntText;

    // create font for barcode, use the size as you require
    fntBarCode.CreatePointFont(800,_T("Free 3 of 9"));

    // create font for text, use the size as you require

    CFont* oldFont = pDC->SelectObject(&fntBarCode);
    pDC->TextOutW(nX,nY,_T("*") + strLable + _T("*"));

    pDC->TextOutW(nX,nY + tm.tmExternalLeading + tm.tmHeight + 10,strLable);



// usage:

// output of the above call: