最近在给以个客户做一个POS机上的打印程序,其实现的简单思路大致如下:
void ComPrint()
{
static int nIndex = 1;
HDC dc;
// USES_CONVERSION;
DEVMODEW lpdevmodew;
memset(&lpdevmodew, 0, sizeof(DEVMODEW));
lpdevmodew.dmSize = sizeof(DEVMODEW);
wcscpy( lpdevmodew.dmDeviceName, _T("XDLThermal Printer") );
lpdevmodew.dmPaperSize = 0; // 0 means User Define
lpdevmodew.dmPaperLength = 800; //600 // Length for 1000 C.L
lpdevmodew.dmPaperWidth = 800; // Width 80 mm
lpdevmodew.dmPrintQuality = -4;
DWORD status;
DWORD dwRtn = PrinterStatus(&status);
dc = CreateDC(TEXT("MyPrint.dll"), TEXT("XDL Thermal Printer"), TEXT("XDLBuild-In"), (DEVMODE *)(&lpdevmodew) ); //这个是驱动哦,这里不多说,直接CALL驱动的DLL就OK。
if(dc == NULL)
{
//return 0;
}
DOCINFO di;
LOGFONT lf;
RECT rt;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = TEXT("Hello World");
ZeroMemory(&lf, sizeof(lf));
lf.lfHeight = 40;
lf.lfWidth = 7;
::lstrcpy(lf.lfFaceName,_T("Arial"));
HFONT hFont, hOldFont, hRMB, hOldRmb;
// hFont = CreateFontIndirect(&lf);
// hOldFont = (HFONT)SelectObject(dc, hFont);
lf.lfHeight = 45;
lf.lfWidth = 8;
lf.lfWeight = FW_BOLD;
hRMB = CreateFontIndirect(&lf);
hOldFont = (HFONT)SelectObject(dc, hRMB);
StartDoc(dc, &di);
StartPage(dc);
rt.right = 700;
rt.top = 0;
rt.bottom = 80;
rt.left = 0;
// hOldFont = (HFONT)SelectObject(dc, hFont);
SelectObject(dc, hRMB);
TCHAR szTemp[32] = {0};
wsprintf(szTemp, _T("Print -- %i"), nIndex);
DrawText(dc,szTemp, -1, &rt, DT_CENTER);
/*
for(int i=0; i<2; i++)
{
//打印宽度由rt.right决定
DrawText(dc,TEXT("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV"),-1,&rt, DT_WORDBREAK);
OffsetRect(&rt, 0, 45); //平移矩形,相当于换行
}
*/
SelectObject(dc, hOldFont);
EndPage(dc);
EndDoc(dc);
nIndex++;
// DeleteObject(hFont);
DeleteObject(hRMB);
DeleteDC(dc);
return 0;
}
VOID EndPrint(HWND hWnd,HINSTANCE hInst,HDC hdc,HFONT hFont,HFONT hLargeFont,HFONT hOldFont,DWORD dwMessage)
{
SelectObject(hdc,hOldFont);
EndPage(hdc);
EndDoc(hdc);
DeleteObject(hFont);
DeleteObject(hLargeFont);
DeleteDC(hdc);
WaitForPrinter(hWnd);
}