PrintDialog (Clase)
Actualización: noviembre 2007
Permite a los usuarios seleccionar una impresora y elegir qué secciones del documento se van a imprimir desde una aplicación de formularios Windows Forms.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
Cuando se crea una instancia de PrintDialog, se establecen las propiedades de lectura y escritura en sus valores iniciales. Para obtener una lista de esos valores, vea el constructor PrintDialog. Para obtener la configuración de impresora que el usuario ha modificado con PrintDialog, utilice la propiedad PrinterSettings.
Nota:
|
|---|
|
Esta clase quizá no funcione en los microprocesadores AMD64 a menos que se establezca la propiedad UseEXDialog en true. |
Para obtener más información sobre cómo imprimir con formularios Windows Forms, vea la información general relacionada con el espacio de nombres System.Drawing.Printing. Si desea imprimir desde una aplicación de Windows Presentation Foundation, vea el espacio de nombres System.Printing.
En el ejemplo de código siguiente se muestra cómo utilizar el control PrintDialog para establecer las propiedades AllowSomePages, ShowHelp y Document. Para ejecutar este ejemplo, pegue el siguiente código en un formulario que contiene un control PrintDialog denominado PrintDialog1 y un botón denominado Button1. En este ejemplo se requiere que el evento Click del botón y el evento PrintPage de docToPrint se hayan conectado a los métodos de control de eventos definidos en este ejemplo.
// 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 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(Object sender, System.EventArgs e)
{
// Allow the user to choose the page range he or she would
// like to print.
printDialog1.set_AllowSomePages(true);
// Show the help button.
printDialog1.set_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.set_Document(docToPrint);
DialogResult result = printDialog1.ShowDialog();
// If the result is OK then print the document.
if (result.Equals(DialogResult.OK)) {
docToPrint.Print();
}
} //button1_Click
// 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.get_Graphics().DrawString(text, printFont,
System.Drawing.Brushes.get_Black(), 10, 10);
} //Document_PrintPage
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.CommonDialog
System.Windows.Forms.PrintDialog
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.
Nota: