Delen via


Rect Struct

Definition

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

public value class Rect
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.FoundationContract, 65536)]
struct Rect
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.FoundationContract), 65536)]
public struct Rect
var rect = {
x : /* Your value */,
y : /* Your value */,
width : /* Your value */,
height : /* Your value */
}
Public Structure Rect
<object property="x,y,width,height"/>
-or-
<object property="x y width height"/>

Inheritance
Rect
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.FoundationContract (introduced in v1.0)

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

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

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. See Rect in the .NET API Browser.

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

This table shows the equivalent methods available in .NET and C++.

.NET (Rect) C++ (RectHelper)
Rect(Point, Point) FromPoints(Point, Point)
Rect(Point, Size) FromLocationAndSize(Point, Size)
Rect(Double, Double, Double, Double) FromCoordinatesAndDimensions(Single, Single, Single, Single)
Bottom GetBottom(Rect)
Empty Empty
IsEmpty GetIsEmpty(Rect)
Left GetLeft(Rect)
Right GetRight(Rect)
Top GetTop(Rect)
Contains(Point) Contains(Rect, Point)
Equals Equals(Rect, Rect)
Intersect(Rect) Intersect(Rect, Rect)
Union(Point) Union(Rect, Point)
Union(Rect) Union(Rect, Rect)

Fields

Height

The height of the rectangle, in pixels.

Width

The width of the rectangle, in pixels.

X

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

Y

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

Applies to

See also