Windows Dev Center

Rect structure

Contains number values that represent the location and size of a rectangle.

Syntax


Public Structure Rect


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

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, in pixels.

Width | width [C++, JavaScript]

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

The width of the rectangle, in pixels.

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 have an indeterminate frame of reference (it depends on how this value is applied) but they often represent a point in the coordinate space of the main app window.

JavaScript:  In JavaScript, a Rect is an object with four data members: 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's not commonly used.

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 4 components in the string.

A Rect element cannot be declared as a resource in a XAML ResourceDictionary because Rect is not a shareable type (for more info see ResourceDictionary and XAML resource references).

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. .NET supports this functionality through the System.Runtime.WindowsRuntime.dll interop assembly that's shipped as part of .NET for Windows Runtime apps. C++/CX supports it through the language-specific platform.winmd metadata and the runtime behavior it represents.

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. For more info on WRL, see WRL Integration.

Rect values and how they are interpreted by properties

Size is a similar structure that uses the same metaphor of 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, particularly for adaptive layout (where the positioning is often automatic).

The typical behavior of a Rect value when applied to 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 behaves best when the point locations and unit measures don't use sub-pixel values.

Requirements (Windows 10 device family)

Device family

Universal

API contract

Windows.Foundation.FoundationContract, introduced version 1.0

Namespace

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

Metadata

Windows.Foundation.FoundationContract.winmd

Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8

Namespace

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

Metadata

Windows.winmd

Attributes

[Version(0x06020000)]

See also

Size
RectHelper

 

 

Show:
© 2015 Microsoft