Click here to Skip to main content
12,509,269 members (63,312 online)
Click here to Skip to main content
Add your own
alternative version

Stats

45K views
1K downloads
14 bookmarked
Posted

How to show a popup window for printer properties

, 7 Jun 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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);

[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:

//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)
{
    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. From GREATER (greater@inbrein.com).

License

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

Share

About the Author

greater@inbrein.com
Korea (Republic of) Korea (Republic of)
I'm working for INBREIN in korea of republic
(South Korea)


You may also be interested in...

Pro
Pro

Comments and Discussions

 
SuggestionOutOfMemory Exception on 64 bit machines (Interop Problem) Pin
Member 816231219-Aug-15 20:18
memberMember 816231219-Aug-15 20:18 
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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160927.1 | Last Updated 8 Jun 2006
Article Copyright 2006 by greater@inbrein.com
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid