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

📄 printdirectctl.cpp

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


	// 合计数据
	strcpy( LineData, "┃     合计     ┃") ;
	for ( j =0 ; j < Cols[Lines - 3 ] ; j ++ )
	{
		if ( j == 7 || j == 9 )
		{
			strcat( LineData, CPrintDirectCtrl::szSpaceData(22 - strlen(szPrintData[Lines - 3][j]) )) ;
		}
		else
		{
			strcat( LineData, CPrintDirectCtrl::szSpaceData(10 - strlen(szPrintData[Lines - 3][j]) )) ;
		}
	
		strcat( LineData, szPrintData[Lines - 3][j]) ;
		strcat( LineData, "┃") ;
	}
	CPrintDirectCtrl::PrintOneLine( PrintHandle, LineData, 26, 1) ;

	strcpy( LineData, "┣━━━━━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━━━━━━╋━━━━━╋━━━━━━━━━━━╋━━━━━┫") ;
	CPrintDirectCtrl::PrintOneLine( PrintHandle, LineData, 26, 1) ;


	strcpy( LineData, "┃   16市合计   ┃") ;
	for ( j =0 ; j < Cols[Lines - 2] ; j ++ )
	{
		if ( j == 7 || j == 9 )
		{
			strcat( LineData, CPrintDirectCtrl::szSpaceData(22 - strlen(szPrintData[Lines - 2][j]) )) ;
		}
		else
		{
			strcat( LineData, CPrintDirectCtrl::szSpaceData(10 - strlen(szPrintData[Lines - 2][j]) )) ;
		}
	
		strcat( LineData, szPrintData[Lines - 2][j]) ;
		strcat( LineData, "┃") ;
	}
	CPrintDirectCtrl::PrintOneLine( PrintHandle, LineData, 26, 1) ;

	strcpy( LineData, "┗━━━━━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━┻━━━━━┛") ;
	CPrintDirectCtrl::PrintOneLine( PrintHandle, LineData, 26, 1) ;
*/
	return 0;

}

/***************************************************************************/
//																		   //
//  打印  "养路费免费证"	B/S									   //
//																		   //
//  参数值:int 4 个字节长度 (打印设备资源句柄)	PrintHandle				   //																   //
//																		   //
//			字符串, 打印数据。列分割符号 "@",行分隔符 "*"	PrintData  //	 			   //
//																		   //	
//	返回值:int 4 个字节长度 (打印信息返回码)							   //
//																		   //
//			= 0 ---  错误 ;												   //
//			> 0 ---  正确												   //					   //
//																		   //
/***************************************************************************/

long CPrintDirectCtrl::PrintMFZ(LPCTSTR PrintData) 
{
	// TODO: Add your dispatch handler code here
	int nRet = -1 ;	
	//char szDataBuffer[1024] ;


	//long lgPrintHandle ;
	// 关闭打印句柄

	if ( CPrintDirectCtrl::dwGlobeHandle > 0 )
	{
		//theApp.lgPrintHandle = 67 ;
		
		//CPrintDirectCtrl::DisConnectToPrinter( lgPrintHandle ) ;
		CPrintDirectCtrl::DisConnectToPrinter( dwGlobeHandle) ;
	}
	DWORD dwRet = -1 ;
	long  dwHandle = 0 ;
	LPCTSTR FileName = "LPT1" ;

	dwHandle = CPrintDirectCtrl::ConnectToPrint( FileName) ;
	
	//AfxMessageBox( ltoa( dwHandle, szDataBuffer, 10 )) ;
	if ( dwHandle <= 0 )
	{
		// 返回打印连接错误!!
		return dwHandle ;
	}
	else
	{
		// 设置全局句柄
		//lgPrintHandle = dwHandle;
		CPrintDirectCtrl::dwGlobeHandle = dwHandle ;
	}


/////////////////////////////////////////////////////////////////////////////
	// 设置页长
	// nRet = CPrintDirectCtrl::PageSet( dwHandle, 5, 0);

//////////////////////////////////////////////////////////////////////////////
// 开始打印数据   szPrintData[0][0]
	nRet = CPrintDirectCtrl::PrintYLFMFZ( dwHandle,  PrintData) ;
	

//	nRet = CPrintDirectCtrl::PaperFore( dwHandle, 20) ;
// nRet = CPrintDirectCtrl::PageLF( dwHandle) ;
	return dwHandle;
}

/////////////////////////////////////////////////////////////////////////
long CPrintDirectCtrl::PrintYLFMFZ(long PrintHandle, LPCTSTR PrintData) 
{
	// TODO: Add your dispatch handler code here

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

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

	DWORD t1,dwRead;
	DWORD dwError ;
	COMSTAT t2;

	// 结构和数组赋值
	nRet = CPrintDirectCtrl::SetStructData( PrintData, 1) ;

	if ( nRet < 0  )
		return  nRet ;



	char *Command = new char[1024*10] ;
	char CommandControlCode[10];
	
	//* 纵向行距
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x33;
	CommandControlCode[2] = 0x12;//0x40;
	CommandControlCode[3] = 0x00;

	strcpy( Command, CommandControlCode) ;
	
	//* 票据页长(设定页长为 n 行)
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x43;
	CommandControlCode[2] = 0x25;
	CommandControlCode[3] = 0x00;

	strcat( Command, CommandControlCode) ;

	//* 纵向行距
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x33;
	CommandControlCode[2] = 0x45;
	CommandControlCode[3] = 0x00;

	strcat( Command, CommandControlCode) ;

	//* 打印质量 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) ;


	// 设置字符点阵 24X24

	CommandControlCode[0] = 0x1C;
	CommandControlCode[1] = 0x65;
	CommandControlCode[2] = 24;
	CommandControlCode[3] = 24;
	CommandControlCode[4] = 0x00;	

	strcat( Command, CommandControlCode) ;

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

	strcat( Command, CommandControlCode) ;
*/
	//* 设定左边界
	
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x6C;
	CommandControlCode[2] = 0x06;
	CommandControlCode[3] = 0x00;	// 六个字符列

	strcat( Command, CommandControlCode) ;


/*	//* 打印机前走纸	(一次设置 127 个 1/180 英寸单位)
	
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x4A;
	CommandControlCode[2] = (char)0x8F;
	CommandControlCode[3] = 0x00;	// n / 180 英寸


	strcat( Command, CommandControlCode) ;
*/
	//**********************************************************************
	// 开始打印数据
	// 
	// 车属单位

	for ( int i = 0 ; i < Lines ; i++ )
{
	strcat( Command, szPrintData[i][0] ) ;

	
	// 绝对位置右移(空格)
	strcat( Command, CPrintDirectCtrl::szSpaceData( 36 - strlen( szPrintData[i][0] ) ) ) ;

	// 车型

	//Command+="奔驰 600 SEL";
	strcat( Command, szPrintData[i][1] ) ;

	// 回车换行
	CommandControlCode[0] = 0x0A;
	CommandControlCode[1] = 0x0D;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;

	// 车号
	//Command+="鲁B 54624";
	// 倍宽
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x0E;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;

	strcat( Command, szPrintData[i][2] ) ;
	// 倍宽
	CommandControlCode[0] = 0x1C;
	CommandControlCode[1] = 0x14;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;

	// 绝对位置右移(空格)
	strcat( Command, CPrintDirectCtrl::szSpaceData( 36 - 2* strlen( szPrintData[i][2] ) ) ) ;


	// 吨位

	strcat( Command, szPrintData[i][3] ) ;


	// 回车换行
	CommandControlCode[0] = 0x0A;
	CommandControlCode[1] = 0x0D;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;


	// 发证人章

	strcat( Command, szPrintData[i][4] ) ;

	// 回车换行
	CommandControlCode[0] = 0x0A;
	CommandControlCode[1] = 0x0D;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;

	// 有效日期

	strcat( Command, szPrintData[i][5] ) ;

	// 回车换行
	CommandControlCode[0] = 0x0A;
	CommandControlCode[1] = 0x0D;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;

	
	// 鲁征微机号

	// 绝对位置右移(空格)
	strcat( Command, szSpaceData( 22 ) ) ;

	//Command+="02号机";
	strcat( Command, szPrintData[i][6] ) ;

	// 回车换行
	CommandControlCode[0] = 0x0A;
	CommandControlCode[1] = 0x0D;
	CommandControlCode[2] = 0x00;

	strcat( Command, CommandControlCode) ;

/*	//* 打印机前走纸	(一次设置 127 个 1/180 英寸单位)
	
	CommandControlCode[0] = 0x1B;
	CommandControlCode[1] = 0x4A;
	CommandControlCode[2] = 0x40; //0x31;
	CommandControlCode[3] = 0x00;

	strcat( Command, CommandControlCode) ;
*/



	//* 换页
	
	CommandControlCode[0] = 0x0C;
	CommandControlCode[1] = 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
	delete Command ;
	return nRet ;


}


/***************************************************************************/
//																		   //
//  返回指定数目的空格 (内部函数)			B/S							   //
//																		   //
//  参数值:int 4 个字节长度 (打印设备资源句柄)	PrintHandle				   //																   //
//																		   //
//																		   //	
//	返回值:int 4 个字节长度 (打印信息返回码)							   //
//																		   //
//			= 0 ---  错误 ;												   //
//			> 0 ---  正确												   //					   //
//																		   //
/***************************************************************************/

char * CPrintDirectCtrl::szSpaceData(int SpaceNumber)
{
	char spaceData[256];
	strcpy( spaceBlank , "" );
/*	for ( int i = 0 ; i < SpaceNumber ; i++ )
	{
		spaceData[i] = 0x20 ;

	}

	spaceData[SpaceNumber] = 0x00 ;
*/
	strcpy( spaceData , "");
	for ( int i = 0 ; i < SpaceNumber ; i++ )
	{
		strcat(spaceData, " ");

	}
	strcpy( spaceBlank, spaceData ) ;
	return spaceBlank;
}

/***************************************************************************/
//																		   //

⌨️ 快捷键说明

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