Procedura: Inviare dati direttamente a una stampante GDI
L'esempio di codice riportato più avanti in questo argomento illustra come inviare i dati di controllo della stampante direttamente alle stampanti che usano driver di stampante basati su GDI.
I passaggi seguenti descrivono come inviare dati direttamente a una stampante. Questi passaggi sono illustrati anche nell'esempio di codice seguente.
- Chiamare OpenPrinter per ottenere un handle per la stampante.
- Inizializzare una struttura DOCINFO con i dati della stampante.
- Chiamare StartDocPrinter per indicare che l'applicazione inserirà i dati del documento alla stampante.
- Chiamare StartPagePrinter per indicare che l'applicazione insedierà una nuova pagina alla stampante.
- Chiamare WritePrinter per inviare i dati.
- Chiamare EndPagePrinter per indicare che tutti i dati per la pagina corrente sono stati inviati.
- Chiamare EndDocPrinter per indicare che tutti i dati per questo documento sono stati inviati.
- Chiamare ClosePrinter per rilasciare le risorse.
Inviare i dati di controllo della stampante direttamente alle stampanti che utilizzano driver di stampante basati su GDI.
//
// RawDataToPrinter - sends binary data directly to a printer
//
// szPrinterName: NULL-terminated string specifying printer name
// lpData: Pointer to raw data bytes
// dwCount Length of lpData in bytes
//
// Returns: TRUE for success, FALSE for failure.
//
BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
BOOL bStatus = FALSE;
HANDLE hPrinter = NULL;
DOC_INFO_1 DocInfo;
DWORD dwJob = 0L;
DWORD dwBytesWritten = 0L;
// Open a handle to the printer.
bStatus = OpenPrinter( szPrinterName, &hPrinter, NULL );
if (bStatus) {
// Fill in the structure with info about this "document."
DocInfo.pDocName = (LPTSTR)_T("My Document");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = (LPTSTR)_T("RAW");
// Inform the spooler the document is beginning.
dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
if (dwJob > 0) {
// Start a page.
bStatus = StartPagePrinter( hPrinter );
if (bStatus) {
// Send the data to the printer.
bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);
EndPagePrinter (hPrinter);
}
// Inform the spooler that the document is ending.
EndDocPrinter( hPrinter );
}
// Close the printer handle.
ClosePrinter( hPrinter );
}
// Check to see if correct number of bytes were written.
if (!bStatus || (dwBytesWritten != dwCount)) {
bStatus = FALSE;
} else {
bStatus = TRUE;
}
return bStatus;
}
Argomenti correlati