Pen 建構函式

定義

使用指定的色彩,初始化 Pen 類別的新執行個體。

多載

Pen(Brush)

使用指定的 Pen 初始化 Brush 類別的新執行個體。

Pen(Color)

使用指定的色彩,初始化 Pen 類別的新執行個體。

Pen(Brush, Single)

使用指定的 PenBrush,初始化 Width 類別的新執行個體。

Pen(Color, Single)

使用指定的 ColorWidth 屬性,初始化 Pen 類別的新執行個體。

Pen(Brush)

來源:
Pen.cs
來源:
Pen.cs
來源:
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以及在 上Pen設定 LineJoin 屬性的效果。

此範例的設計目的是要與 Windows Forms 搭配使用。 將程式代碼貼到表單中,並在處理表單的事件時呼叫 ShowLineJoin 方法,並e傳遞為 PaintEventArgsPaint

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的特性。

Width新的 Pen 屬性會設定為 1, (預設) 。

適用於

Pen(Color)

來源:
Pen.cs
來源:
Pen.cs
來源:
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)

來源:
Pen.cs
來源:
Pen.cs
來源:
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 Forms 搭配使用。 將程式代碼貼到表單中,並在處理表單的事件時呼叫 ShowStartAndEndCaps 方法,並e傳遞為 PaintEventArgsPaint

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 參數也會指定 Color 這個 Pen的屬性。

如果此值為 0,則裝置單位的寬度一律為 1 像素, 不會受到用於 之 Graphics 物件的 Pen 縮放轉換作業影響。

適用於

Pen(Color, Single)

來源:
Pen.cs
來源:
Pen.cs
來源:
Pen.cs

使用指定的 ColorWidth 屬性,初始化 Pen 類別的新執行個體。

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 Forms 搭配使用。 將程式代碼貼到表單中,並在處理表單的事件時呼叫 ShowPensAndSmoothingMode 方法,並將 e 傳遞為 PaintEventArgsPaint

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 縮放轉換作業影響。

適用於