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);
    dlg.GetDefaults();
    CDC dc;
    CDC* pDC = dc.FromHandle(dlg.m_pd.hDC);

    pDC->StartDocW(L"LABEL");
    pDC->StartPage();
    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
    fntText.CreatePointFont(500,_T("Arial"));

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

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

    pDC->EndPage();
    pDC->EndDoc();

    pDC->SelectObject(oldFont);
}

// usage:
PrintLabel(100,100,L"CODEFOUND");


// output of the above call:












2 comments: