⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printdirectctl.cpp

📁 直接控制端口打印的MFC控件程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	DWORD dwError ;
	COMSTAT t2;


	char *Command = new char[1024] ;
	char CommandControlCode[10];

	//* 纵向行距
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x33;
	CommandControlCode[2] = (char)LineS;//0x39;
	CommandControlCode[3] = 0x00;

	strcpy( Command, CommandControlCode) ;
	
	// 普通行(----)
	if ( Flag == 0 )
	{

		for ( int i = 0 ;i < LineLength ; i++ )
		{
		//	CommandControlCode[0] = 0x1B;
		//	CommandControlCode[1] = 'R';
		//	CommandControlCode[2] = 0x10;
		//	CommandControlCode[3] = 0x00;
				
		//	strcat( Command, CommandControlCode) ;
			
			CommandControlCode[0] = (char)0xA9; // (byte) 0xA9 ;
			CommandControlCode[1] = (char)0xA5;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;
		}

	}

	// 第一行(顶行)
	if ( Flag == 1 )
	{
		CommandControlCode[0] = (char)0xA9;
		CommandControlCode[1] = (char)0xB3;
		CommandControlCode[2] = 0x00;
		strcat( Command, CommandControlCode) ;

		for ( int i = 0 ;i < LineLength ; i++ )
		{
		//	CommandControlCode[0] = 0x1B;
		//	CommandControlCode[1] = 'R';
		//	CommandControlCode[2] = 0x10;
		//	CommandControlCode[3] = 0x00;
				
		//	strcat( Command, CommandControlCode) ;
			
			CommandControlCode[0] = (char)0xA9;
			CommandControlCode[1] = (char)0xA5;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;
		}

		CommandControlCode[0] = (char)0xA9;
		CommandControlCode[1] = (char)0xB7;
		CommandControlCode[2] = 0x00;

		strcat( Command, CommandControlCode) ;
	}

	// 第二行(中间行)
	if ( Flag == 2 )
	{
		CommandControlCode[0] = (char)0xA9;
		CommandControlCode[1] = (char)0xC4;
		CommandControlCode[2] = 0x00;
		strcat( Command, CommandControlCode) ;

		for ( int i = 0 ;i < LineLength ; i++ )
		{
		//	CommandControlCode[0] = 0x1B;
		//	CommandControlCode[1] = 'R';
		//	CommandControlCode[2] = 0x10;
		//	CommandControlCode[3] = 0x00;
				
		//	strcat( Command, CommandControlCode) ;
			
			CommandControlCode[0] = (char)0xA9;
			CommandControlCode[1] = (char)0xA4;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;
		}

		CommandControlCode[0] = (char)0xA9;
		CommandControlCode[1] = (char)0xCC;
		CommandControlCode[2] = 0x00;

		strcat( Command, CommandControlCode) ;
	}

	// 第三行(第行)
	if ( Flag == 3 )
	{
		CommandControlCode[0] = (char)0xA9;
		CommandControlCode[1] = (char)0xBB;
		CommandControlCode[2] = 0x00;
		strcat( Command, CommandControlCode) ;

		for ( int i = 0 ;i < LineLength ; i++ )
		{
		//	CommandControlCode[0] = 0x1B;
		//	CommandControlCode[1] = 'R';
		//	CommandControlCode[2] = 0x10;
		//	CommandControlCode[3] = 0x00;
				
		//	strcat( Command, CommandControlCode) ;
			
			CommandControlCode[0] = (char)0xA9;
			CommandControlCode[1] = (char)0xA5;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;
		}

		CommandControlCode[0] = (char)0xA9;
		CommandControlCode[1] = (char)0xBF;
		CommandControlCode[2] = 0x00;

		strcat( Command, CommandControlCode) ;
	}

	CommandControlCode[0] = 0x0A;
	CommandControlCode[1] = 0x0D;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;

	OVERLAPPED stOverLapped ;
	memset( &stOverLapped, 0, sizeof(OVERLAPPED));
		
	nRet = WriteFile( hHandle, Command, strlen( Command), &dwRead, &stOverLapped ) ;
	if ( nRet == 0 )
	{
		dwError = GetLastError();
		if ( dwError == ERROR_IO_PENDING ) 
			nRet = 1 ;
	}
	if( !ClearCommError( hHandle, &t1, &t2) )
	{
		switch(t1)	// return false
		{
			case CE_OOP:
			//	TRACE("G CE_OOP\n");
				return -1 ;
			break;
			case CE_PTO://超时错误
				//TRACE("G CE_PTO\n");
				return -2 ;
			break;
			case CE_IOE:
				//TRACE("G CE_IOE\n");
				return -3 ;
			break;
		}
	}
	
	//if ( dwError == ERROR_IO_PENDING && nRet  ) // ERROR_IO_PENDING = 997
	return nRet ;



}

/**********************************************************************************************/
//																							  //
//  打印一行数据																			  //
//																							  //
//  票据页长 CHR(27)+'C'+CHR(24)															  //
//  纵向行距 CHR(27)+'3'+CHR(14)														      //
//  打印质量 CHR(28)+'x1'																	  //
//  横向压缩 CHR(26)+'Q'																	  //
//																							  //
//  英寸厘米对照表																			  //
//																							  //
//  英寸	9		10.4	11.3	12.1	13.3	14		15		17		19		21		  //
//  厘米	22.86	26.4	28.7	30.73	33.02	35.56	38.01	43.18	48.26	53.34	  //
//																							  //
//  参数:PrintHandle -->  设备句柄															  //
//        szPrintData -->  打印的数据行														  //
//		  LineS		  -->  行距( 距离上一行的行距,0 :默认行距标题 )						  //
//		  Flag		  -->  标志( 0. 标题 ;													  //
//								 1. 正常数据;												  //
//								 2 .两端带| 的数据--> 如: | 票据格式   |;					  //
//								 3 .两端带| 的数据,同2但不包含回车换行;						  //
//							   > 4 横线 )													  //
//																							  //
//*********************************************************************************************/

long CPrintDirectCtrl::PrintOneLine(long PrintHandle, LPCTSTR szPrintData, short LineS, short Flag) 
{
	// TODO: Add your dispatch handler code here

	int nRet = -1 ;	
	HANDLE hHandle = NULL ;
	hHandle = (HANDLE) PrintHandle;

	if ( PrintHandle <= 0 )			// 非法的设备句柄
		return -11 ;

	DWORD t1,dwRead;
	DWORD dwError ;
	COMSTAT t2;


	char *Command = new char[1024] ;
	char CommandControlCode[10];

	//* 纵向行距
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x33;
	CommandControlCode[2] = (char)LineS;//0x39;
	CommandControlCode[3] = 0x00;

	strcpy( Command, CommandControlCode) ;

	//* 设定左边界
	
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x6C;
	CommandControlCode[2] = 0x01;
	CommandControlCode[3] = 0x00;	// 六个字符列

	strcat( Command, CommandControlCode) ;


	
	switch(Flag)
	{
		case 0:
			//* 设置汉字字体
			CommandControlCode[0] = 0x1B;
			CommandControlCode[1] = 0x75;
			CommandControlCode[2] = 0x31;
			CommandControlCode[3] = 0x00;

			strcat( Command, CommandControlCode) ;


			// * 设置字体(倍宽打印)

			CommandControlCode[0] = 0x1C;
			CommandControlCode[1] = 0x0E;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;
			
		// * 打印数据
			strcat( Command, szPrintData) ;

				
			CommandControlCode[0] = 0x0D;
			CommandControlCode[1] = 0x0A;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;


			break;
		case 1:
		
			//* 打印质量 CHR(28)+'x1'
			CommandControlCode[0] = 0x1C;
			CommandControlCode[1] = 0x78;
			CommandControlCode[2] = 0x01;
			CommandControlCode[3] = 0x00;

			strcat( Command, CommandControlCode) ;

			//* 横向压缩 CHR(26)+'Q'
			CommandControlCode[0] = 0x1A;
			CommandControlCode[1] = 0x51;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;
		
			//* 设置汉字字体
			CommandControlCode[0] = 0x1B;
			CommandControlCode[1] = 0x75;
			CommandControlCode[2] = 0x31;
			CommandControlCode[3] = 0x00;

			strcat( Command, CommandControlCode) ;

			// * 打印数据
			strcat( Command, szPrintData) ;
			break;
		case 2:

			//* 打印质量 CHR(28)+'x1'
			CommandControlCode[0] = 0x1C;
			CommandControlCode[1] = 0x78;
			CommandControlCode[2] = 0x01;
			CommandControlCode[3] = 0x00;

			strcat( Command, CommandControlCode) ;

			//* 横向压缩 CHR(26)+'Q'
			CommandControlCode[0] = 0x1A;
			CommandControlCode[1] = 0x51;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;
		
			//* 设置汉字字体
			CommandControlCode[0] = 0x1B;
			CommandControlCode[1] = 0x75;
			CommandControlCode[2] = 0x31;
			CommandControlCode[3] = 0x00;

			strcat( Command, CommandControlCode) ;

			// " | "

			CommandControlCode[0] = (char)0xA9;
			CommandControlCode[1] = (char)0xA7;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;

			// * 打印数据
			strcat( Command, szPrintData) ;

			// " | "

			CommandControlCode[0] = (char)0xA9;
			CommandControlCode[1] = (char)0xA7;
			CommandControlCode[2] = 0x00;

			strcat( Command, CommandControlCode) ;

			break;

		default:
			for ( int i = 0 ;i < Flag ; i++ )
			{		
				CommandControlCode[0] = (char)0xA9;
				CommandControlCode[1] = (char)0xA4;
				CommandControlCode[2] = 0x00;

				strcat( Command, CommandControlCode) ;
			}
			break;

	}

	CommandControlCode[0] = 0x0D;
	CommandControlCode[1] = 0x0A;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;

	OVERLAPPED stOverLapped ;
	memset( &stOverLapped, 0, sizeof(OVERLAPPED));
		
	nRet = WriteFile( hHandle, Command, strlen( Command), &dwRead, &stOverLapped ) ;
	if ( nRet == 0 )
	{
		dwError = GetLastError();
		if ( dwError == ERROR_IO_PENDING ) 
			nRet = 1 ;
	}
	if( !ClearCommError( hHandle, &t1, &t2) )
	{
		switch(t1)	// return false
		{
			case CE_OOP:
				nRet =  -3 ;
				break;
			case CE_PTO://超时错误
				nRet =  -4 ;
				break;
			case CE_IOE:
				nRet =  -5 ;
				break;
			default:
				nRet = -1 ;
				break;
		}
	}
	
	return nRet ;


}

/***************************************************************************/
//																		   //
//  打印机状态检测 														   //
//																		   //
//  参数:int 4 个字节长度 (打印设备资源句柄)							   //																   //
//																		   //	
//																		   //
//																		   //
/***************************************************************************/

long CPrintDirectCtrl::PrinterCheck(long PrintHandle) 
{
	// TODO: Add your dispatch handler code here
	// 打印机状态检测

	int  nRet = -1 ;
	char *Command = new char[10] ;
	char CommandControlCode[10];
	DWORD dwError = -1;
	DWORD dwRead  = 0 ;
	DWORD dwRet   = -1 ;
	if ( PrintHandle <= 0 )			// 非法的设备句柄
		return -11 ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -