Margins.Top 属性
获取或设置上边距宽度(以百分之一英寸为单位)。
**命名空间:**System.Drawing.Printing
**程序集:**System.Drawing(在 system.drawing.dll 中)
语法
声明
Public Property Top As Integer
用法
Dim instance As Margins
Dim value As Integer
value = instance.Top
instance.Top = value
public int Top { get; set; }
public:
property int Top {
int get ();
void set (int value);
}
/** @property */
public int get_Top ()
/** @property */
public void set_Top (int value)
public function get Top () : int
public function set Top (value : int)
属性值
上边距宽度(以百分之一英寸为单位)。
异常
异常类型 | 条件 |
---|---|
Top 属性被设置为一个小于 0 的值。 |
备注
如果以毫米为单位测量边距,则将需要的以毫米为单位的边距宽度乘以 3.937,以确定以百分之一英寸为单位的正确尺寸。例如,如果需要 25 毫米的边距,则将它乘以 3.937,舍去小数之后结果为 98。随后将适当的 Margins 成员设置为 98。
示例
在此示例中使用 System.Drawing、System.Drawing.Printing 和 System.IO 命名空间。
下面的代码示例设置文档的默认页面设置:左边距和右边距宽度为 1 英寸,上边距和下边距宽度为 1.5 英寸。
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
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);
}
}
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.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);
// Set the left and right margins to 1 inch.
pd.get_DefaultPageSettings().get_Margins().set_Left(100);
pd.get_DefaultPageSettings().get_Margins().set_Right(100);
// Set the top and bottom margins to 1.5 inches.
pd.get_DefaultPageSettings().get_Margins().set_Top(150);
pd.get_DefaultPageSettings().get_Margins().set_Bottom(150);
pd.Print();
}
finally {
streamToPrint.Close();
}
}
catch (System.Exception ex) {
MessageBox.Show(("An error occurred printing the file - "
+ ex.get_Message()));
}
} //Printing
平台
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 类
Margins 成员
System.Drawing.Printing 命名空间
Margins.Left 属性
Margins.Right 属性
Margins.Bottom 属性