StringAlignment 枚举
指定文本字符串相对于其布局矩形的对齐方式。
**命名空间:**System.Drawing
**程序集:**System.Drawing(在 system.drawing.dll 中)
语法
声明
Public Enumeration StringAlignment
用法
Dim instance As StringAlignment
public enum StringAlignment
public enum class StringAlignment
public enum StringAlignment
public enum StringAlignment
成员
成员名称 | 说明 | |
---|---|---|
Center | 指定文本在布局矩形中居中对齐。 | |
Far | 指定文本远离布局矩形的原点位置对齐。在左到右布局中,远端位置是右。在右到左布局中,远端位置是左。 | |
Near | 指定文本靠近布局对齐。在左到右布局中,近端位置是左。在右到左布局中,近端位置是右。 |
备注
当用于 LineAlignment 属性时,此枚举设置绘制字符串的垂直对齐方式。当用于 Alignment 属性时,此枚举设置水平对齐方式。
示例
下面的代码示例演示如何使用 LineAlignment 和 Alignment 属性以及 StringAlignment 枚举来对齐字符串。此示例是针对使用 Windows 窗体而设计的。将代码粘贴到一个窗体中,然后在处理窗体的 Paint 事件时调用 ShowLineAndAlignment
方法,并传递 e 作为 PaintEventArgs。
Private Sub ShowLineAndAlignment(ByVal e As PaintEventArgs)
' Construct a new Rectangle.
Dim displayRectangle _
As New Rectangle(New Point(40, 40), New Size(80, 80))
' Construct two new StringFormat objects
Dim format1 As New StringFormat(StringFormatFlags.NoClip)
Dim format2 As New StringFormat(format1)
' Set the LineAlignment and Alignment properties for
' both StringFormat objects to different values.
format1.LineAlignment = StringAlignment.Near
format1.Alignment = StringAlignment.Center
format2.LineAlignment = StringAlignment.Center
format2.Alignment = StringAlignment.Far
' Draw the bounding rectangle and a string for each
' StringFormat object.
e.Graphics.DrawRectangle(Pens.Black, displayRectangle)
e.Graphics.DrawString("Showing Format1", Me.Font, Brushes.Red, _
RectangleF.op_Implicit(displayRectangle), format1)
e.Graphics.DrawString("Showing Format2", Me.Font, Brushes.Red, _
RectangleF.op_Implicit(displayRectangle), format2)
End Sub
private void ShowLineAndAlignment(PaintEventArgs e)
{
// Construct a new Rectangle .
Rectangle displayRectangle =
new Rectangle (new Point(40, 40), new Size (80, 80));
// Construct 2 new StringFormat objects
StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat format2 = new StringFormat(format1);
// Set the LineAlignment and Alignment properties for
// both StringFormat objects to different values.
format1.LineAlignment = StringAlignment.Near;
format1.Alignment = StringAlignment.Center;
format2.LineAlignment = StringAlignment.Center;
format2.Alignment = StringAlignment.Far;
// Draw the bounding rectangle and a string for each
// StringFormat object.
e.Graphics.DrawRectangle(Pens.Black, displayRectangle);
e.Graphics.DrawString("Showing Format1", this.Font,
Brushes.Red, (RectangleF)displayRectangle, format1);
e.Graphics.DrawString("Showing Format2", this.Font,
Brushes.Red, (RectangleF)displayRectangle, format2);
}
private:
void ShowLineAndAlignment( PaintEventArgs^ e )
{
// Construct a new Rectangle .
Rectangle displayRectangle = Rectangle(Point(40,40),System::Drawing::Size( 80, 80 ));
// Construct 2 new StringFormat objects
StringFormat^ format1 = gcnew StringFormat( StringFormatFlags::NoClip );
StringFormat^ format2 = gcnew StringFormat( format1 );
// Set the LineAlignment and Alignment properties for
// both StringFormat objects to different values.
format1->LineAlignment = StringAlignment::Near;
format1->Alignment = StringAlignment::Center;
format2->LineAlignment = StringAlignment::Center;
format2->Alignment = StringAlignment::Far;
// Draw the bounding rectangle and a string for each
// StringFormat object.
e->Graphics->DrawRectangle( Pens::Black, displayRectangle );
e->Graphics->DrawString( "Showing Format1", this->Font, Brushes::Red, displayRectangle, format1 );
e->Graphics->DrawString( "Showing Format2", this->Font, Brushes::Red, displayRectangle, format2 );
}
private void ShowLineAndAlignment(PaintEventArgs e)
{
// Construct a new Rectangle .
Rectangle displayRectangle = new Rectangle(new Point(40, 40),
new Size(80, 80));
// Construct 2 new StringFormat objects
StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat format2 = new StringFormat(format1);
// Set the LineAlignment and Alignment properties for
// both StringFormat objects to different values.
format1.set_LineAlignment(StringAlignment.Near);
format1.set_Alignment(StringAlignment.Center);
format2.set_LineAlignment(StringAlignment.Center);
format2.set_Alignment(StringAlignment.Far);
// Draw the bounding rectangle and a string for each
// StringFormat object.
e.get_Graphics().DrawRectangle(Pens.get_Black(), displayRectangle);
e.get_Graphics().DrawString("Showing Format1", this.get_Font(),
Brushes.get_Red(), (RectangleF.op_Implicit((displayRectangle))),
format1);
e.get_Graphics().DrawString("Showing Format2", this.get_Font(),
Brushes.get_Red(), RectangleF.op_Implicit((displayRectangle)),
format2);
} //ShowLineAndAlignment
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、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
.NET Compact Framework
受以下版本支持:2.0