Pen.DashPattern 属性

定义

获取或设置自定义的短划线和空白区域的数组。Gets or sets an array of custom dashes and spaces.

public:
 property cli::array <float> ^ DashPattern { cli::array <float> ^ get(); void set(cli::array <float> ^ value); };
public float[] DashPattern { get; set; }
member this.DashPattern : single[] with get, set
Public Property DashPattern As Single()

属性值

Single[]

实数数组,指定虚线中交替出现的短划线和空白区域的长度。An array of real numbers that specifies the lengths of alternating dashes and spaces in dashed lines.

例外

对不可变的 DashPattern(比如由 Pen 类所返回)设置 Pens 属性。The DashPattern property is set on an immutable Pen, such as those returned by the Pens class.

示例

下面的代码示例演示设置 DashCap 、和属性的效果 DashPattern SmoothingModeThe following code example demonstrates the effects of setting the DashCap, DashPattern, and SmoothingMode properties.

此示例旨在与 Windows 窗体一起使用。This example is designed to be used with Windows Forms. 将代码粘贴到窗体中,并 ShowPensAndSmoothingMode 在处理窗体事件时调用方法,并将 Paint e 作为传递 PaintEventArgsPaste the code into a form and call the ShowPensAndSmoothingMode method when handling the form's Paint event, passing e as PaintEventArgs.

private:
   void ShowPensAndSmoothingMode( PaintEventArgs^ e )
   {
      // Set the SmoothingMode property to smooth the line.
      e->Graphics->SmoothingMode = System::Drawing::Drawing2D::SmoothingMode::AntiAlias;

      // Create a new Pen object.
      Pen^ greenPen = gcnew Pen( Color::Green );

      // Set the width to 6.
      greenPen->Width = 6.0F;

      // Set the DashCap to round.
      greenPen->DashCap = System::Drawing::Drawing2D::DashCap::Round;

      // Create a custom dash pattern.
      array<Single>^temp0 = {4.0F,2.0F,1.0F,3.0F};
      greenPen->DashPattern = temp0;

      // Draw a line.
      e->Graphics->DrawLine( greenPen, 20.0F, 20.0F, 100.0F, 240.0F );

      // Change the SmoothingMode to none.
      e->Graphics->SmoothingMode = System::Drawing::Drawing2D::SmoothingMode::None;

      // Draw another line.
      e->Graphics->DrawLine( greenPen, 100.0F, 240.0F, 160.0F, 20.0F );

      // Dispose of the custom pen.
      delete greenPen;
   }
private void ShowPensAndSmoothingMode(PaintEventArgs e)
{

    // Set the SmoothingMode property to smooth the line.
    e.Graphics.SmoothingMode = 
        System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    // Create a new Pen object.
    Pen greenPen = new Pen(Color.Green);

    // Set the width to 6.
    greenPen.Width = 6.0F;

    // Set the DashCap to round.
    greenPen.DashCap = System.Drawing.Drawing2D.DashCap.Round;

    // Create a custom dash pattern.
    greenPen.DashPattern = new float[]{4.0F, 2.0F, 1.0F, 3.0F};

    // Draw a line.
    e.Graphics.DrawLine(greenPen, 20.0F, 20.0F, 100.0F, 240.0F);

    // Change the SmoothingMode to none.
    e.Graphics.SmoothingMode = 
        System.Drawing.Drawing2D.SmoothingMode.None;

    // Draw another line.
    e.Graphics.DrawLine(greenPen, 100.0F, 240.0F, 160.0F, 20.0F);

    // Dispose of the custom pen.
    greenPen.Dispose();
}
Private Sub ShowPensAndSmoothingMode(ByVal e As PaintEventArgs)

    ' Set the SmoothingMode property to smooth the line.
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

    ' Create a new Pen object.
    Dim greenPen As New Pen(Color.Green)

    ' Set the width to 6.
    greenPen.Width = 6.0F

    ' Set the DashCap to round.
    greenPen.DashCap = Drawing2D.DashCap.Round

    ' Create a custom dash pattern.
    greenPen.DashPattern = New Single() {4.0F, 2.0F, 1.0F, 3.0F}

    ' Draw a line.
    e.Graphics.DrawLine(greenPen, 20.0F, 20.0F, 100.0F, 240.0F)

    ' Change the SmoothingMode to none.
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.None

    ' Draw another line.
    e.Graphics.DrawLine(greenPen, 100.0F, 240.0F, 160.0F, 20.0F)

    ' Dispose of the custom pen.
    greenPen.Dispose()
End Sub

注解

如果为此属性指定的值不是 null ,则会将 DashStyle 此的属性设置 PenCustomAssigning a value other than null to this property will set the DashStyle property for this Pen to Custom.

数组中的元素 dashArray 设置破折号模式中每个短划线和空间的长度。The elements in the dashArray array set the length of each dash and space in the dash pattern. 第一个元素设置短划线的长度,第二个元素设置空格的长度,第三个元素设置短划线的长度,依此类推。The first element sets the length of a dash, the second element sets the length of a space, the third element sets the length of a dash, and so on. 因此,每个元素应为非零正数。Consequently, each element should be a non-zero positive number.

破折号模式中每个短划线和空间的长度是数组中元素值和的宽度的乘积 PenThe length of each dash and space in the dash pattern is the product of the element value in the array and the width of the Pen.

适用于