PrintPageEventArgs.Graphics Właściwość

Definicja

Pobiera element Graphics używany do malowania strony.

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

Wartość właściwości

Element Graphics używany do malowania strony.

Przykłady

W poniższym przykładzie kodu przyjęto założenie Button , że nazwana printButton i PrintDocument nazwa pd została utworzona na obiekcie Form. Upewnij się, że Click zdarzenie dla Button obiektu jest skojarzone z printButton_Click metodą , a PrintPage zdarzenie PrintDocument klasy jest skojarzone z pd_PrintPage metodą w przykładzie. Metoda printButton_Click z przykładu wywołuje metodę wywołującą PrintPrintPage zdarzenie i wyświetla plik .bmp określony w metodzie pd_PrintPage . Aby uruchomić ten przykład, zmień ścieżkę na mapę bitową, którą chcesz wydrukować.

W tym przykładzie System.Drawingużyj przestrzeni nazw , System.Drawing.Printingi 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

Dotyczy

Zobacz też