創(chuàng)新互聯(lián)www.cdcxhl.cn八線(xiàn)動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買(mǎi)多久送多久,劃算不套路!

小編給大家分享一下如何使用c++調(diào)用windows api進(jìn)行打印,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討方法吧!
前言
在近期開(kāi)發(fā)的收銀臺(tái)項(xiàng)目中,需要使用打印機(jī)進(jìn)行小票打印,打印流程的時(shí)序圖如下所示:

在客戶(hù)的使用過(guò)程中,遇到一個(gè)問(wèn)題,如果機(jī)器安裝了打印機(jī)驅(qū)動(dòng),那么調(diào)用廠商提供的 sdk 進(jìn)行打印的話(huà),會(huì)導(dǎo)致出現(xiàn)小票只打印一半的情況,對(duì)此,需要繞過(guò)廠商 sdk 使用系統(tǒng)的打印才能夠解決這一問(wèn)題。
在 web 端打印中,需要調(diào)用瀏覽器打印 api 進(jìn)行網(wǎng)頁(yè)打印。這意味著,之前后端編寫(xiě)的esc/pos無(wú)法復(fù)用到,同時(shí),前端還得花費(fèi)精力來(lái)編寫(xiě) html 以及css 來(lái)完成打印內(nèi)容的排版,這無(wú)疑增加了復(fù)雜度以及工作量。正打算開(kāi)始時(shí),得到高人指點(diǎn)。
可以使用 windows api 進(jìn)行打印
具體參見(jiàn)這篇文檔
于是開(kāi)始這方面的研究,功夫不負(fù)有心人,使用 windows api 完成了系統(tǒng)的打印,于是編寫(xiě)這篇文章記錄踩過(guò)的坑。
首先看看如何進(jìn)行打印:
BOOL RawDataToPrinter(LPSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
HANDLE hPrinter;
DOC_INFO_1 DocInfo;
DWORD dwJob;
DWORD dwBytesWritten;
// Need a handle to the printer.
if (!OpenPrinter(szPrinterName, &hPrinter, NULL)) {
int y = GetLastError();
cout << "openFail" << y << endl;
return FALSE;
}
// Fill in the structure with info about this "document."
DocInfo.pDocName = LPSTR("My Document\0");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = NULL; // LPWSTR("RAW\0");
// Inform the spooler the document is beginning.
if ((dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo)) == 0)
{
int x = GetLastError();
cout << "StartDocPrinter Fail" << x << endl;
ClosePrinter(hPrinter);
return FALSE;
}
// Start a page.
if (!StartPagePrinter(hPrinter))
{
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return FALSE;
}
// Send the data to the printer.
if (!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten))
{
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return FALSE;
}
// End the page.
if (!EndPagePrinter(hPrinter))
{
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return FALSE;
}
// Inform the spooler that the document is ending.
if (!EndDocPrinter(hPrinter))
{
ClosePrinter(hPrinter);
return FALSE;
}
// Tidy up the printer handle.
ClosePrinter(hPrinter);
// Check to see if correct number of bytes were written.
if (dwBytesWritten != dwCount)
return FALSE;
return TRUE;
}
當(dāng)前文章:如何使用c++調(diào)用windowsapi進(jìn)行打印-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://chinadenli.net/article18/gdidp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、全網(wǎng)營(yíng)銷(xiāo)推廣、品牌網(wǎng)站制作、企業(yè)網(wǎng)站制作、網(wǎng)站維護(hù)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容