Rect Estructura

Definición

Describe el ancho, el alto y la ubicación de un rectángulo.

public value class Rect : IFormattable
[System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))]
[System.Serializable]
public struct Rect : IFormattable
[System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))]
public struct Rect : IFormattable
[<System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))>]
[<System.Serializable>]
type Rect = struct
    interface IFormattable
[<System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))>]
type Rect = struct
    interface IFormattable
Public Structure Rect
Implements IFormattable
Herencia
Atributos
Implementaciones

Ejemplos

En el ejemplo siguiente se muestra cómo usar una Rect estructura para especificar las dimensiones y la ubicación de un rectángulo mediante XAML.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
    public partial class RectExample : Page
    {
        public RectExample()
        {   
            Path myPath1 = new Path();
            myPath1.Stroke = Brushes.Black;
            myPath1.StrokeThickness = 1;
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255);
            myPath1.Fill = mySolidColorBrush;

            // Create the rectangle.
            // This RectangleGeometry specifies a rectangle that is 100 pixels high and
            // 150 wide. The left side of the rectangle is 10 pixels from the left of the 
            // Canvas and the top side of the rectangle is 100 pixels from the top of the Canvas.  
            // Note: You could alternatively use the Rect Constructor to create this:
            // Rect my Rect1 = new Rect(10,100,150,100");
            Rect myRect1 = new Rect();
            myRect1.X = 10;
            myRect1.Y = 100;
            myRect1.Width = 150;
            myRect1.Height = 100;
            RectangleGeometry myRectangleGeometry1 = new RectangleGeometry();
            myRectangleGeometry1.Rect = myRect1;

            GeometryGroup myGeometryGroup1 = new GeometryGroup();
            myGeometryGroup1.Children.Add(myRectangleGeometry1);

            myPath1.Data = myGeometryGroup1;

            Path myPath2 = new Path();
            myPath2.Stroke = Brushes.Black;
            myPath2.StrokeThickness = 1;
            myPath2.Fill = mySolidColorBrush;

            // Create the rectangle.
            // This Rect uses the Size property to specify a height of 50 and width
            // of 200. The Location property uses a Point value to determine the location of the
            // top-left corner of the rectangle.
            Rect myRect2 = new Rect();
            myRect2.Size = new Size(50, 200);
            myRect2.Location = new Point(300, 100);
            RectangleGeometry myRectangleGeometry2 = new RectangleGeometry();
            myRectangleGeometry2.Rect = myRect2;

            GeometryGroup myGeometryGroup2 = new GeometryGroup();
            myGeometryGroup2.Children.Add(myRectangleGeometry2);

            myPath2.Data = myGeometryGroup2;

            // Add path shape to the UI.
            Canvas myCanvas = new Canvas();
            myCanvas.Children.Add(myPath1);
            myCanvas.Children.Add(myPath2);
            this.Content = myCanvas;       
        }
    }
}

Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes

Namespace SDKSample
    Partial Public Class RectExample
        Inherits Page
        Public Sub New()
            Dim myPath1 As New Path()
            myPath1.Stroke = Brushes.Black
            myPath1.StrokeThickness = 1
            Dim mySolidColorBrush As New SolidColorBrush()
            mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255)
            myPath1.Fill = mySolidColorBrush

            ' Create the rectangle.
            ' This RectangleGeometry specifies a rectangle that is 100 pixels high and
            ' 150 wide. The left side of the rectangle is 10 pixels from the left of the 
            ' Canvas and the top side of the rectangle is 100 pixels from the top of the Canvas.  
            ' Note: You could alternatively use the Rect Constructor to create this:
            ' Dim myRect1 As New Rect(10,100,150,100")
            Dim myRect1 As New Rect()
            myRect1.X = 10
            myRect1.Y = 100
            myRect1.Width = 150
            myRect1.Height = 100
            Dim myRectangleGeometry1 As New RectangleGeometry()
            myRectangleGeometry1.Rect = myRect1

            Dim myGeometryGroup1 As New GeometryGroup()
            myGeometryGroup1.Children.Add(myRectangleGeometry1)

            myPath1.Data = myGeometryGroup1

            Dim myPath2 As New Path()
            myPath2.Stroke = Brushes.Black
            myPath2.StrokeThickness = 1
            myPath2.Fill = mySolidColorBrush

            ' Create the rectangle.
            ' This Rect uses the Size property to specify a height of 50 and width
            ' of 200. The Location property uses a Point value to determine the location of the
            ' top-left corner of the rectangle.
            Dim myRect2 As New Rect()
            myRect2.Size = New Size(50, 200)
            myRect2.Location = New Point(300, 100)
            Dim myRectangleGeometry2 As New RectangleGeometry()
            myRectangleGeometry2.Rect = myRect2

            Dim myGeometryGroup2 As New GeometryGroup()
            myGeometryGroup2.Children.Add(myRectangleGeometry2)

            myPath2.Data = myGeometryGroup2

            ' Add path shape to the UI.
            Dim myCanvas As New Canvas()
            myCanvas.Children.Add(myPath1)
            myCanvas.Children.Add(myPath2)
            Me.Content = myCanvas
        End Sub
    End Class

End Namespace
<Page  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas>
    
    <!-- This rectangle demonstrates using the X, Y, Width, and Height properties
         of a Rect object. -->
    <Path Stroke="Black" StrokeThickness="1" Fill="LemonChiffon">
      <Path.Data>

        <!-- This RectangleGeometry specifies a rectangle that is 100 pixels high and
             150 wide. The left side of the rectangle is 10 pixels from the left of the 
             Canvas and the top side of the rectangle is 100 pixels from the top of the Canvas.  
             Note: An abbreviated syntax for creating an equivalent rectangle is:
             <RectangleGeometry Rect="10,100,150,100" /> -->
        <RectangleGeometry>
          <RectangleGeometry.Rect>
            <Rect X="10" Y="100" Width="150" Height="100" />
          </RectangleGeometry.Rect>
        </RectangleGeometry>
      </Path.Data>
    </Path>

    <!-- This rectangle demonstrates using the Size and Location properties of a Rect object. -->
    <Path Stroke="Black" StrokeThickness="1" Fill="LemonChiffon">
      <Path.Data>

        <!-- This RectangleGeometry uses the Size property to specify a height of 50 and width
             of 200. The Location property uses a Point value to determine the location of the
             top-left corner of the rectangle. /> -->
        <RectangleGeometry>
          <RectangleGeometry.Rect>
            <Rect Size="50,200" Location="300,100" />
          </RectangleGeometry.Rect>
        </RectangleGeometry>
      </Path.Data>
    </Path>
  </Canvas>
</Page>

En el ejemplo siguiente se muestra cómo usar código para crear un rectángulo y agregarlo a la página. En el ejemplo también se muestra cómo buscar información de tamaño y coordenadas sobre el nuevo rectángulo y representar la información en un TextBox debajo del rectángulo.

// Create a rectangle and add it to the page. Also,
// find size and coordinate information about this
// new rectangle and render information in a TextBox 
// below the rectangle.
private StackPanel createRectExample1()
{
    // Initialize new rectangle.
    Rect myRectangle = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. Set the Location property to an X coordinate of 10 and a
    // Y coordinate of 5. 
    myRectangle.Location = new Point(10, 5);

    // Set the Size property of the rectangle with a width of 200
    // and a height of 50.
    myRectangle.Size = new Size(200, 50);

    RectangleGeometry myRectangleGeometry = new RectangleGeometry();
    myRectangleGeometry.Rect = myRectangle;

    // This path is defined by the rectangle.
    Path myPath = new Path();
    myPath.Fill = Brushes.LemonChiffon;
    myPath.Stroke = Brushes.Black;
    myPath.StrokeThickness = 1;
    myPath.Data = myRectangleGeometry;

    //////////// Create string of rectangle property information /////////////
    // This string will contain all the size and coordinate property
    // information about the rectangle.
    /////////////////////////////////////////////////////////////////////////
    string rectInfo = "Rectangle Property Information: ";

    // Bottom property gets the y-axis value of the bottom of the rectangle. 
    // For this rectangle the value is 55.
    rectInfo = rectInfo + "Bottom: " + myRectangle.Bottom;

    // BottomLeft property gets the coordinates of the bottom left corner of the rectangle. 
    // For this rectangle the value is 10,55.
    rectInfo = rectInfo + "| BottomLeft: " + myRectangle.BottomLeft;

    // BottomRight property gets the coordinates of the bottom right corner of the rectangle. 
    // For this rectangle the value is 210,55.
    rectInfo = rectInfo + "| BottomRight: " + myRectangle.BottomRight;

    // Height property gets or sets the height of the rectangle. 
    // For this rectangle the value is 50.
    rectInfo = rectInfo + "| Height: " + myRectangle.Height;

    // Width property gets or sets the width of the rectangle. 
    // For this rectangle the value is 200.
    rectInfo = rectInfo + "| Width: " + myRectangle.Width;

    // Left property gets the x-axis position of the left side of the rectangle which is 
    // equivalent to getting the rectangle's X property. 
    // For this rectangle the value is 10.
    rectInfo = rectInfo + "| Left: " + myRectangle.Left;

    // Location property gets or sets the position of the rectangle's top-left corner.
    // For this rectangle the value is 10,5.
    rectInfo = rectInfo + "| Location: " + myRectangle.Location;

    // Right property gets the x-axis value of the right side of the rectangle. 
    // For this rectangle the value is 210.
    rectInfo = rectInfo + "| Right: " + myRectangle.Right;

    // Size property gets or sets the width and height of the rectangle.  
    // For this rectangle the value is 200,50.
    rectInfo = rectInfo + "| Size: " + myRectangle.Size;

    // Top property gets the y-axis position of the top of the rectangle which is 
    // equivalent to getting the rectangle's Y property.
    // For this rectangle the value is 5.
    rectInfo = rectInfo + "| Top: " + myRectangle.Top;

    // TopLeft property gets the position of the top-left corner of the rectangle, which 
    // is equivalent to (X, Y).   
    // For this rectangle the value is 10,5.
    rectInfo = rectInfo + "| TopLeft: " + myRectangle.TopLeft;

    // TopRight property gets the position of the top-left corner of the rectangle, which 
    // is equivalent to (X + Width, Y).   
    // For this rectangle the value is 210,5.
    rectInfo = rectInfo + "| TopRight: " + myRectangle.TopRight;

    // X property gets or sets the location of the rectangle's left side.  
    // For this rectangle the value is 10.
    rectInfo = rectInfo + "| X: " + myRectangle.X;

    // Y property gets or sets the location of the rectangle's top side.  
    // For this rectangle the value is 5.
    rectInfo = rectInfo + "| Y: " + myRectangle.Y;

    //////// End of creating string containing rectangle property information ////////

    // This StackPanel will contain the rectangle and TextBlock.
    StackPanel parentPanel = new StackPanel();

    // Add the rectangle path to the StackPanel. This will display the rectangle.
    parentPanel.Children.Add(myPath);

    // Add a TextBlock to display the rectangle's size and coordinate information.
    TextBlock myTextBlock = new TextBlock();
    myTextBlock.Text = rectInfo;
    parentPanel.Children.Add(myTextBlock);

    // Return the parent container to be displayed to the screen.
    return parentPanel;
}

Comentarios

Uso de atributos XAML

<object property="x,y,width,height"/>  

Valores XAML

x
System.Double

Ubicación de coordenada x del lado izquierdo del rectángulo.

y
System.Double

Ubicación de coordenada y del lado superior del rectángulo.

width
System.Double

Valor no negativo que representa el Width del rectángulo.

height
System.Double

Valor no negativo que representa el Height del rectángulo.

Constructores

Rect(Double, Double, Double, Double)

Inicializa una nueva instancia de la estructura Rect que tiene las coordenadas X e Y especificadas, así como el ancho y alto especificados.

Rect(Point, Point)

Inicializa una nueva instancia de la estructura Rect que tiene el largo exacto para contener los dos puntos especificados.

Rect(Point, Size)

Inicializa una nueva instancia de la estructura Rect que tiene la ubicación de la esquina superior izquierda especificada, así como el ancho y alto especificados.

Rect(Point, Vector)

Inicializa una nueva instancia de la estructura Rect que tiene el largo exacto para contener el punto especificado, la suma del punto y el vector especificados.

Rect(Size)

Inicializa una nueva instancia de la estructura Rect que tiene el tamaño especificado y se ubica en (0,0).

Propiedades

Bottom

Obtiene el valor del eje Y de la parte inferior del rectángulo.

BottomLeft

Obtiene la posición de la esquina inferior izquierda del rectángulo.

BottomRight

Obtiene la posición de la esquina inferior derecha del rectángulo.

Empty

Obtiene un valor especial que representa un rectángulo sin posición ni área.

Height

Obtiene o establece la altura del rectángulo.

IsEmpty

Obtiene un valor que indica si el rectángulo es el rectángulo de la propiedad Empty.

Left

Obtiene el valor del eje X del lado izquierdo del rectángulo.

Location

Obtiene o establece la posición de la esquina superior izquierda del rectángulo.

Right

Obtiene el valor del eje X del lado derecho del rectángulo.

Size

Obtiene o establece el alto y el ancho del rectángulo.

Top

Obtiene la posición del eje Y de la parte superior del rectángulo.

TopLeft

Obtiene la posición de la esquina superior izquierda del rectángulo.

TopRight

Obtiene la posición de la esquina superior derecha del rectángulo.

Width

Obtiene o establece el ancho del rectángulo.

X

Obtiene o establece el valor del eje X del lado izquierdo del rectángulo.

Y

Obtiene o establece el valor del eje Y del lado superior del rectángulo.

Métodos

Contains(Double, Double)

Indica si el rectángulo contiene las coordenadas X e Y especificadas.

Contains(Point)

Indica si el rectángulo contiene el punto especificado.

Contains(Rect)

Indica si el rectángulo contiene el rectángulo especificado.

Equals(Object)

Indica si el objeto especificado es igual al rectángulo actual.

Equals(Rect)

Indica si el rectángulo especificado es igual al rectángulo actual.

Equals(Rect, Rect)

Indica si los rectángulos especificados son iguales.

GetHashCode()

Crea un código hash para el rectángulo.

Inflate(Double, Double)

Se expande o reduce el rectángulo con el ancho y el alto especificados, en todas las direcciones.

Inflate(Rect, Double, Double)

Crea un rectángulo que es el resultado de expandir o reducir el rectángulo especificado por el ancho y el alto especificados, en todas las direcciones.

Inflate(Rect, Size)

Devuelve el rectángulo que es el resultado de expandir el rectángulo especificado por la estructura Size especificada, en todas las direcciones.

Inflate(Size)

Expande el rectángulo utilizando la estructura Sizeespecificada, en todas las direcciones.

Intersect(Rect)

Encuentra la intersección del rectángulo actual y el rectángulo especificado y almacena el resultado como el rectángulo actual.

Intersect(Rect, Rect)

Devuelve la intersección de los rectángulos especificados.

IntersectsWith(Rect)

Indica si el rectángulo especificado se interseca con el rectángulo actual.

Offset(Double, Double)

Mueve el rectángulo las cantidades horizontal y vertical especificadas.

Offset(Rect, Double, Double)

Devuelve un rectángulo que se desplaza desde el rectángulo especificado usando las cantidades horizontal y vertical especificadas.

Offset(Rect, Vector)

Devuelve un rectángulo que se desplaza desde el rectángulo especificado mediante el vector especificado.

Offset(Vector)

Mueve el rectángulo el vector especificado.

Parse(String)

Crea un nuevo rectángulo a partir de la representación de cadena especificada.

Scale(Double, Double)

Multiplica el tamaño del rectángulo actual por los valores x e y especificados.

ToString()

Devuelve una representación de cadena del rectángulo.

ToString(IFormatProvider)

Devuelve una representación de cadena del rectángulo usando el proveedor de formato especificado.

Transform(Matrix)

Transforma el rectángulo aplicando la matriz especificada.

Transform(Rect, Matrix)

Devuelve el rectángulo que es el resultado de aplicar la matriz especificada al rectángulo especificado.

Union(Point)

Expande el rectángulo actual lo suficiente para contener el punto especificado.

Union(Rect)

Expande el rectángulo actual lo suficiente para contener el rectángulo especificado.

Union(Rect, Point)

Crea un rectángulo que es lo bastante grande como para incluir el rectángulo y el punto especificados.

Union(Rect, Rect)

Crea un rectángulo que es lo bastante grande como para contener los dos rectángulos especificados.

Operadores

Equality(Rect, Rect)

Compara dos rectángulos para determinar si son exactamente iguales.

Inequality(Rect, Rect)

Compara dos rectángulos para determinar si no son iguales.

Implementaciones de interfaz explícitas

IFormattable.ToString(String, IFormatProvider)

Da formato al valor de la instancia actual usando el formato especificado.

Se aplica a