Graphics.DrawRectangle 方法

定义

绘制由坐标对、宽度和高度指定的矩形。

重载

DrawRectangle(Pen, Int32, Int32, Int32, Int32)

绘制由坐标对、宽度和高度指定的矩形。

DrawRectangle(Pen, Single, Single, Single, Single)

绘制由坐标对、宽度和高度指定的矩形。

DrawRectangle(Pen, RectangleF)

绘制指定矩形的轮廓。

DrawRectangle(Pen, Rectangle)

绘制由 Rectangle 结构指定的矩形。

DrawRectangle(Pen, Int32, Int32, Int32, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

绘制由坐标对、宽度和高度指定的矩形。

public:
 void DrawRectangle(System::Drawing::Pen ^ pen, int x, int y, int width, int height);
public void DrawRectangle (System.Drawing.Pen pen, int x, int y, int width, int height);
member this.DrawRectangle : System.Drawing.Pen * int * int * int * int -> unit
Public Sub DrawRectangle (pen As Pen, x As Integer, y As Integer, width As Integer, height As Integer)

参数

pen
Pen

确定矩形的颜色、宽度和样式的 Pen

x
Int32

要绘制的矩形左上角的 x 坐标。

y
Int32

要绘制的矩形左上角的 y 坐标。

width
Int32

要绘制的矩形的宽度。

height
Int32

要绘制的矩形的高度。

例外

pen null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建黑色笔。

  • 创建矩形的位置和大小。

  • 将矩形绘制到屏幕。

public:
   void DrawRectangleInt( PaintEventArgs^ e )
   {
      // Create pen.
      Pen^ blackPen = gcnew Pen( Color::Black,3.0f );

      // Create location and size of rectangle.
      int x = 0;
      int y = 0;
      int width = 200;
      int height = 200;

      // Draw rectangle to screen.
      e->Graphics->DrawRectangle( blackPen, x, y, width, height );
   }
public void DrawRectangleInt(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create location and size of rectangle.
    int x = 0;
    int y = 0;
    int width = 200;
    int height = 200;
             
    // Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);
}
Public Sub DrawRectangleInt(ByVal e As PaintEventArgs)

    ' Create pen.
    Dim blackPen As New Pen(Color.Black, 3)

    ' Create location and size of rectangle.
    Dim x As Integer = 0
    Dim y As Integer = 0
    Dim width As Integer = 200
    Dim height As Integer = 200

    ' Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, x, y, width, height)
End Sub

注解

有关如何绘制 RectangleF的信息,请参阅 DrawRectangles(Pen, RectangleF[])

适用于

DrawRectangle(Pen, Single, Single, Single, Single)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

绘制由坐标对、宽度和高度指定的矩形。

public:
 void DrawRectangle(System::Drawing::Pen ^ pen, float x, float y, float width, float height);
public void DrawRectangle (System.Drawing.Pen pen, float x, float y, float width, float height);
member this.DrawRectangle : System.Drawing.Pen * single * single * single * single -> unit
Public Sub DrawRectangle (pen As Pen, x As Single, y As Single, width As Single, height As Single)

参数

pen
Pen

确定矩形的颜色、宽度和样式的 Pen

x
Single

要绘制的矩形左上角的 x 坐标。

y
Single

要绘制的矩形左上角的 y 坐标。

width
Single

要绘制的矩形的宽度。

height
Single

要绘制的矩形的高度。

例外

pen null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建黑色笔。

  • 创建矩形的位置和大小。

  • 将矩形绘制到屏幕。

public:
   void DrawRectangleFloat( PaintEventArgs^ e )
   {
      // Create pen.
      Pen^ blackPen = gcnew Pen( Color::Black,3.0f );

      // Create location and size of rectangle.
      float x = 0.0F;
      float y = 0.0F;
      float width = 200.0F;
      float height = 200.0F;

      // Draw rectangle to screen.
      e->Graphics->DrawRectangle( blackPen, x, y, width, height );
   }
public void DrawRectangleFloat(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create location and size of rectangle.
    float x = 0.0F;
    float y = 0.0F;
    float width = 200.0F;
    float height = 200.0F;
             
    // Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);
}
Public Sub DrawRectangleFloat(ByVal e As PaintEventArgs)

    ' Create pen.
    Dim blackPen As New Pen(Color.Black, 3)

    ' Create location and size of rectangle.
    Dim x As Single = 0.0F
    Dim y As Single = 0.0F
    Dim width As Single = 200.0F
    Dim height As Single = 200.0F

    ' Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, x, y, width, height)
End Sub

注解

有关如何绘制 RectangleF的信息,请参阅 DrawRectangles(Pen, RectangleF[])

适用于

DrawRectangle(Pen, RectangleF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

绘制指定矩形的轮廓。

public:
 void DrawRectangle(System::Drawing::Pen ^ pen, System::Drawing::RectangleF rect);
public void DrawRectangle (System.Drawing.Pen pen, System.Drawing.RectangleF rect);
member this.DrawRectangle : System.Drawing.Pen * System.Drawing.RectangleF -> unit
Public Sub DrawRectangle (pen As Pen, rect As RectangleF)

参数

pen
Pen

确定矩形的颜色、宽度和样式的笔。

rect
RectangleF

要绘制的矩形。

适用于

DrawRectangle(Pen, Rectangle)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

绘制由 Rectangle 结构指定的矩形。

public:
 void DrawRectangle(System::Drawing::Pen ^ pen, System::Drawing::Rectangle rect);
public void DrawRectangle (System.Drawing.Pen pen, System.Drawing.Rectangle rect);
member this.DrawRectangle : System.Drawing.Pen * System.Drawing.Rectangle -> unit
Public Sub DrawRectangle (pen As Pen, rect As Rectangle)

参数

pen
Pen

确定矩形的颜色、宽度和样式的 Pen

rect
Rectangle

表示要绘制的矩形的 Rectangle 结构。

例外

pen null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建黑色笔。

  • 创建一个矩形。

  • 将矩形绘制到屏幕。

public:
   void DrawRectangleRectangle( PaintEventArgs^ e )
   {
      // Create pen.
      Pen^ blackPen = gcnew Pen( Color::Black,3.0f );

      // Create rectangle.
      Rectangle rect = Rectangle(0,0,200,200);

      // Draw rectangle to screen.
      e->Graphics->DrawRectangle( blackPen, rect );
   }
public void DrawRectangleRectangle(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create rectangle.
    Rectangle rect = new Rectangle(0, 0, 200, 200);
             
    // Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, rect);
}
Public Sub DrawRectangleRectangle(ByVal e As PaintEventArgs)

    ' Create pen.
    Dim blackPen As New Pen(Color.Black, 3)

    ' Create rectangle.
    Dim rect As New Rectangle(0, 0, 200, 200)

    ' Draw rectangle to screen.
    e.Graphics.DrawRectangle(blackPen, rect)
End Sub

注解

有关如何绘制 RectangleF的信息,请参阅 DrawRectangles(Pen, RectangleF[])

适用于