// 使用GDI打印输出文本
int WINAPI PTR_PrintText(LPCSTR szDocName, LPBYTE pBuf, int iLen)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDC printdc;
CPrintDialog dlg(FALSE, PD_NOPAGENUMS|PD_NOSELECTION);
dlg.GetDefaults();
{
CDC dc;
DOCINFO di;
dc.Attach(dlg.GetPrinterDC());//把打印设备环境附加到DC对象
::ZeroMemory (&di, sizeof (DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = szDocName;
di.lpszOutput = NULL;
di.lpszDatatype = NULL;
di.fwType = 0;
dc.StartDoc(&di); //通知打印机驱动程序执行一新的打印任务
dc.StartPage();//通知打印机驱动程序打印新页
dc.SetWindowExt(1440,1440);
dc.SetViewportExt( dc.GetDeviceCaps(LOGPIXELSX),//返回设备的.以毫米为单位的物理显示宽度
dc.GetDeviceCaps(LOGPIXELSX));//返回设备的.以毫米为单位的物理显示高度 )
//X方向向右增加,Y方向向上增加
CRect rectPrint(0, 0,
dc.GetDeviceCaps(HORZRES),//返回设备的.以毫米为单位的物理显示宽度
dc.GetDeviceCaps(VERTRES));//返回设备的.以毫米为单位的物理显示高度
dc.DPtoLP(&rectPrint);//设备物理单位转化为逻辑单位
dc.SetWindowOrg(0, 0);//设置原点
CFont font;
VERIFY(font.CreatePointFont(120,
"宋体",//"Arial",
&dc));//为DC创建字体
CFont* def_font = dc.SelectObject(&font);//保存现在的字体
dc.SetTextAlign(TA_TOP|TA_LEFT);
int index;
CSize size;
int x = 1;
int y = 10;//3800;//注意原点位置和坐标增加的方向
int y0 = y;
size = dc.GetTextExtent("00", 2);//计算使用当前字体输出时文本所占大小、宽 度
// #CR##LF#
#define CRLF "#CR##LF#"
CString s = "";
CString szpBuf((char *)pBuf, iLen); //要打印的字符串(注意有长度的限制)
// TfxWriteToFile("c://prr_log.txt", (LPCSTR)szpBuf);
szpBuf += CRLF;
szpBuf += "/n";
//0a0d
char achSep[4] = {0x0a, 0x0d};
CString strSep = CString(achSep);
while((index = szpBuf.Find(strSep)) != -1)
{
s += szpBuf.Left(index);
s += CRLF;
szpBuf = szpBuf.Mid(index+strlen(strSep));
}
s +=szpBuf;
CString ss;
while((index = s.Find(CRLF)) != -1)
{
ss = s.Left(index);
if(ss.Find("/r") != -1)//输入的字符串有回车符
ss = s.Left(index-1);
int t = 0;
if(((t = ss.Find("#CPI")) != -1)||((t = ss.Find("#cpi")) != -1))//输入的字符串有回车符
{
CString s1 = ss.Left(t);
CString s2 = ss.Mid(t+6);
ss = s1 + s2;
}
s = s.Mid(index+strlen(CRLF));//取剩余的字符串 // AfxMessageBox("A"+ss+"A");
/*
if((index = s.Find("#CR##LF##CR##LF##CR##LF##CR##LF##CR##LF#")) != -1)
{
//s = s.Mid(index+strlen("#CR##LF##CR##LF##CR##LF##CR##LF#"));//取剩余的字符串
string szBitmapName = "C://SSPC//receipt.bmp";
CDC memdc;
BITMAP bm;
memdc.CreateCompatibleDC( NULL );
HBITMAP pbitmap = (HBITMAP)LoadImage(NULL, szBitmapName.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
memdc.SelectObject(pbitmap);
GetObject(pbitmap, sizeof(bm), &bm);
dc.StretchBlt(0, 0, bm.bmWidth, bm.bmHeight, &memdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
memdc.DeleteDC();
DeleteObject(pbitmap);
}
*/
int offset = 0;
if(ss.Find("Seq No") != -1)
ss = "";
else if((ss.Find("CWD")!=-1)||(ss.Find("PIN")!=-1))
{
offset = 5*size.cx;
ss = ss.Mid(4);
}
else if(ss.Find("TFR")!=-1)
{
// CString strICBC = "转入帐号";
// dc.TextOut(300, y0+size.cy*18, strICBC);//打印到缓冲区
offset = 5*size.cx;
ss = ss.Mid(4);
}
dc.TextOut(x+offset, y, ss);//打印到缓冲区
string ls_text="";
ls_text = "x:"+ TfxIntToStr(x+offset) + ", y:" + TfxIntToStr(y) + ">>>";
ls_text += (LPCSTR)ss;
// TfxWriteToFile("c://prr_prn_log.txt", ls_text);
y += size.cy;
}
dc.SelectObject(def_font); //恢复原来的字体
font.DeleteObject();
dc.EndPage(); //通知打印机驱动程序页结束
dc.EndDoc();//通知打印机驱动程序打印完毕
DeleteDC(dc.Detach());
}
return 0;
}