Click here to Skip to main content
13,260,161 members (54,025 online)
Rate this:
Please Sign up or sign in to vote.
Hi all,

I am developing a windows forms application that selects client ID card designs by whatever client is selected in a datagridview, and then prints them on PVC ID cards using a Magicard Rio Pro card printer.

I want to handle all the printer settings within my application, so that the user does not need to worry about this, and the only setting that I need to control is portrait / landscape orientations.

I am handling the page orientation in my code using the PrinterSettings.DefaultPageSettings.Landscape = true/false, which works fine. But... it does not change the document (page), or in this case "card" orientation when a print job takes place, meaning it prints a landscape image on a portrait document. Now, if printing on A4 paper this is not a problem as there is plenty of room, however, on a small ID card this is not the case!

I have been searching for a solution but none are an easy feet.. I have see another post on CP () which works to some extent, not keen on displaying the printers dialogBox but at this stage I just want it to work.

When I run my application at present and process a print job, I set the orientation to landscape in the printer dialogBox that pops up but this does not change the default setting. In the other post mentioned above it is suggested that the new selected printer settings are not saved back to the defaultPrinterSettings, and when I step through my code it looks as though this is the case.

This is a new area for me re: printing and have only just come across DEVMODE a few days ago, and its quite a chunk to get my head round!

Could someone please help me out with a piece of sample code on how to save selected settings back to the DefaultPrinterSettings, and also how to add (if necessary) the orientation parameter ORIENT_LANDSCAPE.

Your help will be very very gratefully received.

<pre lang="c#">private void OpenPrinterPropertiesDialog()
            var printerSettings = new System.Drawing.Printing.PrinterSettings();
            var printerName = printerSettings.PrinterName;
            IntPtr handle;
            OpenPrinter(printerName, out handle, IntPtr.Zero);
            IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
            IntPtr pDevMode = GlobalLock(hDevMode);
            int sizeNeeded = DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, IntPtr.Zero, pDevMode, 0);
            IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
            // opens Magicard dialog
            DocumentProperties(this.Handle, handle, printerName, devModeData, pDevMode, DM_IN_BUFFER | DM_PROMPT | DM_OUT_BUFFER);
            // need to save changes here!!!
Posted 7-Jan-13 3:36am
Updated 7-Jan-13 3:37am
Why, why? Standard print dialogs already allow to save preference — look thoroughly. Using P/Invoke kills the platform compatibility of your code, did you know that?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


You can use Marshal class and it methods PtrToStructure and StructureToPtr to convert your pDevMode IntPtr. The Unicode version of the structure DEVMODE will be:
public struct DEVMODEW
    public string dmDeviceName;
    public int dmSpecVersion;
    public int dmDriverVersion;
    public int dmSize;
    public int dmDriverExtra;
    public int dmFields;
    public int dmOrientation;
    public int dmPaperSize;
    public int dmPaperLength;
    public int dmPaperWidth;
    public int dmScale;
    public int dmCopies;
    public int dmDefaultSource;
    public int dmPrintQuality;
    public Point dmPosition;
    public int dmDisplayOrientation;
    public int dmDisplayFixedOutput;
    public int dmColor;
    public int dmDuplex;
    public int dmYResolution;
    public int dmTTOption;
    public int dmCollate;
    public string dmFormName;
    public int dmLogPixels;
    public int dmBitsPerPel;
    public int dmPelsWidth;
    public int dmPelsHeight;
    public int dmDisplayFlags;
    public int dmNup;
    public int dmDisplayFrequency;
    public int dmICMMethod;
    public int dmICMIntent;
    public int dmMediaType;
    public int dmDitherType;
    public int dmReserved1;
    public int dmReserved2;
    public int dmPanningWidth;
    public int dmPanningHeight;


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100