Export (0) Print
Expand All

Rect Structure

Describes the width, height, and location of a rectangle.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[SerializableAttribute]
[TypeConverterAttribute(typeof(RectConverter))]
public struct Rect : IFormattable
<object property="x,y,width,height"/>

XAML Values

x

System.Double

The x-coordinate location of the left side of the rectangle.

y

System.Double

The y-coordinate location of the top side of the rectangle.

width

System.Double

A non-negative value that represents the Width of the rectangle.

height

System.Double

A non-negative value that represents the Height of the rectangle.

The Rect type exposes the following members.

  NameDescription
Public methodRect(Size)Initializes a new instance of the Rect structure that is of the specified size and is located at (0,0).
Public methodRect(Point, Point)Initializes a new instance of the Rect structure that is exactly large enough to contain the two specified points.
Public methodRect(Point, Size)Initializes a new instance of the Rect structure that has the specified top-left corner location and the specified width and height.
Public methodRect(Point, Vector)Initializes a new instance of the Rect structure that is exactly large enough to contain the specified point and the sum of the specified point and the specified vector.
Public methodRect(Double, Double, Double, Double)Initializes a new instance of the Rect structure that has the specified x-coordinate, y-coordinate, width, and height.
Top

  NameDescription
Public propertyBottomGets the y-axis value of the bottom of the rectangle.
Public propertyBottomLeftGets the position of the bottom-left corner of the rectangle
Public propertyBottomRightGets the position of the bottom-right corner of the rectangle.
Public propertyStatic memberEmptyGets a special value that represents a rectangle with no position or area.
Public propertyHeightGets or sets the height of the rectangle.
Public propertyIsEmptyGets a value that indicates whether the rectangle is the Empty rectangle.
Public propertyLeftGets the x-axis value of the left side of the rectangle.
Public propertyLocationGets or sets the position of the top-left corner of the rectangle.
Public propertyRightGets the x-axis value of the right side of the rectangle.
Public propertySizeGets or sets the width and height of the rectangle.
Public propertyTopGets the y-axis position of the top of the rectangle.
Public propertyTopLeftGets the position of the top-left corner of the rectangle.
Public propertyTopRightGets the position of the top-right corner of the rectangle.
Public propertyWidthGets or sets the width of the rectangle.
Public propertyXGets or sets the x-axis value of the left side of the rectangle.
Public propertyYGets or sets the y-axis value of the top side of the rectangle.
Top

  NameDescription
Public methodContains(Point)Indicates whether the rectangle contains the specified point.
Public methodContains(Rect)Indicates whether the rectangle contains the specified rectangle.
Public methodContains(Double, Double)Indicates whether the rectangle contains the specified x-coordinate and y-coordinate.
Public methodEquals(Object)Indicates whether the specified object is equal to the current rectangle. (Overrides ValueType.Equals(Object).)
Public methodEquals(Rect)Indicates whether the specified rectangle is equal to the current rectangle.
Public methodStatic memberEquals(Rect, Rect)Indicates whether the specified rectangles are equal.
Public methodGetHashCodeCreates a hash code for the rectangle. (Overrides ValueType.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInflate(Size)Expands the rectangle by using the specified Size, in all directions.
Public methodInflate(Double, Double)Expands or shrinks the rectangle by using the specified width and height amounts, in all directions.
Public methodStatic memberInflate(Rect, Size)Returns the rectangle that results from expanding the specified rectangle by the specified Size, in all directions.
Public methodStatic memberInflate(Rect, Double, Double)Creates a rectangle that results from expanding or shrinking the specified rectangle by the specified width and height amounts, in all directions.
Public methodIntersect(Rect)Finds the intersection of the current rectangle and the specified rectangle, and stores the result as the current rectangle.
Public methodStatic memberIntersect(Rect, Rect)Returns the intersection of the specified rectangles.
Public methodIntersectsWithIndicates whether the specified rectangle intersects with the current rectangle.
Public methodOffset(Vector)Moves the rectangle by the specified vector.
Public methodOffset(Double, Double)Moves the rectangle by the specified horizontal and vertical amounts.
Public methodStatic memberOffset(Rect, Vector)Returns a rectangle that is offset from the specified rectangle by using the specified vector.
Public methodStatic memberOffset(Rect, Double, Double)Returns a rectangle that is offset from the specified rectangle by using the specified horizontal and vertical amounts.
Public methodStatic memberParseCreates a new rectangle from the specified string representation.
Public methodScaleMultiplies the size of the current rectangle by the specified x and y values.
Public methodToString()Returns a string representation of the rectangle. (Overrides ValueType.ToString().)
Public methodToString(IFormatProvider)Returns a string representation of the rectangle by using the specified format provider.
Public methodTransform(Matrix)Transforms the rectangle by applying the specified matrix.
Public methodStatic memberTransform(Rect, Matrix)Returns the rectangle that results from applying the specified matrix to the specified rectangle.
Public methodUnion(Point)Expands the current rectangle exactly enough to contain the specified point.
Public methodUnion(Rect)Expands the current rectangle exactly enough to contain the specified rectangle.
Public methodStatic memberUnion(Rect, Point)Creates a rectangle that is exactly large enough to include the specified rectangle and the specified point.
Public methodStatic memberUnion(Rect, Rect)Creates a rectangle that is exactly large enough to contain the two specified rectangles.
Top

  NameDescription
Public operatorStatic memberEqualityCompares two rectangles for exact equality.
Public operatorStatic memberInequalityCompares two rectangles for inequality.
Top

  NameDescription
Explicit interface implemetationPrivate methodIFormattable.ToStringFormats the value of the current instance using the specified format.
Top

The following example shows how to use a Rect structure to specify the dimensions and location of a rectangle using 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;       
        }
    }

}
<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>

The following example shows how to use code to create a rectangle and add it to the page. The example also illustrates how to find size and coordinate information about the new rectangle and render the information in a TextBox below the rectangle.

// 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;
}

More Code

How to: Animate a RectangleThis example shows how to animate changes to the size and position of a rectangle.
How to: Animate a Rectangle Geometry by Using Key FramesThis example shows how to animate the Rect property of a RectangleGeometry by using key frames.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft