Bewerken

Share via


PrintDialog Class

Definition

Lets users select a printer and choose which sections of the document to print from a Windows Forms application.

public ref class PrintDialog sealed : System::Windows::Forms::CommonDialog
public sealed class PrintDialog : System.Windows.Forms.CommonDialog
type PrintDialog = class
    inherit CommonDialog
Public NotInheritable Class PrintDialog
Inherits CommonDialog
Inheritance

Examples

The following code example demonstrates how to use the PrintDialog control to set the AllowSomePages, ShowHelp, and Document properties. To run this example, paste the following code into a form that contains a PrintDialog control named PrintDialog1 and a button named Button1. This example requires that the button's Click event and the PrintPage event of docToPrint have been connected to the event-handling methods defined in this example.

// Declare the PrintDocument object.
System::Drawing::Printing::PrintDocument^ docToPrint;

// This method will set properties on the PrintDialog object and
// then display the dialog.
void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   // Allow the user to choose the page range he or she would
   // like to print.
   PrintDialog1->AllowSomePages = true;
   
   // Show the help button.
   PrintDialog1->ShowHelp = true;
   
   // Set the Document property to the PrintDocument for 
   // which the PrintPage Event has been handled. To display the
   // dialog, either this property or the PrinterSettings property 
   // must be set 
   PrintDialog1->Document = docToPrint;
   if ( docToPrint == nullptr )
         System::Windows::Forms::MessageBox::Show(  "null" );

   ;
   ;
   if ( PrintDialog1 == nullptr )
         System::Windows::Forms::MessageBox::Show(  "pnull" );

   ;
   ;
   System::Windows::Forms::DialogResult result = PrintDialog1->ShowDialog();
   System::Windows::Forms::MessageBox::Show( result.ToString() );
   ;
   ;
   
   // If the result is OK then print the document.
   if ( result == ::DialogResult::OK )
   {
      docToPrint->Print();
   }

}

// The PrintDialog will print the document
// by handling the document's PrintPage event.
void document_PrintPage( Object^ /*sender*/, System::Drawing::Printing::PrintPageEventArgs^ e )
{
   // Insert code to render the page here.
   // This code will be called when the control is drawn.
   // The following code will render a simple
   // message on the printed document.
   String^ text = "In document_PrintPage method.";
   System::Drawing::Font^ printFont = gcnew System::Drawing::Font( "Arial",35,System::Drawing::FontStyle::Regular );
   
   // Draw the content.
   e->Graphics->DrawString( text, printFont, System::Drawing::Brushes::Black, 10, 10 );
}

// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint = 
    new System.Drawing.Printing.PrintDocument();

// This method will set properties on the PrintDialog object and
// then display the dialog.
private void Button1_Click(System.Object sender, 
    System.EventArgs e)
{

    // Allow the user to choose the page range he or she would
    // like to print.
    PrintDialog1.AllowSomePages = true;

    // Show the help button.
    PrintDialog1.ShowHelp = true;

    // Set the Document property to the PrintDocument for 
    // which the PrintPage Event has been handled. To display the
    // dialog, either this property or the PrinterSettings property 
    // must be set 
    PrintDialog1.Document = docToPrint;

    DialogResult result = PrintDialog1.ShowDialog();

    // If the result is OK then print the document.
    if (result==DialogResult.OK)
    {
        docToPrint.Print();
    }
}

// The PrintDialog will print the document
// by handling the document's PrintPage event.
private void document_PrintPage(object sender, 
    System.Drawing.Printing.PrintPageEventArgs e)
{

    // Insert code to render the page here.
    // This code will be called when the control is drawn.

    // The following code will render a simple
    // message on the printed document.
    string text = "In document_PrintPage method.";
    System.Drawing.Font printFont = new System.Drawing.Font
        ("Arial", 35, System.Drawing.FontStyle.Regular);

    // Draw the content.
    e.Graphics.DrawString(text, printFont, 
        System.Drawing.Brushes.Black, 10, 10);
}

' Declare the PrintDocument object.
Private WithEvents docToPrint As New Printing.PrintDocument

' This method will set properties on the PrintDialog object and
' then display the dialog.
Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    ' Allow the user to choose the page range he or she would
    ' like to print.
    PrintDialog1.AllowSomePages = True

    ' Show the help button.
    PrintDialog1.ShowHelp = True

    ' Set the Document property to the PrintDocument for 
    ' which the PrintPage Event has been handled. To display the
    ' dialog, either this property or the PrinterSettings property 
    ' must be set 
    PrintDialog1.Document = docToPrint

    Dim result As DialogResult = PrintDialog1.ShowDialog()

    ' If the result is OK then print the document.
    If (result = DialogResult.OK) Then
        docToPrint.Print()
    End If

End Sub

' The PrintDialog will print the document
' by handling the document's PrintPage event.
Private Sub document_PrintPage(ByVal sender As Object, _
   ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
       Handles docToPrint.PrintPage

    ' Insert code to render the page here.
    ' This code will be called when the control is drawn.

    ' The following code will render a simple
    ' message on the printed document.
    Dim text As String = "In document_PrintPage method."
    Dim printFont As New System.Drawing.Font _
        ("Arial", 35, System.Drawing.FontStyle.Regular)

    ' Draw the content.
    e.Graphics.DrawString(text, printFont, _
        System.Drawing.Brushes.Black, 10, 10)
End Sub

Remarks

When you create an instance of PrintDialog, the read/write properties are set to initial values. For a list of these values, see the PrintDialog constructor. To get printer settings that are modified by the user with the PrintDialog, use the PrinterSettings property.

For more information about printing with Windows Forms, see the System.Drawing.Printing namespace overview. If you want to print from a Windows Presentation Foundation application, see the System.Printing namespace.

Constructors

PrintDialog()

Initializes a new instance of the PrintDialog class.

Properties

AllowCurrentPage

Gets or sets a value indicating whether the Current Page option button is displayed.

AllowPrintToFile

Gets or sets a value indicating whether the Print to file check box is enabled.

AllowSelection

Gets or sets a value indicating whether the Selection option button is enabled.

AllowSomePages

Gets or sets a value indicating whether the Pages option button is enabled.

CanRaiseEvents

Gets a value indicating whether the component can raise an event.

(Inherited from Component)
Container

Gets the IContainer that contains the Component.

(Inherited from Component)
DesignMode

Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
Document

Gets or sets a value indicating the PrintDocument used to obtain PrinterSettings.

Events

Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
PrinterSettings

Gets or sets the printer settings the dialog box modifies.

PrintToFile

Gets or sets a value indicating whether the Print to file check box is selected.

ShowHelp

Gets or sets a value indicating whether the Help button is displayed.

ShowNetwork

Gets or sets a value indicating whether the Network button is displayed.

Site

Gets or sets the ISite of the Component.

(Inherited from Component)
Tag

Gets or sets an object that contains data about the control.

(Inherited from CommonDialog)
UseEXDialog

Gets or sets a value indicating whether the dialog should be shown in the Windows XP style for systems running Windows XP Home Edition, Windows XP Professional, Windows Server 2003 or later.

Methods

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose()

Releases all resources used by the Component.

(Inherited from Component)
Dispose(Boolean)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(Inherited from Component)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetService(Type)

Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
HookProc(IntPtr, Int32, IntPtr, IntPtr)

Defines the common dialog box hook procedure that is overridden to add specific functionality to a common dialog box.

(Inherited from CommonDialog)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
OnHelpRequest(EventArgs)

Raises the HelpRequest event.

(Inherited from CommonDialog)
OwnerWndProc(IntPtr, Int32, IntPtr, IntPtr)

Defines the owner window procedure that is overridden to add specific functionality to a common dialog box.

(Inherited from CommonDialog)
Reset()

Resets all options, the last selected printer, and the page settings to their default values.

RunDialog(IntPtr)

When overridden in a derived class, specifies a common dialog box.

(Inherited from CommonDialog)
ShowDialog()

Runs a common dialog box with a default owner.

(Inherited from CommonDialog)
ShowDialog(IWin32Window)

Runs a common dialog box with the specified owner.

(Inherited from CommonDialog)
ToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.

(Inherited from Component)

Events

Disposed

Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)
HelpRequest

Occurs when the user clicks the Help button on a common dialog box.

(Inherited from CommonDialog)

Applies to

See also