问题描述
我已经通过Windows计算机上的USB端口安装了ESC / POS热敏打印机,并且尝试使用粗体,双倍高和双倍宽度的文本进行打印,但是它不起作用,我尝试使用WinAPI进行打印。 spooler cuz这是一台USB热敏打印机,我尝试了以下代码,但该打印机无法正常工作,无法正常打印文本大小
#include <windows.h>
#include <string>
#include <vector>
#include <codecvt>
#include <iostream>
const char * posPrintername = "XP-58"; //seting printer
LPTSTR szPrinterName;//declare wide size printer anme
LPBYTE lpData,lpData2;//declare varaiable to print
DWORD dwCount,dwCount2;//decalre length of print contents
HANDLE hPrinter;
DOC_INFO_1 DocInfo;
DWORD dwJob;
DWORD dwBytesWritten;
//convert the printer name to the wide characters
wchar_t * posPrinternamew = new wchar_t[strlen(posPrintername) + 1];
mbstowcs_s(NULL,posPrinternamew,strlen(posPrintername) + 1,posPrintername,strlen(posPrintername));
//pos58 instructions
char initialstr[8] = "\x0D\x1B\x40\x0A";
char bolderstr[2] = "\x08";
char doubleheightstr[2] = "\x10";
char doublewidthstr[2] = "\x20";
char resetstr[2] = "\x00";
//print test contents
std::string printcontents = "hello world\n ";
lpData = (LPBYTE)printcontents.c_str();
dwCount = strlen(printcontents.c_str());
// Need a handle to the printer.
szPrinterName = posPrinternamew;
if (!OpenPrinter(szPrinterName,&hPrinter,NULL))
{
return -1;
}
// Fill in the structure with info about this "document."
DocInfo.pDocName = TEXT("My Document");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = TEXT("RAW");
// Inform the spooler the document is beginning.
if ((dwJob = StartDocPrinter(hPrinter,1,(LPBYTE)&DocInfo)) == 0)
{
ClosePrinter(hPrinter);
return -2;
}
// Start a page.
if (!StartPagePrinter(hPrinter))
{
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -3;
}
// Send the data to the printer. tried to initial the printer
if (!WritePrinter(hPrinter,initialstr,(DWORD)4L,&dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
// Send the data to the printer. tried to set the bolder text
if (!WritePrinter(hPrinter,bolderstr,(DWORD)3L,&dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
// Send the data to the printer. tried to print with bolder text
if (!WritePrinter(hPrinter,lpData,dwCount,&dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
//tried to set double height text
if (!WritePrinter(hPrinter,doubleheightstr,&dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
// Send the data to the printer. //tried to print with double height text
if (!WritePrinter(hPrinter,&dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
//tried to set double width text
if (!WritePrinter(hPrinter,doublewidthstr,&dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
// Send the data to the printer. //tried to print with double width text
if (!WritePrinter(hPrinter,&dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
//tried to reset the printer
if (!WritePrinter(hPrinter,resetstr,&dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
// End the page.
if (!EndPagePrinter(hPrinter))
{
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -5;
}
// Inform the spooler that the document is ending.
if (!EndDocPrinter(hPrinter))
{
ClosePrinter(hPrinter);
return -6;
}
// Tidy up the printer handle.
ClosePrinter(hPrinter);
// Check to see if correct number of bytes were written.
if(dwBytesWritten != dwCount)
{
return -7;
}
return 0;
知道我做错了什么吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)