Graphics.DrawRectangle 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
绘制由坐标对、宽度和高度指定的矩形。
重载
| 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
- 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 -> unitPublic Sub DrawRectangle (pen As Pen, x As Integer, y As Integer, width As Integer, height As Integer)参数
- 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
- 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 -> unitPublic Sub DrawRectangle (pen As Pen, x As Single, y As Single, width As Single, height As Single)参数
- 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
- 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 -> unitPublic 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
- 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 -> unitPublic Sub DrawRectangle (pen As Pen, rect As 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[])。