Share via


作法:列印 Windows Forms 中的圖形

您通常會想要在 Windows 應用程式中列印圖形。 類別 Graphics 提供將物件繪製到裝置的方法,例如螢幕或印表機。

列印圖形

  1. PrintDocument 元件新增至表單。

  2. PrintPage 事件處理常式中,使用 Graphics 類別的 PrintPageEventArgs 屬性,指示印表機列印何種圖形。

    下列程式碼範例顯示事件處理常式,用來在周框內建立藍色橢圓形。 矩形具有下列位置和維度:從 100 開始,150 寬度為 250,高度為 250。

    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);  
    

另請參閱