Pen 构造函数

定义

用指定颜色初始化 Pen 类的新实例。

重载

Pen(Brush)

使用指定的 Pen 初始化 Brush 类的新实例。

Pen(Color)

用指定颜色初始化 Pen 类的新实例。

Pen(Brush, Single)

使用指定的 PenBrush 初始化 Width 类的新实例。

Pen(Color, Single)

用指定的 PenColor 属性初始化 Width 类的新实例。

Pen(Brush)

Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs

使用指定的 Pen 初始化 Brush 类的新实例。

public:
 Pen(System::Drawing::Brush ^ brush);
public Pen (System.Drawing.Brush brush);
new System.Drawing.Pen : System.Drawing.Brush -> System.Drawing.Pen
Public Sub New (brush As Brush)

参数

brush
Brush

一个 Brush,确定该 Pen 的填充属性。

例外

brushnull

示例

下面的代码示例演示如何使用 构造 ,PenBrush以及设置 LineJoinPen属性的效果。

此示例旨在与 Windows 窗体 一起使用。 将代码粘贴到窗体中, ShowLineJoin 并在处理窗体 Paint 的事件时调用 方法,作为 ePaintEventArgs传递。

private:
   void ShowLineJoin( PaintEventArgs^ e )
   {
      // Create a new pen.
      Pen^ skyBluePen = gcnew Pen( Brushes::DeepSkyBlue );

      // Set the pen's width.
      skyBluePen->Width = 8.0F;

      // Set the LineJoin property.
      skyBluePen->LineJoin = System::Drawing::Drawing2D::LineJoin::Bevel;

      // Draw a rectangle.
      e->Graphics->DrawRectangle( skyBluePen, Rectangle(40,40,150,200) );

      //Dispose of the pen.
      delete skyBluePen;
   }
private void ShowLineJoin(PaintEventArgs e)
{

    // Create a new pen.
    Pen skyBluePen = new Pen(Brushes.DeepSkyBlue);

    // Set the pen's width.
    skyBluePen.Width = 8.0F;

    // Set the LineJoin property.
    skyBluePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

    // Draw a rectangle.
    e.Graphics.DrawRectangle(skyBluePen, 
        new Rectangle(40, 40, 150, 200));

    //Dispose of the pen.
    skyBluePen.Dispose();
}
Private Sub ShowLineJoin(ByVal e As PaintEventArgs)

    ' Create a new pen.
    Dim skyBluePen As New Pen(Brushes.DeepSkyBlue)

    ' Set the pen's width.
    skyBluePen.Width = 8.0F

    ' Set the LineJoin property.
    skyBluePen.LineJoin = Drawing2D.LineJoin.Bevel

    ' Draw a rectangle.
    e.Graphics.DrawRectangle(skyBluePen, _
        New Rectangle(40, 40, 150, 200))

    'Dispose of the pen.
    skyBluePen.Dispose()

End Sub

注解

属性 Brush 确定 绘制线条的方式 Pen 。 绘制线条就像是填充矩形一样,具有指定 Brush的特征。

WidthPen 的 属性设置为 1, (默认) 。

适用于

Pen(Color)

Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs

用指定颜色初始化 Pen 类的新实例。

public:
 Pen(System::Drawing::Color color);
public Pen (System.Drawing.Color color);
new System.Drawing.Pen : System.Drawing.Color -> System.Drawing.Pen
Public Sub New (color As Color)

参数

color
Color

Color 结构,指示此 Pen 的颜色。

注解

属性 Color 设置为 参数 color 指定的颜色。 属性 Width 设置为 1, (默认) 。

适用于

Pen(Brush, Single)

Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs

使用指定的 PenBrush 初始化 Width 类的新实例。

public:
 Pen(System::Drawing::Brush ^ brush, float width);
public Pen (System.Drawing.Brush brush, float width);
new System.Drawing.Pen : System.Drawing.Brush * single -> System.Drawing.Pen
Public Sub New (brush As Brush, width As Single)

参数

brush
Brush

一个 Brush,决定此 Pen 的特征。

width
Single

Pen 的宽度。

例外

brushnull

示例

下面的代码示例创建 并Pen演示在 上Pen设置 StartCapEndCap 属性的效果。

此示例旨在与 Windows 窗体 一起使用。 将代码粘贴到窗体中, ShowStartAndEndCaps 并在处理窗体 Paint 的事件时调用 方法,作为 ePaintEventArgs传递。

private:
   void Button3_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      Graphics^ buttonGraphics = Button3->CreateGraphics();
      Pen^ myPen = gcnew Pen( Color::ForestGreen,4.0F );
      myPen->DashStyle = System::Drawing::Drawing2D::DashStyle::DashDotDot;
      Rectangle theRectangle = Button3->ClientRectangle;
      theRectangle.Inflate(  -2, -2 );
      buttonGraphics->DrawRectangle( myPen, theRectangle );
      delete buttonGraphics;
      delete myPen;
   }
private void Button3_Click(System.Object sender, System.EventArgs e)
{

    Graphics buttonGraphics = Button3.CreateGraphics();
    Pen myPen = new Pen(Color.ForestGreen, 4.0F);
    myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;

    Rectangle theRectangle = Button3.ClientRectangle;
    theRectangle.Inflate(-2, -2);
    buttonGraphics.DrawRectangle(myPen, theRectangle);
    buttonGraphics.Dispose();
    myPen.Dispose();
}
Private Sub Button3_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button3.Click

    Dim buttonGraphics As Graphics = Button3.CreateGraphics()
    Dim myPen As Pen = New Pen(Color.ForestGreen, 4.0F)
    myPen.DashStyle = Drawing2D.DashStyle.DashDotDot

    Dim theRectangle As Rectangle = Button3.ClientRectangle
    theRectangle.Inflate(-2, -2)
    buttonGraphics.DrawRectangle(myPen, theRectangle)
    buttonGraphics.Dispose()
    myPen.Dispose()
End Sub

注解

设置为 Brush 参数中指定的 brush 颜色,属性 Width 设置为 参数中指定的 width 值,单位设置为 World

请注意, brush 参数还指定 ColorPen的 属性。

如果此值为 0,则设备单位的宽度始终为 1 像素, 它不受对用于 的 Graphics 对象的 Pen 缩放转换操作的影响。

适用于

Pen(Color, Single)

Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs

用指定的 PenColor 属性初始化 Width 类的新实例。

public:
 Pen(System::Drawing::Color color, float width);
public Pen (System.Drawing.Color color, float width);
new System.Drawing.Pen : System.Drawing.Color * single -> System.Drawing.Pen
Public Sub New (color As Color, width As Single)

参数

color
Color

Color 结构,指示此 Pen 的颜色。

width
Single

指示此 Pen 的宽度的值。

示例

下面的代码示例演示如何创建 以及 Pen 设置 DashCapDashPatternSmoothingMode 属性的效果。

此示例旨在与 Windows 窗体 一起使用。 将代码粘贴到窗体中,并在处理窗体Paint的事件时调用 ShowPensAndSmoothingMode 方法,并将 e 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

注解

属性 Color 设置为 参数 color 指定的颜色。 属性 Width 设置为 参数中指定的 width 值。 如果此值为 0,则设备单位的宽度始终为 1 像素, 它不受对用于 的 Graphics 对象的 Pen 缩放转换操作的影响。

适用于