Home > Blockchain >  how to implement duplex printing via GDI Print API
how to implement duplex printing via GDI Print API

Time:04-17

I need to implement duplex printing on a printer. I set the DEVMODE structure in the printer driver with the dmDuplex parameter

int dvmSize=DocumentProperties(GetForegroundWindow(),hPrinter,(LPWSTR)(printerName.c_str()),NULL,NULL,0);
DEVMODE *dvmSettings = (DEVMODE*) GlobalAlloc(GPTR,dvmSize);
DEVMODE *dvMode = (DEVMODE*) GlobalAlloc(GPTR,dvmSize);
DocumentProperties(GetForegroundWindow(),hPrinter,(LPWSTR)(printerName.c_str()),dvmSettings,NULL,DM_OUT_BUFFER);
dvmSettings->dmDuplex=DMDUP_HORIZONTAL;
dvmSettings->dmFields=DM_DUPLEX;
DocumentProperties(GetForegroundWindow(),hPrinter,(LPWSTR)(printerName.c_str()),dvMode,dvmSettings,DM_IN_BUFFER|DM_OUT_BUFFER);

As far as I understand, there are 2 ways of implementation. Via WritePrinter or create a HDC using CreateDC passing dvMode and draw directly to the context. The second way is more convenient for me, but will it work? I'm asking because I can't test it right now and I need to know if it will work.

CodePudding user response:

As far as I'm concerned, it will work.

You could call the CreateDC or PrintDlgEx function to get the printer DC.

If your application calls the CreateDC function, it must supply a driver and port name. To retrieve these names, call the GetPrinter or EnumPrinters function.

If your application calls the PrintDlgEx function and specifies the PD_RETURNDC value in the Flags member of the PRINTDLGEX structure, the system returns a handle to a device context for the printer selected by the user.

For more details I suggest you could refer to the Docs:

  1. https://docs.microsoft.com/en-us/windows/win32/printdocs/sending-data-directly-to-a-printer

  2. https://docs.microsoft.com/zh-cn/windows/win32/printdocs/gdi-printing

  3. https://docs.microsoft.com/en-us/windows/win32/printdocs/printer-output

  4. https://docs.microsoft.com/zh-cn/windows/win32/dlgbox/using-common-dialog-boxes

  • Related