Graphics.DrawRectangle Método

Definição

Desenha um retângulo especificado por um par de coordenadas, uma largura e uma altura.

Sobrecargas

Name Description
DrawRectangle(Pen, Rectangle)

Desenha um retângulo especificado por uma Rectangle estrutura.

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

Desenha um retângulo especificado por um par de coordenadas, uma largura e uma altura.

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

Desenha um retângulo especificado por um par de coordenadas, uma largura e uma altura.

DrawRectangle(Pen, Rectangle)

Desenha um retângulo especificado por uma Rectangle estrutura.

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)

Parâmetros

pen
Pen

A Pen que determina a cor, largura e estilo do retângulo.

rect
Rectangle

Uma Rectangle estrutura que representa o retângulo a desenhar.

Exceções

pen é null.

Exemplos

O seguinte exemplo de código foi concebido para uso com Windows Forms e requer PaintEventArgse, que é um parâmetro do tratador de eventos Paint. O código executa as seguintes ações:

  • Cria uma caneta preta.

  • Cria um retângulo.

  • Desenha o retângulo para o ecrã.

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

Observações

Para informações sobre como desenhar um RectangleF, veja DrawRectangles(Pen, RectangleF[]).

Aplica-se a

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

Desenha um retângulo especificado por um par de coordenadas, uma largura e uma altura.

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)

Parâmetros

pen
Pen

Pen Isso determina a cor, largura e estilo do retângulo.

x
Int32

A coordenada x do canto superior esquerdo do retângulo a desenhar.

y
Int32

A coordenada y do canto superior esquerdo do retângulo a desenhar.

width
Int32

Largura do retângulo a desenhar.

height
Int32

Altura do retângulo a desenhar.

Exceções

pen é null.

Exemplos

O seguinte exemplo de código foi concebido para uso com Windows Forms e requer PaintEventArgse, que é um parâmetro do tratador de eventos Paint. O código executa as seguintes ações:

  • Cria uma caneta preta.

  • Cria a posição e o tamanho de um retângulo.

  • Desenha o retângulo para o ecrã.

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

Observações

Para informações sobre como desenhar um RectangleF, veja DrawRectangles(Pen, RectangleF[]).

Aplica-se a

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

Desenha um retângulo especificado por um par de coordenadas, uma largura e uma altura.

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)

Parâmetros

pen
Pen

A Pen que determina a cor, largura e estilo do retângulo.

x
Single

A coordenada x do canto superior esquerdo do retângulo a desenhar.

y
Single

A coordenada y do canto superior esquerdo do retângulo a desenhar.

width
Single

A largura do retângulo a desenhar.

height
Single

A altura do retângulo a desenhar.

Exceções

pen é null.

Exemplos

O seguinte exemplo de código foi concebido para uso com Windows Forms e requer PaintEventArgse, que é um parâmetro do tratador de eventos Paint. O código executa as seguintes ações:

  • Cria uma caneta preta.

  • Cria a posição e o tamanho de um retângulo.

  • Desenha o retângulo para o ecrã.

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

Observações

Para informações sobre como desenhar um RectangleF, veja DrawRectangles(Pen, RectangleF[]).

Aplica-se a