天天看點

c++控制列印機列印demo

#include <stdafx.h>
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

#define PRINTER_NAME "HP LaserJet 5200 Series PCL 5-hzz3"

int _tmain() {
    string hello = "!!! Hello Printer !!!\f";
    HANDLE hPrinter = NULL;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob;
    DWORD      dwBytesWritten = ;


    if(OpenPrinter( (LPTSTR)_T(PRINTER_NAME), &hPrinter, NULL )) {
        cout << "printer opened" << endl;
        DocInfo.pDocName = (LPTSTR)_T("My Document");
        DocInfo.pOutputFile = NULL;
        DocInfo.pDatatype = (LPTSTR)_T("RAW");
        dwJob = StartDocPrinter( hPrinter, , (LPBYTE)&DocInfo );
        if (dwJob != ) {
            cout << "Print job open" << endl;
            if (StartPagePrinter( hPrinter )) {
                cout << "Page started" << endl;
                // Send the data to the printer.
                if (WritePrinter( hPrinter, (void*)hello.c_str(), hello.length(), &dwBytesWritten)) {
                    if (dwBytesWritten == hello.length()) { cout << "Message sent to printer" << endl; }
                }

                cout << "Page Closed" << endl;
            }
            // Inform the spooler that the document is ending.
            EndDocPrinter( hPrinter );
            cout << "Print job open" << endl;
        } else {
            cout << "Could not create print job" << endl;
        }
        // Close the printer handle.
        ClosePrinter( hPrinter );
        cout << "printer closed" << endl;
    } else {
        cout << "Could not open Printer" << endl;
    }
    cout << "done";
    return ;
}


           
c++

繼續閱讀