Margins.Top 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置上边距宽度(以百分之一英寸为单位)。
public:
property int Top { int get(); void set(int value); };
public int Top { get; set; }
member this.Top : int with get, set
Public Property Top As Integer
属性值
上边距宽度(以百分之一英寸为单位)。
例外
Top 属性被设置为一个小于 0 的值。
示例
对于此示例, System.Drawing请使用 、 System.Drawing.Printing和 System.IO 命名空间。
下面的代码示例将文档的默认页面设置设置为宽度为 1 英寸的左右边距,以及宽度为 1.5 英寸的上边距和下边距。
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;
// Set the left and right margins to 1 inch.
pd->DefaultPageSettings->Margins->Left = 100;
pd->DefaultPageSettings->Margins->Right = 100;
// Set the top and bottom margins to 1.5 inches.
pd->DefaultPageSettings->Margins->Top = 150;
pd->DefaultPageSettings->Margins->Bottom = 150;
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.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;
// Set the left and right margins to 1 inch.
pd.DefaultPageSettings.Margins.Left = 100;
pd.DefaultPageSettings.Margins.Right = 100;
// Set the top and bottom margins to 1.5 inches.
pd.DefaultPageSettings.Margins.Top = 150;
pd.DefaultPageSettings.Margins.Bottom = 150;
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch(Exception ex)
{
MessageBox.Show("An error occurred printing the file - " + ex.Message);
}
}
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
' Set the left and right margins to 1 inch.
pd.DefaultPageSettings.Margins.Left = 100
pd.DefaultPageSettings.Margins.Right = 100
' Set the top and bottom margins to 1.5 inches.
pd.DefaultPageSettings.Margins.Top = 150
pd.DefaultPageSettings.Margins.Bottom = 150
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
注解
如果以毫米为单位测量边距,请将所需的边距宽度(以毫米为单位)乘以 3.937,以确定以百分之一英寸为单位的正确度量。 例如,如果希望边距为 25mm,则乘以 3.937,向下舍入时结果为 98。 然后将相应的 Margins 成员设置为 98。