Rect 结构

定义

描述矩形的宽度、高度和位置。Describes the width, height, and location of a rectangle.

public value class Rect : IFormattable
[System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))]
public struct Rect : IFormattable
[System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))]
[System.Serializable]
public struct Rect : IFormattable
type Rect = struct
    interface IFormattable
Public Structure Rect
Implements IFormattable
继承
属性
实现

示例

下面的示例演示如何使用 Rect 结构通过 XAML 来指定矩形的尺寸和位置。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;       
        }
    }
}

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>

下面的示例演示如何使用代码创建一个矩形,并将其添加到页面中。The following example shows how to use code to create a rectangle and add it to the page. 该示例还演示了如何查找有关新矩形的大小和坐标信息,以及如何在矩形下面的 TextBox 中呈现信息。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;
}

注解

XAML 属性用法XAML Attribute Usage

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

XAML 值XAML Values

xx
System.Double

矩形左边的 x 坐标位置。The x-coordinate location of the left side of the rectangle.

yy
System.Double

矩形上边缘的 y 坐标位置。The y-coordinate location of the top side of the rectangle.

widthwidth
System.Double

一个非负值,表示矩形的 WidthA non-negative value that represents the Width of the rectangle.

height
System.Double

一个非负值,表示矩形的 HeightA non-negative value that represents the Height of the rectangle.

构造函数

Rect(Double, Double, Double, Double)

初始化 Rect 结构的新实例,此结构具有指定的 x 坐标、y 坐标、宽度和高度。Initializes a new instance of the Rect structure that has the specified x-coordinate, y-coordinate, width, and height.

Rect(Point, Point)

初始化 Rect 结构的新实例,此结构的大小刚好足以包含两个指定点。Initializes a new instance of the Rect structure that is exactly large enough to contain the two specified points.

Rect(Point, Size)

初始化 Rect 结构的新实例,此结构具有指定的左上角位置和指定的宽度及高度。Initializes a new instance of the Rect structure that has the specified top-left corner location and the specified width and height.

Rect(Point, Vector)

初始化 Rect 结构的新实例,此结构的大小刚好足以包含指定点及指定点和指定向量之和。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.

Rect(Size)

初始化 Rect 结构的新实例,此结构具有指定大小,位于 (0,0)。Initializes a new instance of the Rect structure that is of the specified size and is located at (0,0).

属性

Bottom

获取矩形底边的 y 轴值。Gets the y-axis value of the bottom of the rectangle.

BottomLeft

获取矩形的左下角位置。Gets the position of the bottom-left corner of the rectangle.

BottomRight

获取矩形的右下角位置。Gets the position of the bottom-right corner of the rectangle.

Empty

获取一个特殊值,该值表示没有位置或区域的矩形。Gets a special value that represents a rectangle with no position or area.

Height

获取或设置矩形的高度。Gets or sets the height of the rectangle.

IsEmpty

获取一个值,该值指示矩形是否为 Empty 矩形。Gets a value that indicates whether the rectangle is the Empty rectangle.

Left

获取矩形左边的 x 轴值。Gets the x-axis value of the left side of the rectangle.

Location

获取或设置矩形的左上角位置。Gets or sets the position of the top-left corner of the rectangle.

Right

获取矩形右边的 x 轴值。Gets the x-axis value of the right side of the rectangle.

Size

获取或设置矩形的宽度和高度。Gets or sets the width and height of the rectangle.

Top

获取矩形顶边的 y 轴位置。Gets the y-axis position of the top of the rectangle.

TopLeft

获取矩形的左上角位置。Gets the position of the top-left corner of the rectangle.

TopRight

获取矩形的右上角位置。Gets the position of the top-right corner of the rectangle.

Width

获取或设置矩形的宽度。Gets or sets the width of the rectangle.

X

获取或设置矩形左边的 x 轴值。Gets or sets the x-axis value of the left side of the rectangle.

Y

获取或设置矩形顶边的 y 轴值。Gets or sets the y-axis value of the top side of the rectangle.

方法

Contains(Double, Double)

指示矩形中是否包含指定的 x 坐标和 y 坐标。Indicates whether the rectangle contains the specified x-coordinate and y-coordinate.

Contains(Point)

指示矩形中是否包含指定点。Indicates whether the rectangle contains the specified point.

Contains(Rect)

指示矩形中是否包含指定矩形。Indicates whether the rectangle contains the specified rectangle.

Equals(Object)

指示指定对象是否等于当前矩形。Indicates whether the specified object is equal to the current rectangle.

Equals(Rect)

指示指定矩形是否等于当前矩形。Indicates whether the specified rectangle is equal to the current rectangle.

Equals(Rect, Rect)

指示指定的矩形是否相等。Indicates whether the specified rectangles are equal.

GetHashCode()

创建矩形的哈希代码。Creates a hash code for the rectangle.

Inflate(Double, Double)

使用指定的宽度和高度量向所有方向放大或缩小矩形。Expands or shrinks the rectangle by using the specified width and height amounts, in all directions.

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

Inflate(Rect, Size)

返回矩形,该矩形是通过将指定矩形向所有方向放大指定 Size 而得到的。Returns the rectangle that results from expanding the specified rectangle by the specified Size, in all directions.

Inflate(Size)

使用指定的 Size 向所有方向放大矩形。Expands the rectangle by using the specified Size, in all directions.

Intersect(Rect)

查找当前矩形和指定矩形的交集,并将结果存储为当前矩形。Finds the intersection of the current rectangle and the specified rectangle, and stores the result as the current rectangle.

Intersect(Rect, Rect)

返回指定矩形的交集。Returns the intersection of the specified rectangles.

IntersectsWith(Rect)

指示指定矩形是否与当前矩形相交。Indicates whether the specified rectangle intersects with the current rectangle.

Offset(Double, Double)

将矩形沿水平和垂直方向移动指定的量。Moves the rectangle by the specified horizontal and vertical amounts.

Offset(Rect, Double, Double)

返回一个矩形,该矩形是通过将指定矩形偏移指定的水平和垂直量而得到的。Returns a rectangle that is offset from the specified rectangle by using the specified horizontal and vertical amounts.

Offset(Rect, Vector)

返回一个矩形,该矩形是通过将指定矩形偏移指定向量而得到的。Returns a rectangle that is offset from the specified rectangle by using the specified vector.

Offset(Vector)

将矩形移动指定向量。Moves the rectangle by the specified vector.

Parse(String)

根据指定的字符串表示形式创建新的矩形。Creates a new rectangle from the specified string representation.

Scale(Double, Double)

将当前矩形的大小与指定的 x 和 y 值相乘。Multiplies the size of the current rectangle by the specified x and y values.

ToString()

返回矩形的字符串表示形式。Returns a string representation of the rectangle.

ToString(IFormatProvider)

使用指定的格式提供程序返回矩形的字符串表示形式。Returns a string representation of the rectangle by using the specified format provider.

Transform(Matrix)

通过应用指定矩阵来转换矩形。Transforms the rectangle by applying the specified matrix.

Transform(Rect, Matrix)

返回矩形,该矩形是通过向指定矩形应用指定矩阵而得到的。Returns the rectangle that results from applying the specified matrix to the specified rectangle.

Union(Point)

放大当前矩形,使其刚好足以包含指定点。Expands the current rectangle exactly enough to contain the specified point.

Union(Rect)

放大当前矩形,使其刚好足以包含指定矩形。Expands the current rectangle exactly enough to contain the specified rectangle.

Union(Rect, Point)

创建一个矩形,该矩形的大小刚好足以包含指定矩形和指定点。Creates a rectangle that is exactly large enough to include the specified rectangle and the specified point.

Union(Rect, Rect)

创建一个矩形,该矩形的大小刚好足以包含两个指定矩形。Creates a rectangle that is exactly large enough to contain the two specified rectangles.

运算符

Equality(Rect, Rect)

比较两个矩形是否完全相等。Compares two rectangles for exact equality.

Inequality(Rect, Rect)

比较两个矩形是否不相等。Compares two rectangles for inequality.

显式接口实现

IFormattable.ToString(String, IFormatProvider)

使用指定格式对当前实例的值设置格式。Formats the value of the current instance using the specified format.

适用于