StringAlignment StringAlignment StringAlignment StringAlignment Enum

定义

指定文本字符串相对于其布局矩形的对齐方式。Specifies the alignment of a text string relative to its layout rectangle.

public enum class StringAlignment
public enum StringAlignment
type StringAlignment = 
Public Enum StringAlignment
继承
StringAlignmentStringAlignmentStringAlignmentStringAlignment

字段

Center Center Center Center 1

指定文本在布局矩形中居中对齐。Specifies that text is aligned in the center of the layout rectangle.

Far Far Far Far 2

指定文本远离布局矩形的原点位置对齐。Specifies that text is aligned far from the origin position of the layout rectangle. 在左到右布局中,远端位置是右。In a left-to-right layout, the far position is right. 在右到左布局中,远端位置是左。In a right-to-left layout, the far position is left.

Near Near Near Near 0

指定文本靠近布局对齐。Specifies the text be aligned near the layout. 在左到右布局中,近端位置是左。In a left-to-right layout, the near position is left. 在右到左布局中,近端位置是右。In a right-to-left layout, the near position is right.

示例

下面的代码示例演示如何使用LineAlignmentAlignment属性以及StringAlignment枚举来对齐字符串。The following code example demonstrates how to use the LineAlignment and Alignment properties and the StringAlignment enumeration to align strings. 此示例旨在与 Windows 窗体一起使用。This example is designed to be used with Windows Forms. 将代码粘贴到窗体中, 并ShowLineAndAlignment在处理Paint窗体事件时调用方法, ePaintEventArgs将作为传递。Paste the code into a form and call the ShowLineAndAlignment method when handling the form's Paint event, passing e as 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属性一起使用时, 此枚举设置所绘制的字符串的垂直对齐方式。When used with the LineAlignment property, this enumeration sets the vertical alignment for a drawn string. Alignment属性一起使用时, 此枚举设置水平对齐方式。When used with the Alignment property, this enumeration sets the horizontal alignment.

适用于