條碼(code 128 模式C ) 列印 MFC

來源:互聯網
上載者:User

 

 

標頭檔 Code128C.h

/***************************************************************
* NAME             : Code128C.h
* FUNCTION         : 列印code128 模式C 條碼 
* PROGRAMMED       : Ming_zhang
* DATE(ORG)        : August 24 2011
* PROJECT          :
* OS               : Windows XP
* HISTORY          :
* ID --- DATE -----------NOTE-------------------------------
* 00 
***************************************************************/

#ifndef _CODE128C_H_
#define _CODE128C_H_

/***********************Global Variable Declare***************/
//#define    -1;                                       
//////////////////////////////////////////////////////////////

/************************INCLUDE FILES*************************/
//#pragma warning (disable : 4786)
//#include <iostream>
//#include <string>
//#include <map>
//using namespace std;
///////////////////////////////////////////////////////////////

/***************************************************************  
* NAME             : Code128C                         
* BASE CLASS NAME  :                                    
* FUNCTION         : 列印 code128 模式C 條碼                                  
* Interface        :
***************************************************************/

class Code128C
{
public:

// Constructors & Destructor
 Code128C();
 virtual ~Code128C();
// Interface
int PrintCodeBar(const CString &sNum,const RECT &Rect,CDC &dc );

// Attributes
private:
// Operations
 int GetPrintCode(const CString& sNum,CString &sPrintCode);
 int PrintCode(const CString &sNum,const CString &sPrintCode,const RECT &Rect,CDC &dc  );
// Attributes
 int m_nHead;
 int m_nTail;
 char m_code128_c[128][8];
};

#endif;

 

//實現檔案 Code128C.cpp

 

/***************************************************************
* NAME         : Code128C.cpp
* FUNCTION     :
* PROGRAMMED   : Ming_zhang
* DATE(ORG)    : August 24 2011
* PROJECT      :
* OS           : Windows XP
* HISTORY      :
* ID --- DATE -----------NOTE-------------------------------
* 00 
***************************************************************/

/************************INCLUDE FILES*************************/
#include "stdafx.h"
#include "Code128C.h"
//#include <string>
//using namespace std;
///////////////////////////////////////////////////////////////

Code128C::Code128C()
{
 m_nHead = 105;
 m_nTail = 106;
 /*初始化編碼錶 code128 c */
 strcpy(m_code128_c[0],"212222");  
 strcpy(m_code128_c[1],"222122");  
 strcpy(m_code128_c[2],"222221");  
 strcpy(m_code128_c[3],"121223");  
 strcpy(m_code128_c[4],"121322");  
 strcpy(m_code128_c[5],"131222");  
 strcpy(m_code128_c[6],"122213");  
 strcpy(m_code128_c[7],"122312");  
 strcpy(m_code128_c[8],"132212");  
 strcpy(m_code128_c[9],"221213");  
 strcpy(m_code128_c[10],"221312"); 
 strcpy(m_code128_c[11],"231212"); 
 strcpy(m_code128_c[12],"112232"); 
 strcpy(m_code128_c[13],"122132"); 
 strcpy(m_code128_c[14],"122231"); 
 strcpy(m_code128_c[15],"113222"); 
 strcpy(m_code128_c[16],"123122"); 
 strcpy(m_code128_c[17],"123221"); 
 strcpy(m_code128_c[18],"223211"); 
 strcpy(m_code128_c[19],"221132"); 
 strcpy(m_code128_c[20],"221231"); 
 strcpy(m_code128_c[21],"213212"); 
 strcpy(m_code128_c[22],"223112"); 
 strcpy(m_code128_c[23],"312131"); 
 strcpy(m_code128_c[24],"311222"); 
 strcpy(m_code128_c[25],"321122"); 
 strcpy(m_code128_c[26],"321221"); 
 strcpy(m_code128_c[27],"312212"); 
 strcpy(m_code128_c[28],"322112"); 
 strcpy(m_code128_c[29],"322211"); 
 strcpy(m_code128_c[30],"212123"); 
 strcpy(m_code128_c[31],"212321"); 
 strcpy(m_code128_c[32],"232121"); 
 strcpy(m_code128_c[33],"111323"); 
 strcpy(m_code128_c[34],"131123"); 
 strcpy(m_code128_c[35],"131321"); 
 strcpy(m_code128_c[36],"112313"); 
 strcpy(m_code128_c[37],"132113"); 
 strcpy(m_code128_c[38],"132311"); 
 strcpy(m_code128_c[39],"211313"); 
 strcpy(m_code128_c[40],"231113"); 
 strcpy(m_code128_c[41],"231311"); 
 strcpy(m_code128_c[42],"112133"); 
 strcpy(m_code128_c[43],"112331"); 
 strcpy(m_code128_c[44],"132131"); 
 strcpy(m_code128_c[45],"113123"); 
 strcpy(m_code128_c[46],"113321"); 
 strcpy(m_code128_c[47],"133121"); 
 strcpy(m_code128_c[48],"313121"); 
 strcpy(m_code128_c[49],"211331"); 
 strcpy(m_code128_c[50],"231131"); 
 strcpy(m_code128_c[51],"213113"); 
 strcpy(m_code128_c[52],"213311"); 
 strcpy(m_code128_c[53],"213131"); 
 strcpy(m_code128_c[54],"311123"); 
 strcpy(m_code128_c[55],"311321"); 
 strcpy(m_code128_c[56],"331121"); 
 strcpy(m_code128_c[57],"312113"); 
 strcpy(m_code128_c[58],"312311"); 
 strcpy(m_code128_c[59],"332111"); 
 strcpy(m_code128_c[60],"314111"); 
 strcpy(m_code128_c[61],"221411"); 
 strcpy(m_code128_c[62],"431111"); 
 strcpy(m_code128_c[63],"111224"); 
 strcpy(m_code128_c[64],"111422"); 
 strcpy(m_code128_c[65],"121124"); 
 strcpy(m_code128_c[66],"121421"); 
 strcpy(m_code128_c[67],"141122"); 
 strcpy(m_code128_c[68],"141221"); 
 strcpy(m_code128_c[69],"112214"); 
 strcpy(m_code128_c[70],"112412"); 
 strcpy(m_code128_c[71],"122114"); 
 strcpy(m_code128_c[72],"122411"); 
 strcpy(m_code128_c[73],"142112"); 
 strcpy(m_code128_c[74],"142211"); 
 strcpy(m_code128_c[75],"241211"); 
 strcpy(m_code128_c[76],"221114"); 
 strcpy(m_code128_c[77],"413111"); 
 strcpy(m_code128_c[78],"241112"); 
 strcpy(m_code128_c[79],"134111"); 
 strcpy(m_code128_c[80],"111242"); 
 strcpy(m_code128_c[81],"121142"); 
 strcpy(m_code128_c[82],"121241"); 
 strcpy(m_code128_c[83],"114212"); 
 strcpy(m_code128_c[84],"124112"); 
 strcpy(m_code128_c[85],"124211"); 
 strcpy(m_code128_c[86],"411212"); 
 strcpy(m_code128_c[87],"421112"); 
 strcpy(m_code128_c[88],"421211"); 
 strcpy(m_code128_c[89],"212141"); 
 strcpy(m_code128_c[90],"214121"); 
 strcpy(m_code128_c[91],"412121"); 
 strcpy(m_code128_c[92],"111143"); 
 strcpy(m_code128_c[93],"111341"); 
 strcpy(m_code128_c[94],"131141"); 
 strcpy(m_code128_c[95],"114113"); 
 strcpy(m_code128_c[96],"114311"); 
 strcpy(m_code128_c[97],"411113"); 
 strcpy(m_code128_c[98],"411311"); 
 strcpy(m_code128_c[99],"113141"); 
 strcpy(m_code128_c[100],"114131");
 strcpy(m_code128_c[101],"311141");
 strcpy(m_code128_c[102],"411131");
 strcpy(m_code128_c[103],"211412");
 strcpy(m_code128_c[104],"211214");
 strcpy(m_code128_c[105],"211232");
 strcpy(m_code128_c[106],"2331112");
}

Code128C::~Code128C()
{
}

/********************************************************************
* NAME         : PrintCodeBar
* FUNCTION     : 對外介面 列印條碼
* PARAMETER    :
* RETURN       :
* PROGRAMMER   : Ming zhang
* DATE(ORG)    : August 24 2011
* UPDATE       :
* MEMO         :
********************************************************************/
int Code128C::PrintCodeBar(const CString &sNum,const RECT &Rect,CDC &dc )
{
 CString sPrintCode;
 GetPrintCode(sNum,sPrintCode);
 PrintCode(sNum,sPrintCode,Rect,dc);
 
 return 1;
}

/********************************************************************
* NAME         : GetPrintCode
* FUNCTION     : 根據傳進來的資料取得的列印編碼
* PARAMETER    :
* RETURN       :
* PROGRAMMER   : Ming zhang
* DATE(ORG)    : August 24 2011
* UPDATE       :
* MEMO         :
********************************************************************/
int Code128C::GetPrintCode(const CString &sNum, CString &sPrintCode)
{
/*頭*/
 sPrintCode = m_code128_c[m_nHead];
/*資料*/ 
 int nLength = sNum.GetLength();
 int nV;
 int j =1;
 int nVeryfy = m_nHead;
 for(int i  = 0;i<nLength;i=i+2)
 {
  nV = atoi(sNum.Mid(i,2));
  sPrintCode = sPrintCode+m_code128_c[nV];
  nVeryfy = nVeryfy+j*nV;
  ++j;
 }

/*計算檢驗碼*/
 nVeryfy = nVeryfy%103;
 sPrintCode = sPrintCode+m_code128_c[nVeryfy];
/*尾*/
 sPrintCode = sPrintCode +m_code128_c[m_nTail];
 return 1;

/********************************************************************
* NAME         : PrintCode
* FUNCTION     : 列印條碼
* PARAMETER    :
* RETURN       :
* PROGRAMMER   : Ming zhang
* DATE(ORG)    : August 24 2011
* UPDATE       :
* MEMO         :
********************************************************************/
int Code128C::PrintCode(const CString &sNum,const CString &sPrintCode,const RECT &Rect,CDC &dc )
{                                                           
 int i ;                                                 
 int nLen = sPrintCode.GetLength();                            
 int nTotalBar = 0;                                        
 for(i=0;i<nLen;++i)                                       
  nTotalBar = nTotalBar+(sPrintCode[i]-'0');                  
                                                           
 int nBarWidth = (Rect.right-Rect.left)/nTotalBar;         
 int nBarHeight = (Rect.bottom-Rect.top); 
 /*字型 nBarHeight = 1/4圖形高度*/
 int nFontHeight =   nBarHeight/2;
 nBarHeight = nBarHeight - nFontHeight;

                                                           
 int nEachPrintBarNum;                                     
 int nValueLength = sPrintCode.GetLength();                    
 int nBegPos = Rect.left; /*計算列印的開始位位置 單位像素*/
 int nEndPos;                                              
 RECT rt;                                                  
                                                            
 CBrush brush_black(RGB(0,0,0));                           
 CBrush brush_white(RGB(255,255,255));                     
 for( i = 0;i<nValueLength;++i)                            
 {                                                         
  nEachPrintBarNum = sPrintCode[i]-'0';                       
/*計算列印*/                                                
  nEndPos =  nBegPos+nEachPrintBarNum*nBarWidth;          
  rt.left = nBegPos;                                      
  rt.right = nEndPos;                                     
  rt.top = Rect.top;                                             
  rt.bottom = Rect.top+nBarHeight;                                 
                                                            
  if(i%2==0)                                              
  {/*打黑色的*/                                           
   dc.FillRect(&rt,&brush_black);                        
  }                                                       
  else                                                    
  {                                                       
   dc.FillRect(&rt,&brush_white);                        
  }                                                       
  nBegPos =nEndPos;/*下一個起點*/                         
 }
 rt.top = Rect.bottom-nFontHeight;
 rt.bottom = Rect.bottom;
 rt.left = Rect.left;
 rt.right = Rect.left +nBarWidth*nTotalBar;

 dc.DrawText(sNum,&rt,DT_CENTER|DT_SINGLELINE|DT_NOPREFIX);
                                                            
 return 1;                                                 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.