Files
mfc/tests/addprinter.cpp
T
Claude ffc4ccff8f Print preview and the printing progress dialog
CPreviewDC draws with the printer's DC for layout and the window's for
output, scaled to the page on the screen; CPreviewView shows one or two
pages, zooms, scrolls and pages; the frame hides its menu, bars and main
pane while preview is on. The preview toolbar and the printing dialog
come from the program's resources when it has them (afxprint.rc) and
are built in memory otherwise.

The tests run in a Wine prefix of their own, which gets a PostScript
printer, and the SDI test now previews several pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRUqfQ3sTcDCMdNCGGs
2026-11-06 16:48:17 +00:00

90 lines
2.8 KiB
C++

// addprinter.cpp - gives the Wine prefix the tests run in a printer, so
// printing and print preview have a printer DC. Wine's PostScript driver
// needs a PPD file; this one describes a plain US Letter printer.
#include <windows.h>
#include <stdio.h>
static const char szPPD[] =
"*PPD-Adobe: \"4.3\"\n"
"*FormatVersion: \"4.3\"\n"
"*FileVersion: \"1.0\"\n"
"*LanguageVersion: English\n"
"*LanguageEncoding: ISOLatin1\n"
"*PCFileName: \"MFCTEST.PPD\"\n"
"*Manufacturer: \"MFC Test\"\n"
"*Product: \"(MFC Test Printer)\"\n"
"*PSVersion: \"(3010.000) 0\"\n"
"*ModelName: \"MFC Test Printer\"\n"
"*ShortNickName: \"MFC Test Printer\"\n"
"*NickName: \"MFC Test Printer\"\n"
"*ColorDevice: True\n"
"*DefaultColorSpace: RGB\n"
"*LanguageLevel: \"3\"\n"
"*DefaultResolution: 300dpi\n"
"*OpenUI *PageSize: PickOne\n"
"*DefaultPageSize: Letter\n"
"*PageSize Letter/US Letter: \"<</PageSize[612 792]>>setpagedevice\"\n"
"*CloseUI: *PageSize\n"
"*DefaultPageRegion: Letter\n"
"*DefaultImageableArea: Letter\n"
"*ImageableArea Letter/US Letter: \"18 36 594 756\"\n"
"*DefaultPaperDimension: Letter\n"
"*PaperDimension Letter/US Letter: \"612 792\"\n";
int main() {
const char* pszName = "MFC Test Printer";
char szDir[MAX_PATH], szPath[MAX_PATH];
DWORD cb = sizeof(szDir);
if (!GetPrinterDriverDirectoryA(NULL, NULL, 1, (BYTE*)szDir, cb, &cb)) {
printf("no printer driver directory\n");
return 1;
}
CreateDirectoryA(szDir, NULL);
snprintf(szPath, sizeof(szPath), "%s\\mfctest.ppd", szDir);
FILE* f = fopen(szPath, "wb");
if (f == NULL) {
printf("cannot write %s\n", szPath);
return 1;
}
fputs(szPPD, f);
fclose(f);
DRIVER_INFO_3A di;
ZeroMemory(&di, sizeof(di));
di.cVersion = 3;
di.pName = (LPSTR) "MFC Test PostScript";
di.pDriverPath = (LPSTR) "wineps.drv";
di.pDataFile = (LPSTR) "mfctest.ppd";
di.pConfigFile = (LPSTR) "wineps.drv";
if (!AddPrinterDriverA(NULL, 3, (BYTE*)&di) && GetLastError() != ERROR_PRINTER_DRIVER_ALREADY_INSTALLED) {
printf("AddPrinterDriver failed: %lu\n", GetLastError());
return 1;
}
PRINTER_INFO_2A pi;
ZeroMemory(&pi, sizeof(pi));
pi.pPrinterName = (LPSTR)pszName;
pi.pPortName = (LPSTR) "FILE:";
pi.pDriverName = di.pName;
pi.pPrintProcessor = (LPSTR) "WinPrint";
pi.pDatatype = (LPSTR) "RAW";
HANDLE h = AddPrinterA(NULL, 2, (BYTE*)&pi);
if (h != NULL)
ClosePrinter(h);
else if (GetLastError() != ERROR_PRINTER_ALREADY_EXISTS) {
printf("AddPrinter failed: %lu\n", GetLastError());
return 1;
}
if (!SetDefaultPrinterA(pszName)) {
printf("SetDefaultPrinter failed: %lu\n", GetLastError());
return 1;
}
HDC hDC = CreateDCA("WINSPOOL", pszName, NULL, NULL);
if (hDC == NULL) {
printf("CreateDC failed\n");
return 1;
}
DeleteDC(hDC);
return 0;
}