Pen.DashPattern 属性

获取或设置自定义的短划线和空白区域的数组。

**命名空间:**System.Drawing
**程序集:**System.Drawing(在 system.drawing.dll 中)

语法

声明
Public Property DashPattern As Single()
用法
Dim instance As Pen
Dim value As Single()

value = instance.DashPattern

instance.DashPattern = value
public float[] DashPattern { get; set; }
public:
property array<float>^ DashPattern {
    array<float>^ get ();
    void set (array<float>^ value);
}
/** @property */
public float[] get_DashPattern ()

/** @property */
public void set_DashPattern (float[] value)
public function get DashPattern () : float[]

public function set DashPattern (value : float[])

属性值

实数数组,指定虚线中交替出现的短划线和空白区域的长度。

备注

如果为此属性指定一个非 空引用(在 Visual Basic 中为 Nothing) 的值,会将该 PenDashStyle 属性设置为 Custom

dashArray 数组中的元素设置短划线图案中每个短划线和空白区域的长度。第一个元素设置短划线的长度,第二个元素设置空白区域的长度,第三个元素设置短划线的长度,依此类推。

虚线图案中每个短划线和空白区域的长度是数组中的元素值与 Pen 宽度的乘积。

示例

下面的代码示例演示了设置 DashCapDashPatternSmoothingMode 属性的效果。

此示例是针对使用 Windows 窗体而设计的。将代码粘贴到一个窗体中,然后在处理窗体的 Paint 事件时调用 ShowPensAndSmoothingMode 方法,并传递 e 作为 PaintEventArgs

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
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:
   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.get_Graphics().set_SmoothingMode(
        System.Drawing.Drawing2D.SmoothingMode.AntiAlias);

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

    // Set the width to 6.
    greenPen.set_Width(6);

    // Set the DashCap to round.
    greenPen.set_DashCap(System.Drawing.Drawing2D.DashCap.Round);

    // Create a custom dash pattern.
    greenPen.set_DashPattern(new float[] { 4, 2, 1, 3 });

    // Draw a line.
    e.get_Graphics().DrawLine(greenPen, 20, 20, 100, 240);

    // Change the SmoothingMode to none.
    e.get_Graphics().set_SmoothingMode(
        System.Drawing.Drawing2D.SmoothingMode.None);

    // Draw another line.
    e.get_Graphics().DrawLine(greenPen, 100, 240, 160, 20);

    // Dispose of the custom pen.
    greenPen.Dispose();
} //ShowPensAndSmoothingMode

平台

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

请参见

参考

Pen 类
Pen 成员
System.Drawing 命名空间