Freigeben über


Margins-Klasse

Gibt die Abmessungen der Ränder einer gedruckten Seite an.

Namespace: System.Drawing.Printing
Assembly: System.Drawing (in system.drawing.dll)

Syntax

'Declaration
<SerializableAttribute> _
Public Class Margins
    Implements ICloneable
'Usage
Dim instance As Margins
[SerializableAttribute] 
public class Margins : ICloneable
[SerializableAttribute] 
public ref class Margins : ICloneable
/** @attribute SerializableAttribute() */ 
public class Margins implements ICloneable
SerializableAttribute 
public class Margins implements ICloneable

Hinweise

Mit der Margins-Klasse wird die Breite der Ränder in PageSettings und PrintController geändert. Der Typdeskriptor für diese Klasse ist MarginsConverter.

Left, Right, Top und Bottom sind Eigenschaften, mit denen die Seitenränder definiert werden. Clone erstellt eine identische Instanz von Margins. Equals bestimmt, ob ein anderes Objekt die gleichen Abmessungen wie Margins besitzt.

Weitere Informationen zum Drucken finden Sie in der Übersicht über den System.Drawing.Printing-Namespace.

Beispiel

Verwenden Sie für dieses Beispiel die Namespaces System.Drawing, System.Drawing.Printing und System.IO.

Im folgenden Codebeispiel werden die Standardseiteneinstellungen für ein Dokument auf 1 Zoll für jeden Seitenrand festgelegt.

Public Sub Printing()
    Try
        ' This assumes that a variable of type string, named filePath,
        ' has been set to the path of the file to print. 
        streamToPrint = New StreamReader(filePath)
        Try
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            ' This assumes that a method, named pd_PrintPage, has been
            ' defined. pd_PrintPage handles the PrintPage event. 
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            ' This assumes that a variable of type string, named
            ' printer, has been set to the printer's name. 
            pd.PrinterSettings.PrinterName = printer
            ' Create a new instance of Margins with one inch margins.
            Dim margins As New Margins(100, 100, 100, 100)
            pd.DefaultPageSettings.Margins = margins
            pd.Print()
        Finally
            streamToPrint.Close()
        End Try
    Catch ex As Exception
        MessageBox.Show("An error occurred printing the file - " & ex.Message)
    End Try
End Sub
   
public void Printing()
{
  try 
  {
    /* This assumes that a variable of type string, named filePath,
       has been set to the path of the file to print. */
    streamToPrint = new StreamReader (filePath);
    try 
    {
      printFont = new Font("Arial", 10);
      PrintDocument pd = new PrintDocument(); 
      /* This assumes that a method, named pd_PrintPage, has been
         defined. pd_PrintPage handles the PrintPage event. */
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
      /* This assumes that a variable of type string, named 
         printer, has been set to the printer's name. */
      pd.PrinterSettings.PrinterName = printer;
      // Create a new instance of Margins with one inch margins.
      Margins margins = new Margins(100,100,100,100);
      pd.DefaultPageSettings.Margins = margins;
      pd.Print();
    } 
    finally 
    {
      streamToPrint.Close() ;
    }
  } 
  catch(Exception ex) 
  { 
    MessageBox.Show("An error occurred printing the file - " + ex.Message);
  }
}
void Printing()
{
   try
   {
      
      /* This assumes that a variable of type string, named filePath,
              has been set to the path of the file to print. */
      streamToPrint = gcnew StreamReader( filePath );
      try
      {
         printFont = gcnew System::Drawing::Font( "Arial",10 );
         PrintDocument^ pd = gcnew PrintDocument;
         
         /* This assumes that a method, named pd_PrintPage, has been
                   defined. pd_PrintPage handles the PrintPage event. */
         pd->PrintPage += gcnew PrintPageEventHandler( this, &Sample::pd_PrintPage );
         
         /* This assumes that a variable of type string, named 
                   printer, has been set to the printer's name. */
         pd->PrinterSettings->PrinterName = printer;
         
         // Create a new instance of Margins with one inch margins.
         Margins^ margins = gcnew Margins( 100,100,100,100 );
         pd->DefaultPageSettings->Margins = margins;
         pd->Print();
      }
      finally
      {
         streamToPrint->Close();
      }

   }
   catch ( Exception^ ex ) 
   {
      MessageBox::Show( String::Concat( "An error occurred printing the file - ", ex->Message ) );
   }

}

public void Printing()
{
    try {
        /* This assumes that a variable of type string, named filePath,
           has been set to the path of the file to print.
         */
        streamToPrint = new StreamReader(filePath);
        try {
            printFont = new Font("Arial", 10);
            PrintDocument pd = new PrintDocument();

            /* This assumes that a method, named pd_PrintPage, has been
               defined. pd_PrintPage handles the PrintPage event.
             */
            pd.add_PrintPage(new PrintPageEventHandler(pd_PrintPage));

            /* This assumes that a variable of type string, named 
               printer, has been set to the printer's name.
             */
            pd.get_PrinterSettings().set_PrinterName(printer);

            // Create a new instance of Margins with one inch margins.      
            Margins margins = new Margins(100, 100, 100, 100);
            pd.get_DefaultPageSettings().set_Margins(margins);
            pd.Print();
        }
        finally {
            streamToPrint.Close();
        }
    }
    catch (System.Exception ex) {
        MessageBox.Show(("An error occurred printing the file - " 
            + ex.get_Message()));
    }
} //Printing

Vererbungshierarchie

System.Object
  System.Drawing.Printing.Margins

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

Margins-Member
System.Drawing.Printing-Namespace
PageSettings
PrintController
MarginsConverter

Weitere Ressourcen

Druckunterstützung in Windows Forms