📄 printdirectctl.cpp
字号:
// PrintDirectCtl.cpp : Implementation of the CPrintDirectCtrl ActiveX Control class.
#include "stdafx.h"
#include "PrintDirect.h"
#include "PrintDirectCtl.h"
#include "PrintDirectPpg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CPrintDirectCtrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CPrintDirectCtrl, COleControl)
//{{AFX_MSG_MAP(CPrintDirectCtrl)
// NOTE - ClassWizard will add and remove message map entries
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG_MAP
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CPrintDirectCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CPrintDirectCtrl)
DISP_FUNCTION(CPrintDirectCtrl, "ConnectToPrint", ConnectToPrint, VT_I4, VTS_BSTR)
DISP_FUNCTION(CPrintDirectCtrl, "DisConnectToPrinter", DisConnectToPrinter, VT_I4, VTS_I4)
DISP_FUNCTION(CPrintDirectCtrl, "PrintLine", PrintLine, VT_I4, VTS_I4 VTS_I2 VTS_I2 VTS_I2)
DISP_FUNCTION(CPrintDirectCtrl, "PrintOneLine", PrintOneLine, VT_I4, VTS_I4 VTS_BSTR VTS_I2 VTS_I2)
DISP_FUNCTION(CPrintDirectCtrl, "PrinterCheck", PrinterCheck, VT_I4, VTS_I4)
DISP_FUNCTION(CPrintDirectCtrl, "PaperFore", PaperFore, VT_I4, VTS_I4 VTS_I2)
DISP_FUNCTION(CPrintDirectCtrl, "PaperBack", PaperBack, VT_I4, VTS_I4 VTS_I2)
DISP_FUNCTION(CPrintDirectCtrl, "PrintYLFZSQKFXB", PrintYLFZSQKFXB, VT_I4, VTS_I4 VTS_BSTR)
DISP_FUNCTION(CPrintDirectCtrl, "PrintYLFMFZ", PrintYLFMFZ, VT_I4, VTS_I4 VTS_BSTR)
DISP_FUNCTION(CPrintDirectCtrl, "PrintYLFCLDTQKB", PrintYLFCLDTQKB, VT_I4, VTS_I4 VTS_BSTR)
DISP_FUNCTION(CPrintDirectCtrl, "PrintPJQKHZB", PrintPJQKHZB, VT_I4, VTS_I4 VTS_BSTR)
DISP_FUNCTION(CPrintDirectCtrl, "PrintString", PrintString, VT_I4, VTS_I4 VTS_BSTR VTS_I2 VTS_I2 VTS_I2 VTS_I2 VTS_I2)
DISP_FUNCTION(CPrintDirectCtrl, "PrintStringReport", PrintStringReport, VT_I4, VTS_BSTR VTS_BSTR VTS_I2 VTS_I2 VTS_I2 VTS_I2 VTS_I2 VTS_I2)
DISP_FUNCTION(CPrintDirectCtrl, "PageSet", PageSet, VT_I4, VTS_I4 VTS_I2 VTS_I2)
DISP_FUNCTION(CPrintDirectCtrl, "PageLF", PageLF, VT_I4, VTS_I4)
DISP_FUNCTION(CPrintDirectCtrl, "PrintMFZ", PrintMFZ, VT_I4, VTS_BSTR)
DISP_FUNCTION(CPrintDirectCtrl, "PrintCover", PrintCover, VT_I4, VTS_BSTR VTS_I2)
//}}AFX_DISPATCH_MAP
DISP_FUNCTION_ID(CPrintDirectCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CPrintDirectCtrl, COleControl)
//{{AFX_EVENT_MAP(CPrintDirectCtrl)
// NOTE - ClassWizard will add and remove event map entries
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CPrintDirectCtrl, 1)
PROPPAGEID(CPrintDirectPropPage::guid)
END_PROPPAGEIDS(CPrintDirectCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CPrintDirectCtrl, "PRINTDIRECT.PrintDirectCtrl.1",
0x6744f4d, 0xfc5, 0x11d9, 0x86, 0x68, 0, 0x11, 0x5b, 0x35, 0xbf, 0x86)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CPrintDirectCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DPrintDirect =
{ 0x6744f4b, 0xfc5, 0x11d9, { 0x86, 0x68, 0, 0x11, 0x5b, 0x35, 0xbf, 0x86 } };
const IID BASED_CODE IID_DPrintDirectEvents =
{ 0x6744f4c, 0xfc5, 0x11d9, { 0x86, 0x68, 0, 0x11, 0x5b, 0x35, 0xbf, 0x86 } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwPrintDirectOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CPrintDirectCtrl, IDS_PRINTDIRECT, _dwPrintDirectOleMisc)
/////////////////////////////////////////////////////////////////////////////
// CPrintDirectCtrl::CPrintDirectCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CPrintDirectCtrl
BOOL CPrintDirectCtrl::CPrintDirectCtrlFactory::UpdateRegistry(BOOL bRegister)
{
// TODO: Verify that your control follows apartment-model threading rules.
// Refer to MFC TechNote 64 for more information.
// If your control does not conform to the apartment-model rules, then
// you must modify the code below, changing the 6th parameter from
// afxRegApartmentThreading to 0.
if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_PRINTDIRECT,
IDB_PRINTDIRECT,
afxRegApartmentThreading,
_dwPrintDirectOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
/////////////////////////////////////////////////////////////////////////////
// CPrintDirectCtrl::CPrintDirectCtrl - Constructor
CPrintDirectCtrl::CPrintDirectCtrl()
{
InitializeIIDs(&IID_DPrintDirect, &IID_DPrintDirectEvents);
// TODO: Initialize your control's instance data here.
// 分配内存空间
spaceBlank = new char[256] ;
}
/////////////////////////////////////////////////////////////////////////////
// CPrintDirectCtrl::~CPrintDirectCtrl - Destructor
CPrintDirectCtrl::~CPrintDirectCtrl()
{
// TODO: Cleanup your control's instance data here.
// 删除分配内存, 并释放
delete spaceBlank ;
//delete []szPrintData ;
}
/////////////////////////////////////////////////////////////////////////////
// CPrintDirectCtrl::OnDraw - Drawing function
void CPrintDirectCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// TODO: Replace the following code with your own drawing code.
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
pdc->Ellipse(rcBounds);
}
/////////////////////////////////////////////////////////////////////////////
// CPrintDirectCtrl::DoPropExchange - Persistence support
void CPrintDirectCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
}
/////////////////////////////////////////////////////////////////////////////
// CPrintDirectCtrl::OnResetState - Reset control to default state
void CPrintDirectCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
/////////////////////////////////////////////////////////////////////////////
// CPrintDirectCtrl::AboutBox - Display an "About" box to the user
void CPrintDirectCtrl::AboutBox()
{
CDialog dlgAbout(IDD_ABOUTBOX_PRINTDIRECT);
dlgAbout.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CPrintDirectCtrl message handlers
/***************************************************************************/
// //
// 打开并口连接资源,检测打印机连接 //
// //
// //
// //
// //
/***************************************************************************/
long CPrintDirectCtrl::ConnectToPrint(LPCTSTR FileName)
{
// TODO: Add your dispatch handler code here
// 打开 LPT 端口, 检查打印机连接
// hHandlePrint
HANDLE hHandle = NULL ;
int nRet = -1 ;
DWORD dwCreationDisposition = 0;
DWORD dwRet = 0 ;
if ( FileName == "LPT1" )
dwCreationDisposition = OPEN_EXISTING ;
else
dwCreationDisposition = CREATE_ALWAYS ;
// "LPT1"
hHandle = CreateFile( FileName, GENERIC_READ|GENERIC_WRITE, NULL, NULL, dwCreationDisposition, FILE_FLAG_OVERLAPPED, NULL );
if ( INVALID_HANDLE_VALUE == hHandle )
{
//打开并口错误(并口已被占用)
dwRet = (int)hHandle;
dwRet = GetLastError();
return dwRet ;
}
else
{
// 设置通讯超时
COMMTIMEOUTS m_CommTimeouts ;
m_CommTimeouts.ReadIntervalTimeout = 0xFFFFFFFF; // 读操作两个字节之间
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000; // 读操作的时间(单位:毫秒)
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeoutConstant = 5000;
SetCommTimeouts( hHandle, &m_CommTimeouts );
hHandlePrint = (int)hHandle ;
dwRet = (int)hHandle;
}
// 检测打印机连接状态
char *Command = new char[10] ;
char CommandControlCode[10];
DWORD dwRead = 0 ;
DWORD dwError = -1;
dwRet = -1 ;
if ( hHandle <= 0 ) // 非法的设备句柄
{
return -11 ;
delete Command ;
}
// 设置联机打印, 并蜂鸣
CommandControlCode[0] = 0x1B;
CommandControlCode[1] = 0x39;
CommandControlCode[2] = 0x11;
CommandControlCode[3] = 0x07;
CommandControlCode[4] = 0x00;
strcpy( Command, CommandControlCode) ;
OVERLAPPED stOverLappedWrite ;
ZeroMemory( &stOverLappedWrite, sizeof(OVERLAPPED));
stOverLappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
// 设置通讯超时
COMMTIMEOUTS m_CommTimeouts ;
m_CommTimeouts.ReadIntervalTimeout = 0xFFFFFFFF; //读操作两个字节之间
m_CommTimeouts.ReadTotalTimeoutMultiplier = 0; //读操作的时间(单位:毫秒)
m_CommTimeouts.ReadTotalTimeoutConstant = 0;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
m_CommTimeouts.WriteTotalTimeoutConstant = 5000;
SetCommTimeouts( hHandle, &m_CommTimeouts );
// 写并口(输出到并口资源)
nRet = WriteFile( hHandle, Command, strlen( Command), &dwRead, &stOverLappedWrite ) ;
if ( nRet == 0 )
{
DWORD dwTransferredData ;
dwRet = GetOverlappedResult( hHandle, &stOverLappedWrite, &dwTransferredData, TRUE) ;
if ( dwRet == 0 )
{
dwError = GetLastError();
switch (dwError)
{
case 21:
dwRet = -21;
break;
case 28:
dwRet = -23;
break;
case 1167:
dwRet = -22;
break;
default:
dwRet = -1;
break;
}
}
}
delete Command ;
if ( dwRet <= 0 )
return dwRet ;
else
return (long)hHandle;
}
/************************************************************************************************/
// //
// 断开打印机连接,释放并口连接资源 //
// //
// //
// //
// //
/************************************************************************************************/
long CPrintDirectCtrl::DisConnectToPrinter(long PrintHandle)
{
// TODO: Add your dispatch handler code here
// 断开打印机连接,释放并口资源
int nRet = -1 ;
HANDLE hHandle = NULL ;
hHandle = (HANDLE)PrintHandle ;
if ( PrintHandle <= 0 ) // 非法的设备句柄
{
if ( hHandlePrint <= 0 )
{
nRet = -11 ;
}
else
{
nRet = CloseHandle( (HANDLE)hHandlePrint );
}
}
else
{
nRet = CloseHandle( hHandle ) ;
}
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 //
// //
/************************************************************************************************/
long CPrintDirectCtrl::PrintLine(long PrintHandle, short LineLength, short LineS, short Flag)
{
// TODO: Add your dispatch handler code here
// 打印横线(用于制表)
int nRet = -1 ;
int i = 0 ; // 循环计数
HANDLE hHandle = NULL ;
hHandle = (HANDLE) PrintHandle;
if ( PrintHandle <= 0 ) // 非法的设备句柄
return -11 ;
DWORD t1,dwRead;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -