Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C#

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 76K   1.6K   15   10
This article introduces a method to show a popup window for printer properties.

Sample Image - PrinterPropertiesWindow.jpg

Introduction

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.

C#
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):

C#
[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);

[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);

[DllImport("kernel32.dll")]
static extern bool GlobalUnlock(IntPtr hMem);

[DllImport("kernel32.dll")]
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);
    GlobalUnlock(hDevMode);
    printerSettings.SetHdevmode(devModeData);
    printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
    GlobalFree(hDevMode);
    Marshal.FreeHGlobal(devModeData);
}

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:

C#
//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:

C#
private void button1_Click(object sender, System.EventArgs e)
{
    try
    {
        PrinterSettings ps = new PrinterSettings();
        // ps.PrinterName = System.Drawing.Printing.PrinterSettings.InstalledPrinters[1];
        OpenPrinterPropertiesDialog(ps);
   }
   catch(Exception ex)
   {
       MessageBox.Show("Printer settings are incorrect.", "ERROR", 
                       MessageBoxButtons.OK,MessageBoxIcon.Error);
   }  
}

Thank you very much.

Be happy!

License

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


Written By
Korea (Republic of) Korea (Republic of)
DreamFactory.


Comments and Discussions

 
QuestionWorking code after some modifications Pin
donbeto.com14-Sep-17 4:53
donbeto.com14-Sep-17 4:53 
SuggestionOutOfMemory Exception on 64 bit machines (Interop Problem) Pin
Member 816231219-Aug-15 20:18
Member 816231219-Aug-15 20:18 
GeneralMy vote of 5 Pin
Eugen Mihaescu11-May-12 0:30
Eugen Mihaescu11-May-12 0:30 
GeneralSHInvokePrinterCommand(PRINTACTION_PROPERTIES) Pin
kdemuth3-Nov-10 1:29
kdemuth3-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
i2c8-Jan-10 2:33 
QuestionStill getting error Pin
sanjubaba8221-Nov-07 22:17
sanjubaba8221-Nov-07 22:17 
I tried installing printer available @ http://www.pdfcomplete.com/

But I am getting error stating with message "Attempted to read or write protected memory. This is often an indication that memory is corrupt!", when I am trying to view its properties.

Follow below steps to reproduce :-
1. install printer available at http://www.pdfcomplete.com/
2. Uncomment line ps.PrinterName = PrinterSettings.InstalledPrinters[2];
3. Run the application
4. Click on button available on form.
5. Now cancel the dialog.

Appears quite often. Confused | :confused:

Saurabh
AnswerRe: Still getting error Pin
cipriansteclaru18-May-09 9:05
cipriansteclaru18-May-09 9:05 
GeneralWin 2K Pin
pravin dingore2-Jan-07 18:44
pravin dingore2-Jan-07 18:44 
QuestionAre you sure? Pin
DSops9-Nov-06 5:10
DSops9-Nov-06 5:10 
AnswerRe: Are you sure? Pin
BartJoy2-Apr-07 18:56
BartJoy2-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.