www.pudn.com > Barcode_src.zip > Code128.cpp


// Code128.cpp: implementation of the CCode128 class. 
// 
//	Copyright 2002 Neil Van Eps 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Code128.h" 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CCode128::CCode128() 
{ 
	// default to subset a 
	m_nCurrentSubset = SUBSETA; 
 
	// code 128 
	m_nSymbology = COD128; 
} 
 
CCode128::~CCode128() 
{ 
 
} 
 
//////////////////////////////////////////////////////////////////////////////////// 
// 
//	Name: 
//		DrawBitmap() 
// 
//	Description: 
//		draws a barcode using the previously loaded data 
// 
//	Arguments: 
//		none 
// 
//	Return: 
//		void 
// 
//	Called by: 
//		public class interface 
// 
//////////////////////////////////////////////////////////////////////////////////// 
 
void CCode128::DrawBitmap() 
{ 
	long	nChar,nNextChar,nCharacterPosition,nCheckDigit; 
 
	// calculate the check digit 
	nCheckDigit = GetCheckDigit(); 
	 
	// draw start character for current subset 
	if (m_nCurrentSubset==SUBSETA) 
		DrawPattern(RetrievePattern(103)); 
	else if (m_nCurrentSubset==SUBSETB) 
		DrawPattern(RetrievePattern(104)); 
	else if (m_nCurrentSubset==SUBSETC) 
		DrawPattern(RetrievePattern(105)); 
	 
	// initialize position in message	 
	nCharacterPosition = 0; 
 
	while (nCharacterPosition < m_csMessage.GetLength()) 
	{ 
		if (m_nCurrentSubset==SUBSETC) 
		{ 
			// if it's a switch to subsetA - same character (103) for all subsets 
			if (g_nASCIItoCode128SubsetAB[SUBSETA][m_csMessage.GetAt(nCharacterPosition)]==101) 
			{ 
				// draw the startA code 
				DrawPattern(RetrievePattern(101)); 
 
				// we've moved one message character 
				nCharacterPosition++; 
 
				// actually change the subset 
				m_nCurrentSubset = SUBSETA; 
			} 
			// if it's a switch to subsetB - same character (104) for all subsets 
			else if (g_nASCIItoCode128SubsetAB[SUBSETA][m_csMessage.GetAt(nCharacterPosition)]==100) 
			{ 
				// draw the startB code 
				DrawPattern(RetrievePattern(100)); 
 
				// we've moved one message character 
				nCharacterPosition++; 
 
				// actually change the subset 
				m_nCurrentSubset = SUBSETB; 
			} 
			// it's FNC1 - just print it out 
			else if (g_nASCIItoCode128SubsetAB[SUBSETA][m_csMessage.GetAt(nCharacterPosition)]==102) 
			{ 
				// draw the FNC1 
				DrawPattern(RetrievePattern(100)); 
 
				// we've moved one message character 
				nCharacterPosition++; 
			} 
			// it's a digit - pull two at a time 
			else 
			{ 
				CString		csTemp; 
 
				// get the next two characters 
				csTemp = m_csMessage.Mid(nCharacterPosition,2); 
 
				// convert them to longs 
				nChar = atol((const char *)csTemp); 
 
				// draw the code 128 character 
				DrawPattern(RetrievePattern(nChar)); 
 
				// we've moved two message characters 
				nCharacterPosition += 2; 
			} 
		} 
		// we're in SUBSETA or SUBSETB 
		else 
		{ 
			// handle upper ASCII characters if necessary 
			long nTemp2 = m_csMessage.GetAt(nCharacterPosition); 
			if (nTemp2<-1) 
				nTemp2 = nTemp2&255; 
			 
			// retrieve the message character 
			nChar = g_nASCIItoCode128SubsetAB[m_nCurrentSubset][nTemp2]; 
 
			// draw the char 
			DrawPattern(RetrievePattern(nChar)); 
 
			// we've moved one character position 
			nCharacterPosition++; 
 
			// if switch in SUBSETA 
			if (m_nCurrentSubset==SUBSETA) 
			{ 
				if (nChar==100) 
					m_nCurrentSubset = SUBSETB; 
				else if (nChar==99) 
					m_nCurrentSubset = SUBSETC; 
			} 
			// if switch in SUBSETB 
			else if (m_nCurrentSubset==SUBSETB) 
			{ 
				if (nChar==101) 
					m_nCurrentSubset = SUBSETA; 
				else if (nChar==99) 
					m_nCurrentSubset = SUBSETC; 
			} 
			// if a shift character 
			else if (nChar==98) 
			{ 
				// shift subsets for the next character only 
				if (m_nCurrentSubset==SUBSETA) 
					nNextChar = g_nASCIItoCode128SubsetAB[SUBSETB][m_csMessage.GetAt(nCharacterPosition)]; 
				else 
					nNextChar = g_nASCIItoCode128SubsetAB[SUBSETA][m_csMessage.GetAt(nCharacterPosition)]; 
 
				// draw the shifted character 
				DrawPattern(RetrievePattern(nChar)); 
 
				// since we've handled two characters advance character position again 
				nCharacterPosition++; 
			} 
		} 
	} 
 
	// draw check digit 
	DrawPattern(RetrievePattern(nCheckDigit)); 
	 
	// draw stop character 
	DrawPattern(RetrievePattern(106)); 
 
	return; 
} 
 
//////////////////////////////////////////////////////////////////////////////////// 
// 
//	Name: 
//		BitmapToClipboard() 
// 
//	Description: 
//		puts the specified bitmap on the clipboard 
// 
//	Arguments: 
//		none 
// 
//	Return: 
//		void 
// 
//	Called by: 
//		public class interface - called by users of the class 
// 
//////////////////////////////////////////////////////////////////////////////////// 
 
void CCode128::BitmapToClipboard() 
{ 
	CDC					memDC; 
	CBitmap				oBitmap; 
 
	memDC.CreateCompatibleDC(NULL); 
 
	m_hDC = memDC.GetSafeHdc(); 
 
	// create compatible, correctly sized bitmap 
	oBitmap.CreateCompatibleBitmap(&memDC,m_nFinalBarcodePixelWidth,m_nPixelHeight); 
 
	// select our bitmap into the device context 
	CBitmap * oldbm = memDC.SelectObject(&oBitmap); 
 
	// turn area white - stock black bitmap is selected 
	memDC.BitBlt(0,0,m_nFinalBarcodePixelWidth,m_nPixelHeight,NULL,0,0,WHITENESS); 
 
	// draw bitmap into memory device context 
	DrawBitmap(); 
	 
	// put bitmap on clipboard 
	::OpenClipboard(NULL); 
	::EmptyClipboard(); 
	::SetClipboardData(CF_BITMAP, oBitmap.m_hObject); 
	::CloseClipboard(); 
		 
	//	deselect object out of device context 
	memDC.SelectObject(oldbm); 
 
	// make sure bitmap not deleted with CBitmap object		 
	oBitmap.Detach(); 
 
	return; 
} 
 
//////////////////////////////////////////////////////////////////////////////////// 
// 
//	Name: 
//		DrawPattern() 
// 
//	Description: 
//		draws the passed character pattern at the end of the barcode 
// 
//	Arguments: 
//		CString		csPattern	-	the bar pattern to draw 
// 
//	Return: 
//		void 
// 
//	Called by: 
//		CCode128::DrawBitmap() 
// 
//////////////////////////////////////////////////////////////////////////////////// 
 
void CCode128::DrawPattern(CString csPattern) 
{ 
	int			i,nXPixel,nYPixel; 
	CDC			oDC; 
 
	// attach to the device context 
	oDC.Attach(m_hDC); 
 
	// initialize X pixel value 
	nXPixel = m_nStartingXPixel; 
	 
	for (i=0;i