PrintPageEventArgs.Graphics 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得用來繪製頁面的 Graphics。
public:
property System::Drawing::Graphics ^ Graphics { System::Drawing::Graphics ^ get(); };
public System.Drawing.Graphics Graphics { get; }
public System.Drawing.Graphics? Graphics { get; }
member this.Graphics : System.Drawing.Graphics
Public ReadOnly Property Graphics As Graphics
屬性值
用來繪製頁面的 Graphics。
範例
下列程式代碼範例假設 Button 已在 上建立具名 printButton
和 PrintDocument 具名 pd
的 Form。 請確定 Click 的 事件與 printButton_Click
方法相關聯,且 PrintPage 的 PrintDocument 事件與範例中的 方法相關聯pd_PrintPage
Button。 範例 printButton_Click
中的 方法會呼叫 Print 引發 PrintPage 事件的方法,並列印 方法中指定的 pd_PrintPage
.bmp 檔案。 若要執行此範例,請將路徑變更為您要列印的點陣圖。
針對此範例, System.Drawing請使用、 System.Drawing.Printing和 System.Windows.Forms 命名空間。
private:
// Specifies what happens when the user clicks the Button.
void printButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
try
{
pd->Print();
}
catch ( Exception^ ex )
{
MessageBox::Show( "An error occurred while printing", ex->ToString() );
}
}
// Specifies what happens when the PrintPage event is raised.
void pd_PrintPage( Object^ /*sender*/, PrintPageEventArgs^ ev )
{
// Draw a picture.
ev->Graphics->DrawImage( Image::FromFile( "C:\\My Folder\\MyFile.bmp" ),
ev->Graphics->VisibleClipBounds );
// Indicate that this is the last page to print.
ev->HasMorePages = false;
}
// Specifies what happens when the user clicks the Button.
private void printButton_Click(object sender, EventArgs e)
{
try
{
// Assumes the default printer.
pd.Print();
}
catch(Exception ex)
{
MessageBox.Show("An error occurred while printing", ex.ToString());
}
}
// Specifies what happens when the PrintPage event is raised.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
// Draw a picture.
ev.Graphics.DrawImage(Image.FromFile("C:\\My Folder\\MyFile.bmp"), ev.Graphics.VisibleClipBounds);
// Indicate that this is the last page to print.
ev.HasMorePages = false;
}
' Specifies what happens when the user clicks the Button.
Private Sub printButton_Click(sender As Object, e As EventArgs) _
Handles printButton.Click
Try
pd.Print()
Catch ex As Exception
MessageBox.Show("An error occurred while printing", _
ex.ToString())
End Try
End Sub
' Specifies what happens when the PrintPage event is raised.
Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs) _
Handles pd.PrintPage
' Draw a picture.
ev.Graphics.DrawImage(Image.FromFile("C:\My Folder\MyFile.bmp"), _
ev.Graphics.VisibleClipBounds)
' Indicate that this is the last page to print.
ev.HasMorePages = False
End Sub