如何:在 Windows 窗体中选择连接到用户计算机的打印机

更新:2007 年 11 月

用户经常要选择使用默认打印机外的其他打印机进行打印。您可以使用户从当前使用 PrintDialog 组件安装的打印机中选择一台打印机。通过 PrintDialog 组件,可以捕获并使用 PrintDialog 组件的 DialogResult 来选择打印机。

在下面的过程中,选择将文本文件打印到默认打印机。然后实例化 PrintDialog 类。

选择打印机然后打印文件

  • 使用 PrintDialog 组件选择要使用的打印机。

    在下面的代码示例中,有两个要处理的事件。第一个事件是 Button 控件的 Click 事件,在该事件中,PrintDialog 类被实例化,并且用户选择的打印机在 DialogResult 属性中捕获。

    第二个事件是 PrintDocument 组件的 PrintPage 事件,在该事件中,将一个示例文档打印到指定的打印机。

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim PrintDialog1 As New PrintDialog()
       PrintDialog1.Document = PrintDocument1
       Dim result As DialogResult = PrintDialog1.ShowDialog()
    
       If (result = DialogResult.OK) Then
         PrintDocument1.Print()
       End If 
    
    End Sub
    
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
       e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500))        
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       PrintDialog printDialog1 = new PrintDialog();
       printDialog1.Document = printDocument1;
       DialogResult result = printDialog1.ShowDialog();
       if (result == DialogResult.OK)
       {
          printDocument1.Print();
       }
    }
    
    private void printDocument1_PrintPage(object sender, 
    System.Drawing.Printing.PrintPageEventArgs e)
    {
       e.Graphics.FillRectangle(Brushes.Red, 
         new Rectangle(500, 500, 500, 500));
    }
    
    private void button1_Click(Object sender, System.EventArgs e)
    {
       PrintDialog printDialog1 = new PrintDialog();
       printDialog1.set_Document(printDocument1);
       DialogResult result = printDialog1.ShowDialog();
       if (result == DialogResult.OK)
       {
          printDocument1.Print();
       }
    }
    
    private void printDocument1_PrintPage(Object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
    {
       e.get_Graphics().FillRectangle(Brushes.get_Red(),
          new Rectangle(500, 500, 500, 500));
    }
    
    private:
       void button1_Click(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          PrintDialog ^ printDialog1 = gcnew PrintDialog();
          printDialog1->Document = printDocument1;
          System::Windows::Forms::DialogResult result = 
             printDialog1->ShowDialog();
          if (result == DialogResult::OK)
          {
             printDocument1->Print();
          }
       }
    private:
       void printDocument1_PrintPage(System::Object ^ sender,
          System::Drawing::Printing::PrintPageEventArgs ^ e)
       {
          e->Graphics->FillRectangle(Brushes::Red,
             Rectangle(500, 500, 500, 500));
       }
    

    ((Visual C#、Visual J# 和 Visual C++)在窗体的构造函数中放入以下代码以注册事件处理程序。

    this.printDocument1.PrintPage += new
       System.Drawing.Printing.PrintPageEventHandler
       (this.printDocument1_PrintPage);
    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    this.printDocument1.add_PrintPage(new
    System.Drawing.Printing.PrintPageEventHandler
    (this.printDocument1_PrintPage));
    this.button1.add_Click(new System.EventHandler(this.button1_Click));
    
    this->printDocument1->PrintPage += gcnew
       System::Drawing::Printing::PrintPageEventHandler
       (this, &Form1::printDocument1_PrintPage);
    this->button1->Click += gcnew
       System::EventHandler(this, &Form1::button1_Click);
    

请参见

其他资源

Windows 窗体打印支持