Click here to Skip to main content
15,030,781 members
Articles / Programming Languages / C#
Posted 7 Jun 2006


14 bookmarked

How to Show a Popup Window for Printer Properties

Rate me:
Please Sign up or sign in to vote.
2.20/5 (8 votes)
7 Jun 2006CPOL1 min read
This article introduces a method to show a popup window for printer properties.

Sample Image - PrinterPropertiesWindow.jpg


Generally, we use the System.Windows.Forms.PrintDialog class for a printer popup widow and we click the "Properties" button in the printer popup window in order to show a printer properties popup window. But we are occasionally asked to show a printer properties popup window directly.

See the following source code...

Add namespaces

Add the namespaces System.Drawing.Printing and System.Runtime.InteropServices.

using System.Drawing.Printing;
using System.Runtime.InteropServices;

DLL Interop Code

We use a DLL and driver file for the printer properties popup window (winspool.Drv, kernel32.dll). Paste the following code in your class (Form or user control or custom class):

[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true,
            ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter,
            [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName,
            IntPtr pDevModeOutput, ref IntPtr pDevModeInput, int fMode);

static extern IntPtr GlobalLock(IntPtr hMem);

static extern bool GlobalUnlock(IntPtr hMem);

static extern bool GlobalFree(IntPtr hMem);

private void OpenPrinterPropertiesDialog(PrinterSettings printerSettings)
    IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
    IntPtr pDevMode = GlobalLock(hDevMode);
    int sizeNeeded = DocumentProperties(this.Handle, IntPtr.Zero, 
                        printerSettings.PrinterName, pDevMode, ref pDevMode, 0);
    IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
    DocumentProperties(this.Handle, IntPtr.Zero, 
            printerSettings.PrinterName, devModeData, ref pDevMode, 14);

Using the OpenPrinterPropertiesDialog Method

A instance of PrinterSettings has your default printer's setting values. If you want to assign another printer's setting values to an instance of PrinterSettings, then remove the following code's comment symbol:

//ps.PrinterName = System.Drawing.Printing.PrinterSettings.InstalledPrinters[1];

"System.Drawing.Printing.PrinterSettings.InstalledPrinters" is a collection of printer names. If you put a printer name to the PrinterName property of a PrinterSettings instance, you'll have that printer's setting values.

Finally, you should write a try ... catch block because the above code line may cause errors.

You can write the following code in your button control's event handler:

private void button1_Click(object sender, System.EventArgs e)
        PrinterSettings ps = new PrinterSettings();
        // ps.PrinterName = System.Drawing.Printing.PrinterSettings.InstalledPrinters[1];
   catch(Exception ex)
       MessageBox.Show("Printer settings are incorrect.", "ERROR", 

Thank you very much.

Be happy!


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


About the Author

Korea (Republic of) Korea (Republic of)

Comments and Discussions

QuestionWorking code after some modifications Pin
donbeto.com14-Sep-17 4:53
Memberdonbeto.com14-Sep-17 4:53 
SuggestionOutOfMemory Exception on 64 bit machines (Interop Problem) Pin
Member 816231219-Aug-15 20:18
MemberMember 816231219-Aug-15 20:18 
There may be a problem using this code on 64 bit machines. The solution is placed here

To ensure that the code runs on x86 AND x64, I needed to change the call to DocumentProperties as follows

int nSize = DocumentProperties(this.Handle, IntPtr.Zero, psSettings.PrinterName, IntPtr.Zero, pDevMode, 0);

GeneralMy vote of 5 Pin
Eugen Mihaescu11-May-12 0:30
MemberEugen Mihaescu11-May-12 0:30 
GeneralSHInvokePrinterCommand(PRINTACTION_PROPERTIES) Pin
kdemuth3-Nov-10 1:29
Memberkdemuth3-Nov-10 1:29 
QuestionI want to specify a particular tab to show when Printer Preferences dialog box appears, how can i specify this? Pin
i2c8-Jan-10 2:33
Memberi2c8-Jan-10 2:33 
QuestionStill getting error Pin
sanjubaba8221-Nov-07 22:17
Membersanjubaba8221-Nov-07 22:17 
AnswerRe: Still getting error Pin
cipriansteclaru18-May-09 9:05
Membercipriansteclaru18-May-09 9:05 
GeneralWin 2K Pin
pravin dingore2-Jan-07 18:44
Memberpravin dingore2-Jan-07 18:44 
QuestionAre you sure? Pin
DSops9-Nov-06 5:10
MemberDSops9-Nov-06 5:10 
AnswerRe: Are you sure? Pin
BartJoy2-Apr-07 18:56
MemberBartJoy2-Apr-07 18:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.