Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Win32PrintTest.cpp
- // By Mike K.
- // Friday, February 6, 2015
- //
- // A function to print a test page using the same win32 functions that Qt uses but without
- // using the Qt print support.
- //
- #include "Win32PrintTest.h"
- #include <QDebug>
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void win32PrintTest()
- {
- qDebug() << "Starting Win32 Print Test";
- // Get the name of the default printer
- DWORD size = 0;
- GetDefaultPrinter(NULL, &size);
- TCHAR * printerName = new TCHAR[size];
- BOOL ret = GetDefaultPrinter(printerName, &size);
- if (!ret)
- {
- qDebug() << "Failed to get default printer name";
- delete [] printerName;
- return;
- }
- qDebug() << "Default printer name =" << QString::fromWCharArray(printerName);
- // Open the printer
- HANDLE hPrinter;
- ret = OpenPrinter(printerName, &hPrinter, NULL);
- if (!ret)
- {
- qDebug() << "Failed to open printer";
- delete [] printerName;
- return;
- }
- qDebug() << "Successfully opened printer";
- // Get the printer
- DWORD numBytes = 0;
- GetPrinter(hPrinter, 2, 0, 0, &size);
- BYTE * pBytes = new BYTE[size];
- ret = GetPrinter(hPrinter, 2, pBytes, size, &numBytes);
- if (!ret)
- {
- qDebug() << "Failed to get printer";
- ClosePrinter(hPrinter);
- delete [] pBytes;
- delete [] printerName;
- return;
- }
- qDebug() << "Successfully got printer";
- // Get the info
- PRINTER_INFO_2 * pPrinterInfo = (PRINTER_INFO_2 *) pBytes;
- qDebug() << "Server name =" << QString::fromWCharArray(pPrinterInfo->pServerName);
- qDebug() << "Printer name =" << QString::fromWCharArray(pPrinterInfo->pPrinterName);
- qDebug() << "Share name =" << QString::fromWCharArray(pPrinterInfo->pShareName);
- qDebug() << "Port name =" << QString::fromWCharArray(pPrinterInfo->pPortName);
- qDebug() << "Driver name =" << QString::fromWCharArray(pPrinterInfo->pDriverName);
- qDebug() << "Comment =" << QString::fromWCharArray(pPrinterInfo->pComment);
- qDebug() << "Location =" << QString::fromWCharArray(pPrinterInfo->pLocation);
- qDebug() << "Print processor =" << QString::fromWCharArray(pPrinterInfo->pPrintProcessor);
- qDebug() << "Data type =" << QString::fromWCharArray(pPrinterInfo->pDatatype);
- qDebug() << "Status is " << pPrinterInfo->Status << win32PrinterStatusToString(pPrinterInfo->Status);
- qDebug() << "Jobs in queue =" << pPrinterInfo->cJobs;
- DEVMODE * pDevMode = pPrinterInfo->pDevMode;
- bool bOwnDevMode = false;
- if (pDevMode == NULL)
- {
- qDebug() << "DEVMODE is NULL. Getting it a different way.";
- // Get the number of bytes required.
- LONG lBytesNeeded = DocumentProperties(NULL, hPrinter, printerName, NULL, NULL, 0);
- if (lBytesNeeded <= 0)
- {
- qDebug() << "DocumentProperties failed to return bytes needed (" << lBytesNeeded << ")";
- qDebug() << "GetLastError =" << GetLastError();
- ClosePrinter(hPrinter);
- delete [] pBytes;
- delete [] printerName;
- return;
- }
- // Allocate the buffer and get the DEVMODE
- pDevMode = (DEVMODE *) new BYTE[lBytesNeeded];
- bOwnDevMode = true;
- LONG lRet = DocumentProperties(NULL, hPrinter, printerName, pDevMode, NULL, DM_OUT_BUFFER);
- if ((lRet != IDOK) || (pDevMode == NULL))
- {
- qDebug() << "DocumentProperties failed to get DEVMODE";
- qDebug() << "GetLastError =" << GetLastError();
- ClosePrinter(hPrinter);
- if (pDevMode) delete [] (BYTE *)(pDevMode);
- delete [] pBytes;
- delete [] printerName;
- return;
- }
- }
- if (pDevMode)
- {
- qDebug() << "Device name" << QString::fromWCharArray(pDevMode->dmDeviceName);
- if (pDevMode->dmFields & DM_ORIENTATION) qDebug() << "Paper orientation =" << win32OrientationToString(pDevMode->dmOrientation);
- if (pDevMode->dmFields & DM_PAPERSIZE) qDebug() << "Paper size =" << win32PaperSizeToString(pDevMode->dmPaperSize);
- if (pDevMode->dmFields & DM_FORMNAME) qDebug() << "Form name =" << QString::fromWCharArray(pDevMode->dmFormName);
- if (pDevMode->dmFields & DM_PAPERWIDTH) qDebug() << "Paper width =" << pDevMode->dmPaperWidth/10.0 << "mm";
- if (pDevMode->dmFields & DM_PAPERLENGTH) qDebug() << "Paper length =" << pDevMode->dmPaperLength/10.0 << "mm";
- if (pDevMode->dmFields & DM_COPIES) qDebug() << "Copies =" << pDevMode->dmCopies;
- if (pDevMode->dmFields & DM_PRINTQUALITY) qDebug() << "Print quality =" << win32PrintQualityToString(pDevMode->dmPrintQuality);
- if (pDevMode->dmFields & DM_COLLATE) qDebug() << "Collate =" << (pDevMode->dmCollate == DMCOLLATE_FALSE ? "No" : "Yes");
- if (pDevMode->dmFields & DM_COLOR) qDebug() << "Color =" << (pDevMode->dmColor == DMCOLOR_COLOR ? "Yes" : "No");
- if (pDevMode->dmFields & DM_DUPLEX) qDebug() << "Duplex =" << win32DuplexSettingToString(pDevMode->dmDuplex);
- if (pDevMode->dmFields & DM_YRESOLUTION) qDebug() << "Y-resolution =" << pDevMode->dmYResolution << "DPI";
- if (pDevMode->dmFields & DM_DEFAULTSOURCE) qDebug() << "Default source =" << win32DefaultSourceToString(pDevMode->dmDefaultSource);
- }
- // Create a device context
- HDC hDC = CreateDC(NULL, printerName, 0, pDevMode);
- if (hDC == NULL)
- {
- qDebug() << "(!) Failed to create device context";
- ClosePrinter(hPrinter);
- if (bOwnDevMode) delete [] (BYTE *)(pDevMode);
- delete [] pBytes;
- delete [] printerName;
- return;
- }
- qDebug() << "Successfully created device context";
- // Get info about printer and its settings
- int xDPI = GetDeviceCaps(hDC, LOGPIXELSX);
- int yDPI = GetDeviceCaps(hDC, LOGPIXELSY);
- int widthPixels = GetDeviceCaps(hDC, PHYSICALWIDTH);
- int heightPixels = GetDeviceCaps(hDC, PHYSICALHEIGHT);
- int xOffset = GetDeviceCaps(hDC, PHYSICALOFFSETX);
- int yOffset = GetDeviceCaps(hDC, PHYSICALOFFSETY);
- qDebug() << "xDPI =" << xDPI;
- qDebug() << "yDPI =" << yDPI;
- qDebug() << "Size in device pixels =" << widthPixels << "x" << heightPixels;
- qDebug() << "xOffset =" << xOffset;
- qDebug() << "yOffset =" << yOffset;
- // Use 1/2 inch margin
- // Point (0, 0) is the upper-left of the printable area of the page, which is xOffset from the left edge of the paper and yOffset from the top edge of the paper.
- RECT rectPixels;
- rectPixels.left = xDPI/2 - xOffset;
- rectPixels.right = widthPixels - xDPI/2 - xOffset;
- rectPixels.top = yDPI/2 - yOffset;
- rectPixels.bottom = heightPixels - yDPI/2 - yOffset;
- // Draw something
- DOCINFO di = {0};
- di.cbSize = sizeof(DOCINFO);
- di.lpszDocName = L"Win32 Print Test";
- int sd = StartDoc(hDC, &di);
- if (sd > 0)
- {
- qDebug() << "Started document";
- int sp = StartPage(hDC);
- if (sp > 0)
- {
- qDebug() << "Started page";
- SelectObject(hDC, GetStockObject(NULL_BRUSH));
- int xMid = (rectPixels.left + rectPixels.right)/2;
- int yMid = (rectPixels.top + rectPixels.bottom)/2;
- Rectangle(hDC, xMid-xDPI/2, yMid-yDPI/2+2*yDPI, xMid+xDPI/2, yMid+yDPI/2+2*yDPI);
- Rectangle(hDC, xMid-xDPI/2, yMid-yDPI/2-2*yDPI, xMid+xDPI/2, yMid+yDPI/2-2*yDPI);
- Rectangle(hDC, xMid-xDPI/2-2*xDPI, yMid-yDPI/2, xMid+xDPI/2-2*xDPI, yMid+yDPI/2);
- Rectangle(hDC, xMid-xDPI/2+2*xDPI, yMid-yDPI/2, xMid+xDPI/2+2*xDPI, yMid+yDPI/2);
- DrawText(hDC, L"Win32 Print Test", -1, &rectPixels, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
- int ep = EndPage(hDC);
- if (ep) qDebug() << "Ended page"; else qDebug() << "Failed to end page";
- }
- else
- {
- // Failed to start page
- qDebug() << "Failed to start page";
- }
- int ed = EndDoc(hDC);
- if (ed) qDebug() << "Ended document"; else qDebug() << "Failed to end document";
- }
- else
- {
- // Failed to start print job
- qDebug() << "Failed to start document";
- }
- // Cleanup
- DeleteDC(hDC);
- ClosePrinter(hPrinter);
- if (bOwnDevMode) delete [] (BYTE *)(pDevMode);
- delete [] pBytes;
- delete [] printerName;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Converts a win32 printer status to a string (PRINTER_STATUS_*)
- QString win32PrinterStatusToString(DWORD status)
- {
- QString str;
- if (status & PRINTER_STATUS_BUSY) str += "Busy, ";
- if (status & PRINTER_STATUS_DOOR_OPEN) str += "Door Open, ";
- if (status & PRINTER_STATUS_ERROR) str += "Error, ";
- if (status & PRINTER_STATUS_INITIALIZING) str += "Initializing, ";
- if (status & PRINTER_STATUS_IO_ACTIVE) str += "IO Active, ";
- if (status & PRINTER_STATUS_MANUAL_FEED) str += "Manual feed, ";
- if (status & PRINTER_STATUS_NO_TONER) str += "No toner, ";
- if (status & PRINTER_STATUS_NOT_AVAILABLE) str += "Not available, ";
- if (status & PRINTER_STATUS_OFFLINE) str += "Offline, ";
- if (status & PRINTER_STATUS_OUT_OF_MEMORY) str += "Out of memory, ";
- if (status & PRINTER_STATUS_OUTPUT_BIN_FULL) str += "Output bin full, ";
- if (status & PRINTER_STATUS_PAGE_PUNT) str += "Cannot print current page, ";
- if (status & PRINTER_STATUS_PAPER_JAM) str += "Paper jam, ";
- if (status & PRINTER_STATUS_PAPER_OUT) str += "Paper out, ";
- if (status & PRINTER_STATUS_PAPER_PROBLEM) str += "Paper problem, ";
- if (status & PRINTER_STATUS_PAUSED) str += "Paused, ";
- if (status & PRINTER_STATUS_PENDING_DELETION) str += "Pending deletion, ";
- if (status & PRINTER_STATUS_POWER_SAVE) str += "Power save, ";
- if (status & PRINTER_STATUS_PRINTING) str += "Printing, ";
- if (status & PRINTER_STATUS_PROCESSING) str += "Processing, ";
- if (status & PRINTER_STATUS_SERVER_UNKNOWN) str += "Server unknown, ";
- if (status & PRINTER_STATUS_TONER_LOW) str += "Toner low, ";
- if (status & PRINTER_STATUS_USER_INTERVENTION) str += "User intervention, ";
- if (status & PRINTER_STATUS_WAITING) str += "Waiting, ";
- if (status & PRINTER_STATUS_WARMING_UP) str += "Warming up, ";
- // Chop off the last ", "
- if (str.endsWith(", ")) str = str.left(str.length() - 2);
- // If no status
- if (str.isEmpty()) str = "None";
- return "(" + str + ")";
- }
- QString win32OrientationToString(short orientation)
- {
- if (orientation == DMORIENT_PORTRAIT) return "Portrait";
- if (orientation == DMORIENT_LANDSCAPE) return "Landscape";
- return QString::number(orientation);
- }
- QString win32PaperSizeToString(short paperSize)
- {
- if (paperSize == DMPAPER_LETTER) return "Letter";
- if (paperSize == DMPAPER_LEGAL) return "Legal";
- if (paperSize == DMPAPER_A4) return "A4";
- if (paperSize == DMPAPER_USER) return "Custom";
- return QString::number(paperSize);
- }
- QString win32PrintQualityToString(short printQuality)
- {
- if (printQuality == DMRES_HIGH) return "High";
- if (printQuality == DMRES_MEDIUM) return "Medium";
- if (printQuality == DMRES_LOW) return "Low";
- if (printQuality == DMRES_DRAFT) return "Draft";
- if (printQuality > 0) return QString("%1 (xDPI)").arg(printQuality); // If positive, specifies the DPI along the x-axis.
- return QString::number(printQuality);
- }
- QString win32DuplexSettingToString(short duplex)
- {
- if (duplex == DMDUP_SIMPLEX) return "Simplex (nonduplex)";
- if (duplex == DMDUP_HORIZONTAL) return "Duplex short-edge binding";
- if (duplex == DMDUP_VERTICAL) return "Duplex long-edge binding";
- return QString::number(duplex);
- }
- QString win32DefaultSourceToString(short source)
- {
- if (source == DMBIN_AUTO) return "Auto";
- if (source == DMBIN_CASSETTE) return "Cassette";
- if (source == DMBIN_ENVELOPE) return "Envelope";
- if (source == DMBIN_ENVMANUAL) return "Envelope (manual)";
- if (source == DMBIN_FIRST) return "First";
- if (source == DMBIN_FORMSOURCE) return "Form source";
- if (source == DMBIN_LARGECAPACITY) return "Large capacity";
- if (source == DMBIN_LARGEFMT) return "Large format";
- if (source == DMBIN_LAST) return "Last";
- if (source == DMBIN_LOWER) return "Lower";
- if (source == DMBIN_MANUAL) return "Manual";
- if (source == DMBIN_MIDDLE) return "Middle";
- if (source == DMBIN_ONLYONE) return "Only one";
- if (source == DMBIN_TRACTOR) return "Tractor";
- if (source == DMBIN_SMALLFMT) return "Small format";
- if (source == DMBIN_UPPER) return "Upper";
- if (source >= DMBIN_USER) return QString("Device-specific value (%1)").arg(source);
- return QString::number(source);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement