Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

PrintDlg (comdlg32)
 
.
Summary
Summary
TODO - a short description

C# Signature:

[DllImport("comdlg32.dll", CharSet=CharSet.Auto)]
static extern bool PrintDlg([In,Out] PRINTDLG lppd);
private static extern bool PrintDlgEx([In(), Out()] PRINTDLGEX lppd);

VB.NET Signature:

<DllImport("comdlg32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function PrintDlg(<[In], Out> ByVal lppd As PRINTDLG) As Boolean
Private Shared Function PrintDlgEx(<[In](), Out()> ByVal lppd As PRINTDLGEX) As Boolean
End Function

User-Defined Types:

PRINTDLG

Alternative Managed API:

System.Windows.Forms.PrintDialog

Notes:

None.

Tips & Tricks:

Please add some!

Alternative Managed API:

System.Windows.Forms.PrintDialog

Sample Code:

Notes:

None.

// Call the PrintDlg api from c#

public static void InvokePrintDlg (PrinterSettings printerSettings,

    IntPtr hwndDialogOwner)

{

    // PRINTDLG that will be passed to PrintDlg API function.
    PRINTDLG printdlg = new PRINTDLG();
    printdlg.lStructSize = Marshal.SizeOf(printdlg);             
    printdlg.hwndOwner = hwndDialogOwner;            
    // Pass the handles to the DEVMODE and DEVNAMES structures from the
    // printer settings object to the PRINTDLG so that the current
    // printsettings are displayed in the print setup dialog.
    printdlg.hDevMode = printerSettings.GetHdevmode();
    printdlg.hDevNames = printerSettings.GetHdevnames();
    // Uncomment the line below to indicate that only the print setup dialog should be shown.
    //printdlg.Flags = PD_PRINTSETUP;
    // Invoke the dialog.
    bool l = PrintDlg(printdlg);
    // Pass the resulting DEVMODE and DEVNAMES structs back to the
    // caller via the PrinterSettings object that was passed in.
    printerSettings.SetHdevmode(printdlg.hDevMode);
    printerSettings.SetHdevnames(printdlg.hDevNames);         

}

Tips & Tricks:

Please add some!

VB.NET Sample Code:

Imports System.Runtime.InteropServices

Documentation
PrintDlg on MSDN

Public Class WWPrintDialog
    Private Const START_PAGE_GENERAL = &HFFFFFFFF
    Private m_bShowHelp As Boolean = False
    Private m_bShowStatusDialog As Boolean = False
    Private m_bShowNetwork As Boolean = False
    Private m_bAllowSelection As Boolean = False
    Private m_bAllowSomePages As Boolean = False
    Private m_bShowCollate As Boolean = False
    Private m_hParent As IntPtr
    Private m_bAllowPrintToFile As Boolean = False
    Private m_sPrinterName As String = String.Empty
    Private m_Copies As Integer = 1

    <DllImport("comdlg32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function PrintDlgEx(<[In](), Out()> ByVal lppd As PRINTDLGEX) As Boolean
    End Function

    Public Property Parent() As IntPtr
        Get
            Return m_hParent
        End Get
        Set(ByVal value As IntPtr)
            m_hParent = value
        End Set
    End Property

    Public Function ShowDialog() As DialogResult
        Return InvokePrintDlgEx()
    End Function

    Public Function InvokePrintDlgEx() As DialogResult
        Dim ret As Object = Nothing
        Dim _pdex As PRINTDLGEX = New PRINTDLGEX()
        Dim _ppr As PRINTPAGERANGE = New PRINTPAGERANGE()
        Dim prnsettings As Printing.PrinterSettings = New Printing.PrinterSettings()

        _pdex.lStructSize = Marshal.SizeOf(_pdex)
        _pdex.hwndOwner = m_hParent
        _pdex.lpPageRanges = Marshal.SizeOf(_ppr)
        _pdex.hDevMode = prnsettings.GetHdevmode()
        _pdex.hDevNames = prnsettings.GetHdevnames()
        _pdex.hDC = Nothing
        _pdex.lpCallback = Nothing
        _pdex.lphPropertyPages = Nothing
        _pdex.lpPageRanges = Nothing
        _pdex.lpPrintTemplateName = Nothing
        _pdex.nCopies = 1
        _pdex.nMaxPage = 2
        _pdex.nMaxPageRanges = 10
        _pdex.nMinPage = 1
        _pdex.nPageRanges = 0
        _pdex.nMaxPageRanges = 0
        _pdex.Flags = PRINTFLAG.PD_NOSELECTION Or PRINTFLAG.PD_NOPAGENUMS
        _pdex.Flags2 = 0
        _pdex.hInstance = Nothing
        _pdex.dwResultAction = 0
        _pdex.nPropertyPages = Nothing
        _pdex.nStartPage = START_PAGE_GENERAL

        Return IIf(PrintDlgEx(_pdex), DialogResult.OK, DialogResult.Cancel)
    End Function
End Class

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1), System.Runtime.InteropServices.ComVisible(False)> _
Public Class PRINTDLGEX
    Public lStructSize As Int32
    Public hwndOwner As IntPtr
    Public hDevMode As IntPtr
    Public hDevNames As IntPtr
    Public hDC As IntPtr = IntPtr.Zero
    Public Flags As Int32
    Public Flags2 As Int32
    Public ExclusionFlags As Int32
    Public nPageRanges As Int32
    Public nMaxPageRanges As Int32
    Public lpPageRanges As IntPtr
    Public nMinPage As Int32
    Public nMaxPage As Int32
    Public nCopies As Int32
    Public hInstance As IntPtr = IntPtr.Zero
    <MarshalAs(UnmanagedType.LPStr, SizeConst:=&H20)> _
    Public lpPrintTemplateName As String
    Public lpCallback As IntPtr = IntPtr.Zero
    Public nPropertyPages As Int32
    Public lphPropertyPages As IntPtr = IntPtr.Zero
    Public nStartPage As Int32
    Public dwResultAction As Int32
End Class

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1), System.Runtime.InteropServices.ComVisible(False)> _
Public Class PRINTPAGERANGE
    Public nFromPage As Int32
    Public nToPage As Int32
End Class

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1), System.Runtime.InteropServices.ComVisible(False)> _
Public Class PRINTFLAG
    Public Const PD_ALLPAGES As Int32 = 0
    Public Const PD_SELECTION As Int32 = 1
    Public Const PD_PAGENUMS As Int32 = 2
    Public Const PD_NOSELECTION As Int32 = 4
    Public Const PD_NOPAGENUMS As Int32 = 8
    Public Const PD_COLLATE As Int32 = 16
    Public Const PD_PRINTTOFILE As Int32 = 32
    Public Const PD_PRINTSETUP As Int32 = 64
    Public Const PD_NOWARNING As Int32 = 128
    Public Const PD_RETURNDC As Int32 = 256
    Public Const PD_RETURNIC As Int32 = 512
    Public Const PD_RETURNDEFAULT As Int32 = 1024
    Public Const PD_SHOWHELP As Int32 = 2048
    Public Const PD_ENABLEPRINTHOOK As Int32 = 4096
    Public Const PD_ENABLESETUPHOOK As Int32 = 8192
    Public Const PD_ENABLEPRINTTEMPLATE As Int32 = 16384
    Public Const PD_ENABLESETUPTEMPLATE As Int32 = 32768
    Public Const PD_ENABLEPRINTTEMPLATEHANDLE As Int32 = 65536
    Public Const PD_ENABLESETUPTEMPLATEHANDLE As Int32 = 131072
    Public Const PD_USEDEVMODECOPIES As Int32 = 262144
    Public Const PD_USEDEVMODECOPIESANDCOLLATE As Int32 = 262144
    Public Const PD_DISABLEPRINTTOFILE As Int32 = 524288
    Public Const PD_HIDEPRINTTOFILE As Int32 = 1048576
    Public Const PD_NONETWORKBUTTON As Int32 = 2097152
    Public Const PD_CURRENTPAGE As Int32 = 4194304
    Public Const PD_NOCURRENTPAGE As Int32 = 8388608
    Public Const PD_EXCLUSIONFLAGS As Int32 = 16777216
    Public Const PD_USELARGETEMPLATE As Int32 = 268435456
    Public Const PD_EXCL_COPIESANDCOLLATE As Int32 = (256 Or 32768)
End Class

Documentation
PrintDlgEx on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions