Click here to Skip to main content
Licence CPOL
First Posted 7 Jun 2006
Views 31,000
Downloads 393
Bookmarked 13 times

How to show a popup window for printer properties

By | 7 Jun 2006 | Article
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)

About the Author

greater@inbrein.com



Korea (Republic Of) Korea (Republic Of)

Member

I'm working for INBREIN in korea of republic
(South Korea)
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberEugen Mihaescu0:30 11 May '12  
GeneralSHInvokePrinterCommand(PRINTACTION_PROPERTIES) Pinmemberkdemuth1:29 3 Nov '10  
QuestionI want to specify a particular tab to show when Printer Preferences dialog box appears, how can i specify this? Pinmemberi2c2:33 8 Jan '10  
QuestionStill getting error Pinmembersanjubaba8222:17 21 Nov '07  
AnswerRe: Still getting error Pinmembercipriansteclaru9:05 18 May '09  
GeneralWin 2K Pinmemberpravin dingore18:44 2 Jan '07  
QuestionAre you sure? PinmemberDSops5:10 9 Nov '06  
AnswerRe: Are you sure? PinmemberBartJoy18:56 2 Apr '07  

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

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

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