Link to home
Start Free TrialLog in
Avatar of omegaomega
omegaomegaFlag for Canada

asked on

How can I open the printer "Preferences" dialog?

Hello, Experts,

I have customized PageSetup and Print dialogs that emulate the standard dialogs.  I would like to be able to open the "Printer Preferences" dialog by clicking a button (just like can be done on the standard dialogs).  

(On some Print dialogs (such as in Word and Excel) this button is labelled "Properties".  And just to avoid being too consistent, it is labelled "Options" on the Excel PageSetup dialog.)

I thought that this would be trivial, but so far I haven't been able to figure out how to do this.  Does anyone know of an easy way to open this "Printer Preferences" dialog box?

Thanks,
Randy
Avatar of RishadanPort
RishadanPort

Have you tried using the PrintDialog Class?
Why can't you just do this:

            System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
            dialog.ShowDialog();   <-- in here the user can navigate to the advanced settings.
Avatar of omegaomega

ASKER

Hello, RishadanPort,

I am using a custom PrintDialog form.  I want to emulate the functionality of the "Preferences" button on the standard PrintDialog form.

Cheers,
Randy
For anyone else who may be interested, I finally found the answer here:

http://bytes.com/forum/thread389569.html

This answer is provided by (and with my thanks to)  Bart Mermuys.  The attached snippet shows the essence of the solution.

Cheers,
Randy

Imports System.Drawing.Printing
Public Class Form1
    Private Const DM_IN_BUFFER As Integer = 8
    Private Const DM_OUT_BUFFER As Integer = 2
    Private Const DM_IN_PROMPT As Integer = 4
 
    Private Declare Auto Function GlobalLock _
        Lib "kernel32.dll" (ByVal handle As IntPtr) As IntPtr
    Private Declare Auto Function GlobalUnlock _
        Lib "kernel32.dll" (ByVal handle As IntPtr) As Integer
    Private Declare Auto Function GlobalFree _
        Lib "kernel32.dll" (ByVal handle As IntPtr) As IntPtr
    Private Declare Auto Function DocumentProperties _
        Lib "winspool.drv" (ByVal hWnd As IntPtr, _
                            ByVal hPrinter As IntPtr, _
                            ByVal pDeviceName As String, _
                            ByVal pDevModeOutput As IntPtr, _
                            ByVal pDevModeInput As IntPtr, _
                            ByVal fMode As Int32) As Integer
 
    Sub OpenPrinterPropertiesDialog(ByVal Settings As PrinterSettings)
        ' PrinterSettings+PageSettings -> hDEVMODE
        Dim hDevMode As IntPtr
        hDevMode = Settings.GetHdevmode(Settings.DefaultPageSettings)
        ' Show Dialog ( [In,Out] pDEVMODE )
        Dim pDevMode As IntPtr = GlobalLock(hDevMode)
        DocumentProperties(Me.Handle, IntPtr.Zero, _
                Settings.PrinterName, pDevMode, pDevMode, _
                DM_OUT_BUFFER Or DM_IN_BUFFER Or DM_IN_PROMPT)
        GlobalUnlock(hDevMode)
        ' hDEVMODE -> PrinterSettings+PageSettings
        Settings.SetHdevmode(hDevMode)
        Settings.DefaultPageSettings.SetHdevmode(hDevMode)
        ' cleanup
        GlobalFree(hDevMode)
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                                        Handles Button1.Click
        If (Me.PrintDocument1.PrinterSettings.IsValid) Then
            Me.ShowPrinterProperties(Me.PrintDocument1.PrinterSettings)
        Else
            MsgBox("Invalid Printer Name!", MsgBoxStyle.Information)
        End If
    End Sub
 
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of omegaomega
omegaomega
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial