qt连接实体打印机,打印纸质文件
//需要打印的文件的路径
QString m_listStr ;
void Demo::ConnectToPrint()
{
wchar_t th[255] = {0};
//ui.comboBox_PrintName---选择一个打印机名称
ui.comboBox_PrintName->currentText().toWCharArray(th);
int bufSize = WideCharToMultiByte(CP_ACP, NULL, th, -1, NULL, 0, NULL, false);
HANDLE hPrinter;
DOC_INFO_1 DocInfo;
DWORD dwJob;
DWORD dwBytesWritten;
if (!OpenPrinter(th, &hPrinter, NULL))
{
int y = GetLastError();
std::cout << "openFail" << y << std::endl;
return ;
}
DocInfo.pDocName = LPWSTR("My Document\0");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = NULL;
if (0 == (dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo)))
{
int x = GetLastError();
std::cout << "StatDocPrinter Fail" << x << std::endl;
ClosePrinter(hPrinter);
return;
}
if (!StartPagePrinter(hPrinter))
{
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return ;
}
m_listStr;
for (auto &iter : m_listStr)
{
wchar_t th[255] = { 0 };
iter.toWCharArray(th);
int bufSize = WideCharToMultiByte(CP_ACP, NULL, th, -1, NULL, 0, NULL, false);
char* ch = new char[bufSize];
WideCharToMultiByte(CP_ACP, NULL, th, -1, ch, bufSize, NULL, false);
std::string(ch, bufSize);
ShellExecute(0, L"print",th,NULL, NULL, SW_SHOW);
delete[] ch;
ch = NULL;
}
if (!EndPagePrinter(hPrinter))
{
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return;
}
if (!EndDocPrinter(hPrinter))
{
ClosePrinter(hPrinter);
return;
}
}
//初始化--显示打印设备---显示到 ui.comboBox_PrintName
void Demo::InitPrintEquipment()
{
PRINTER_INFO_2* printerList;
unsigned char size;
unsigned long pcbNeeded;
unsigned long pcReturned;
EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &pcbNeeded, &pcReturned);
if (0 == (printerList = (PRINTER_INFO_2*)malloc(pcbNeeded)))
return;
if (!EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)printerList, pcbNeeded, &pcbNeeded, &pcReturned))
{
free(printerList);
return;
}
for (int i = 0; i < (int)pcReturned; i++)
{
LPWSTR printName(printerList[i].pPrinterName);
//if (printerList[i].Attributes & PRINTER_ATTRIBUTE_NETWORK)
// std::cout << "网络打印机: " << printName << std::endl;
//else
// std::cout << "本地打印机: " << printName << std::endl;
// std::cout << "number: " << pcReturned << std::endl;
ui.comboBox_PrintName->addItem(QString::fromWCharArray(printName));
}
}
//凭借记忆写的注释。。。。