次の方法で共有


方法: Windows フォームでグラフィックスを印刷する

多くの場合、Windows ベースのアプリケーションでグラフィックスを印刷する必要があります。 Graphics クラスは、画面やプリンターなどのデバイスにオブジェクトを描画するためのメソッドを提供します。

グラフィックスを印刷するには

  1. PrintDocument コンポーネントをフォームに追加します。

  2. PrintPage イベント ハンドラーで、Graphics クラスの PrintPageEventArgs プロパティを使用して、印刷するグラフィックスの種類をプリンターに指示します。

    次のコード例は、外接する四角形内に青い楕円を作成するために使用されるイベント ハンドラーを示しています。 四角形の位置と寸法は、幅が 250、高さが 250 の 100、150 から始まります。

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
       e.Graphics.FillEllipse(Brushes.Blue, New Rectangle(100, 150, 250, 250))
    End Sub
    
    private void printDocument1_PrintPage(object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
    {
       e.Graphics.FillRectangle(Brushes.Blue,
         new Rectangle(100, 150, 250, 250));
    }
    
    private:
       void printDocument1_PrintPage(System::Object ^ sender,
          System::Drawing::Printing::PrintPageEventArgs ^ e)
       {
          e->Graphics->FillRectangle(Brushes::Blue,
             Rectangle(100, 150, 250, 250));
       }
    

    (Visual C# と Visual C++)フォームのコンストラクターに次のコードを配置して、イベント ハンドラーを登録します。

    this.printDocument1.PrintPage += new
       System.Drawing.Printing.PrintPageEventHandler
       (this.printDocument1_PrintPage);
    
    this->printDocument1->PrintPage += gcnew
       System::Drawing::Printing::PrintPageEventHandler
       (this, &Form1::printDocument1_PrintPage);
    

こちらも参照ください