Margins 类

指定打印页的边距尺寸。

**命名空间:**System.Drawing.Printing
**程序集:**System.Drawing(在 system.drawing.dll 中)

语法

声明
<SerializableAttribute> _
Public Class Margins
    Implements ICloneable
用法
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

备注

Margins 类用于操作 PageSettingsPrintController 中的边距宽度。MarginsConverter 是此类的类型说明符。

LeftRightTopBottom 是定义边距的属性。Clone 可创建一个相同的 MarginsEquals 确定另一对象是否具有与 Margins 相同的尺寸。

有关打印的更多信息,请参见 System.Drawing.Printing 命名空间概述。

示例

在此示例中使用 System.DrawingSystem.Drawing.PrintingSystem.IO 命名空间。

下面的代码示例将文档的默认页面设置设为各边有 1 英寸的边距。

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

继承层次结构

System.Object
  System.Drawing.Printing.Margins

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Margins 成员
System.Drawing.Printing 命名空间
PageSettings
PrintController
MarginsConverter

其他资源

Windows 窗体打印支持