Rect structure

Applies to Windows and Windows Phone

Contains a set of four floating-point numbers that represent the location and size of a rectangle.

Syntax


var rect = {
    height : /* Your value */,
    width : /* Your value */,
    x : /* Your value */,
    y : /* Your value */
}


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


XAML Values

x

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

y

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

width

A value that represents the Width of the rectangle.

height

A value that represents the Height of the rectangle.

Attributes

[Version(0x06020000)]

Members

The Rect structure has these types of members:

Constructors

The Rect structure has these constructors.

ConstructorDescription
Rect(Double, Double, Double, Double) [C#, VB]Initializes a Rect structure that has the specified x-coordinate, y-coordinate, width, and height.
Rect(float, float, float, float) [C++, JavaScript]Initializes a Rect structure that has the specified x-coordinate, y-coordinate, width, and height.
Rect(Point, Point) Initializes a Rect structure that is exactly large enough to contain the two specified points.
Rect(Point, Size) Initializes a Rect structure based on an origin and size.

 

Fields

The Rect structure has these fields.

FieldData typeDescription
Height | height [C++, JavaScript]

Number [JavaScript] | System.Single [.NET] | float32 [C++]

The height of the rectangle.

Width | width [C++, JavaScript]

Number [JavaScript] | System.Single [.NET] | float32 [C++]

The width of the rectangle.

X | x [C++, JavaScript]

Number [JavaScript] | System.Single [.NET] | float32 [C++]

The x-coordinate of the upper-left corner of the rectangle.

Y | y [C++, JavaScript]

Number [JavaScript] | System.Single [.NET] | float32 [C++]

The y-coordinate of the upper-left corner of the rectangle.

 

Methods

The Rect structure has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
Contains Indicates whether the rectangle described by the Rect contains the specified point.
Equals(Object) [C#, VB]Determines whether the specified object is equal to a Rect.
Equals(Rect) [C#, VB]Compares two Rect structures for equality.
GetHashCode [C#, VB]Gets a hash code for this object.
Intersect Finds the intersection of the rectangle represented by the current Rect and the rectangle represented by the specified Rect, and stores the result as the current Rect.
IntersectsWith [C++, JavaScript]Indicates whether the specified rectangle intersects with the current rectangle.
ToString [C#, VB]Converts a Rect to a String representation.
ToString(IFormatProvider) [C#, VB]Creates a String representation of this Rect.
Union(Point) Expands the rectangle represented by the current Rect exactly enough to contain the specified point.
Union(Rect) Expands the rectangle represented by the current Rect exactly enough to contain the specified rectangle.

 

Operators

The Rect structure has these operators.

OperatorDescription
Equality Compares two Rect structures for equality.
Inequality Compares two Rect structures for inequality.

 

Properties

The Rect structure has these properties.

PropertyAccess typeDescription

Bottom

Read-onlyGets the y-axis value of the bottom of the rectangle.

Empty

Read-onlyGets a special value that represents a rectangle with no position or area.

Height

Read/writeGets or sets the height of the rectangle.

IsEmpty

Read-onlyGets a value that indicates whether the rectangle is the Empty rectangle.

Left

Read-onlyGets the x-axis value of the left side of the rectangle.

Right

Read-onlyGets the x-axis value of the right side of the rectangle.

Top

Read-onlyGets the y-axis position of the top of the rectangle.

Width

Read/writeGets or sets the width of the rectangle.

X

Read/writeGets or sets the x-axis value of the left side of the rectangle.

Y

Read/writeGets or sets the y-axis value of the top side of the rectangle.

 

Remarks

When applied to properties that represent UI information, the width and height values of a Rect value represent device-independent pixels. The x and y values represent a point in the coordinate space of the main app window.

JavaScript:  In JavaScript, a Rect is an object with four data memberr: x, y, width, height. Other than the data members (fields), the APIs listed in the Rect members lists don't apply to JavaScript programming.

Notes on XAML syntax

Object attribute usage is technically permitted by the Windows Runtime XAML parsing rules. However, that usage isn't shown because it isn't typical.

You can use a space rather than a comma as the delimiter between values. Mixed delimiter usages are also permitted.

You must specify all four values. "0,0" is not an implicit default for "x,y", and no other conventions exist that can parse less than four values in the string.

A Rect element cannot be declared as a resource in a ResourceDictionary because Rect is not a shareable type.

Language projection and members of Rect

If you are using a Microsoft .NET language (C# or Microsoft Visual Basic) or Visual C++ component extensions (C++/CX), then Rect has non-data members available, and its data members are exposed as read-write properties, not fields.

If you are programming with C++ using the Windows Template Runtime Library (WRL), then only the data member fields exist as members of Rect, and you cannot use the utility methods or properties listed in the members table. WRL code can access similar utility methods that are available from the RectHelper static class.

Rect values and how they are interpreted by properties

Size is a similar structure that uses the same metaphor of specifying a height and width. However, a Rect specifies an origin point, whereas a Size does not. Typically Rect is used for graphics library APIs, and Size is used for presentation and layout in app UI.

The typical behavior of a Rect value as it is interpreted by properties follows these principles:

  • Width and Height cannot be negative.
  • X and Y can be negative, with the result that the Rect definition is outside the coordinate frame of reference unless there is additional translation.
  • X and Y can be 0. A value of 0 is also valid for Width or Height. However, if a Rect with a value of 0 for either Width or Height is applied to a RectangleGeometry, the resulting RectangleGeometry does not render.
  • X, Y, Width and Height can be non-integer values in terms of permitted values. However, you typically use integer values. XAML UI definitions work and look best when the point locations and unit measures don't use sub-pixel values.

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8

Namespace

Windows.Foundation
Windows::Foundation [C++]

Metadata

Platform.winmd
Windows.winmd

Assembly

System.Runtime.WindowsRuntime.dll

See also

Size
RectHelper

 

 

Show:
© 2014 Microsoft. All rights reserved.