Rect 結構

定義

描述矩形的寬度、高度和位置。

public value class Rect : IFormattable
[System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))]
[System.Serializable]
public struct Rect : IFormattable
[System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))]
public struct Rect : IFormattable
[<System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))>]
[<System.Serializable>]
type Rect = struct
    interface IFormattable
[<System.ComponentModel.TypeConverter(typeof(System.Windows.RectConverter))>]
type Rect = struct
    interface IFormattable
Public Structure Rect
Implements IFormattable
繼承
屬性
實作

範例

以下範例說明如何使用 Rect 結構,使用 XAML 指定矩形的尺寸與位置。

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 myRect1 = 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>

以下範例展示了如何使用程式碼建立矩形並將其加入頁面。 範例也說明如何尋找新矩形的大小與座標資訊,並將資訊渲染在 TextBox 矩形下方。

// 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 屬性使用方式

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

XAML 值

xSystem.Double

矩形左側的 x 座標位置。

ySystem.Double

矩形頂端的 y 座標位置。

寬度System.Double

一個非負值,代表矩形的 。Width

高度System.Double

一個非負值,代表矩形的 。Height

建構函式

名稱 Description
Rect(Double, Double, Double, Double)

初始化一個結構的新實例,該實例 Rect 具有指定的 x 座標、y 座標、寬度和高度。

Rect(Point, Point)

初始化一個結構的新實例,該實例 Rect 恰好足夠大以包含指定的兩個點。

Rect(Point, Size)

初始化一個結構的新實例,該實例 Rect 具有指定的左上角位置,以及指定的寬度與高度。

Rect(Point, Vector)

初始化一個結構的新實例,該實例 Rect 恰好足夠大,能包含指定點、指定點與指定向量的總和。

Rect(Size)

初始化一個大小符合指定大小且位於 (0,0) 的新結構實例 Rect

屬性

名稱 Description
Bottom

取得矩形底部的縱軸值。

BottomLeft

取得矩形左下角的位置。

BottomRight

取得矩形右下角的位置。

Empty

會得到一個特殊值,代表一個沒有位置或面積的矩形。

Height

取得或設定矩形的高度。

IsEmpty

會得到一個值,表示該矩形是否為該矩形。Empty

Left

取得矩形左側的 x 軸值。

Location

取得或設定矩形左上角的位置。

Right

取得矩形右側的 x 軸值。

Size

設定矩形的寬度和高度。

Top

取得矩形頂部的 y 軸位置。

TopLeft

取得矩形左上角的位置。

TopRight

取得矩形右上角的位置。

Width

取得或設定矩形的寬度。

X

取得或設定矩形左側的 x 軸值。

Y

取得或設定矩形上端的 y 軸值。

方法

名稱 Description
Contains(Double, Double)

表示矩形是否包含指定的 x 座標和 y 座標。

Contains(Point)

表示矩形是否包含指定點。

Contains(Rect)

表示矩形是否包含指定的矩形。

Equals(Object)

表示指定的物件是否等於當前矩形。

Equals(Rect, Rect)

表示指定的矩形是否相等。

Equals(Rect)

表示指定的矩形是否等於當前的矩形。

GetHashCode()

為矩形建立雜湊碼。

Inflate(Double, Double)

利用指定的寬度與高度,向所有方向擴展或縮小矩形。

Inflate(Rect, Double, Double)

透過將指定的矩形在各方向以指定的寬度和高度膨脹或縮小,產生一個矩形。

Inflate(Rect, Size)

回傳將指定矩形 Size向所有方向展開所產生的矩形。

Inflate(Size)

利用指定的 Size,向所有方向展開矩形。

Intersect(Rect, Rect)

回傳指定矩形的交點。

Intersect(Rect)

尋找當前矩形與指定矩形的交點,並將結果儲存為當前矩形。

IntersectsWith(Rect)

表示指定的矩形是否與當前矩形相交。

Offset(Double, Double)

將矩形移動至指定的水平與垂直方向。

Offset(Rect, Double, Double)

回傳一個矩形,該矩形透過指定的水平與垂直長度偏移。

Offset(Rect, Vector)

回傳一個由指定向量偏移的矩形。

Offset(Vector)

將矩形移動至指定向量。

Parse(String)

從指定的字串表示建立一個新的矩形。

Scale(Double, Double)

將當前矩形的大小乘以指定的 x 和 y 值。

ToString()

回傳矩形的字串表示。

ToString(IFormatProvider)

透過指定的格式提供者回傳矩形的字串表示。

Transform(Matrix)

透過應用指定的矩陣來轉換矩形。

Transform(Rect, Matrix)

回傳將指定矩陣套用到指定矩形後產生的矩形。

Union(Point)

將當前矩形正好展開到包含指定點的位置。

Union(Rect, Point)

建立一個剛好足夠大的矩形,以包含指定的矩形和指定的點。

Union(Rect, Rect)

建立一個剛好足夠容納兩個指定矩形的矩形。

Union(Rect)

將當前矩形擴展到足以容納指定的矩形。

操作員

名稱 Description
Equality(Rect, Rect)

比較兩個矩形以達到完全相同的結果。

Inequality(Rect, Rect)

比較兩個矩形以求不等式。

明確介面實作

名稱 Description
IFormattable.ToString(String, IFormatProvider)

使用指定的格式,格式化目前實例的值。

適用於