方法: Windows フォームでユーザーのコンピューターに接続されたプリンターを選択する

既定のプリンター以外のプリンターに印刷することがよくあります。 PrintDialog コンポーネントを使用すると、現在インストールされているプリンターからユーザーに選択させることができます。 PrintDialog コンポーネントでは、 DialogResult コンポーネントの PrintDialog がキャプチャされ、プリンターの選択に使用されます。

次の手順では、既定のプリンターに印刷するテキスト ファイルを選択します。 PrintDialog クラスがインスタンス化されます。

プリンターを選択してファイルを印刷するには

  1. PrintDialog コンポーネントを使用して、使用するプリンターを選択します。

    次のコード例では、2 つのイベントを処理しています。 最初の Button コントロールの Click イベントでは、PrintDialog クラスがインスタンス化され、ユーザーが選択したプリンターが DialogResult プロパティでキャプチャされます。

    2 番目の 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(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 C++) フォームのコンストラクターに次のコードを配置して、イベント ハンドラーを登録します。

    this.printDocument1.PrintPage += new  
       System.Drawing.Printing.PrintPageEventHandler  
       (this.printDocument1_PrintPage);  
    this.button1.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);  
    

関連項目