StringFormat.Alignment 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置字符串的水平对齐方式。
public:
property System::Drawing::StringAlignment Alignment { System::Drawing::StringAlignment get(); void set(System::Drawing::StringAlignment value); };
public System.Drawing.StringAlignment Alignment { get; set; }
member this.Alignment : System.Drawing.StringAlignment with get, set
Public Property Alignment As StringAlignment
属性值
一个 StringAlignment 枚举,指定字符串的水平对齐方式。
示例
下面的代码示例演示如何使用 LineAlignment 和 Alignment 属性和 StringAlignment 枚举来对齐字符串。 此示例旨在与 Windows 窗体 一起使用。 将代码粘贴到窗体中,并在处理窗体的 Paint 事件时调用 ShowLineAndAlignment
方法,作为 e
PaintEventArgs传递。
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.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 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
注解
使用 LineAlignment 指定字符串的垂直对齐方式。