Rect Struct

Definizione

Descrive la larghezza, l'altezza e la posizione di un rettangolo.

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
Ereditarietà
Attributi
Implementazioni

Esempio

L'esempio seguente illustra come usare una Rect struttura per specificare le dimensioni e la posizione di un rettangolo usando 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>

Nell'esempio seguente viene illustrato come usare il codice per creare un rettangolo e aggiungerlo alla pagina. Nell'esempio viene illustrato anche come trovare informazioni sulle dimensioni e sulle coordinate sul nuovo rettangolo ed eseguire il rendering delle informazioni in un TextBox sotto il rettangolo.

// 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;
}

Commenti

Utilizzo degli attributi XAML

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

Valori XAML

XSystem.Double

Posizione della coordinata x del lato sinistro del rettangolo.

YSystem.Double

Posizione della coordinata y del lato superiore del rettangolo.

LarghezzaSystem.Double

Valore non negativo che rappresenta l'oggetto Width del rettangolo.

AltezzaSystem.Double

Valore non negativo che rappresenta l'oggetto Height del rettangolo.

Costruttori

Nome Descrizione
Rect(Double, Double, Double, Double)

Inizializza una nuova istanza della Rect struttura con la coordinata x, la coordinata y, la larghezza e l'altezza specificate.

Rect(Point, Point)

Inizializza una nuova istanza della Rect struttura di dimensioni sufficienti per contenere i due punti specificati.

Rect(Point, Size)

Inizializza una nuova istanza della Rect struttura con la posizione dell'angolo superiore sinistro specificata e la larghezza e l'altezza specificate.

Rect(Point, Vector)

Inizializza una nuova istanza della Rect struttura con dimensioni sufficienti per contenere il punto specificato e la somma del punto specificato e del vettore specificato.

Rect(Size)

Inizializza una nuova istanza della Rect struttura con dimensioni specificate e che si trova in (0,0).

Proprietà

Nome Descrizione
Bottom

Ottiene il valore dell'asse y della parte inferiore del rettangolo.

BottomLeft

Ottiene la posizione dell'angolo inferiore sinistro del rettangolo.

BottomRight

Ottiene la posizione dell'angolo inferiore destro del rettangolo.

Empty

Ottiene un valore speciale che rappresenta un rettangolo senza posizione o area.

Height

Ottiene o imposta l'altezza del rettangolo.

IsEmpty

Ottiene un valore che indica se il rettangolo è il Empty rettangolo.

Left

Ottiene il valore dell'asse x del lato sinistro del rettangolo.

Location

Ottiene o imposta la posizione dell'angolo superiore sinistro del rettangolo.

Right

Ottiene il valore dell'asse x del lato destro del rettangolo.

Size

Ottiene o imposta la larghezza e l'altezza del rettangolo.

Top

Ottiene la posizione dell'asse y della parte superiore del rettangolo.

TopLeft

Ottiene la posizione dell'angolo superiore sinistro del rettangolo.

TopRight

Ottiene la posizione dell'angolo superiore destro del rettangolo.

Width

Ottiene o imposta la larghezza del rettangolo.

X

Ottiene o imposta il valore dell'asse x del lato sinistro del rettangolo.

Y

Ottiene o imposta il valore dell'asse y del lato superiore del rettangolo.

Metodi

Nome Descrizione
Contains(Double, Double)

Indica se il rettangolo contiene la coordinata x e la coordinata y specificate.

Contains(Point)

Indica se il rettangolo contiene il punto specificato.

Contains(Rect)

Indica se il rettangolo contiene il rettangolo specificato.

Equals(Object)

Indica se l'oggetto specificato è uguale al rettangolo corrente.

Equals(Rect, Rect)

Indica se i rettangoli specificati sono uguali.

Equals(Rect)

Indica se il rettangolo specificato è uguale al rettangolo corrente.

GetHashCode()

Crea un codice hash per il rettangolo.

Inflate(Double, Double)

Espande o riduce il rettangolo utilizzando gli importi di larghezza e altezza specificati, in tutte le direzioni.

Inflate(Rect, Double, Double)

Crea un rettangolo risultante dall'espansione o dalla compattazione del rettangolo specificato in base alla larghezza e all'altezza specificate, in tutte le direzioni.

Inflate(Rect, Size)

Restituisce il rettangolo risultante dall'espansione del rettangolo specificato dall'oggetto specificato Sizein tutte le direzioni.

Inflate(Size)

Espande il rettangolo usando l'oggetto specificato Sizein tutte le direzioni.

Intersect(Rect, Rect)

Restituisce l'intersezione dei rettangoli specificati.

Intersect(Rect)

Trova l'intersezione del rettangolo corrente e del rettangolo specificato e archivia il risultato come rettangolo corrente.

IntersectsWith(Rect)

Indica se il rettangolo specificato si interseca con il rettangolo corrente.

Offset(Double, Double)

Sposta il rettangolo in base agli importi orizzontali e verticali specificati.

Offset(Rect, Double, Double)

Restituisce un rettangolo che viene scostato dal rettangolo specificato utilizzando gli importi orizzontali e verticali specificati.

Offset(Rect, Vector)

Restituisce un rettangolo che viene scostato dal rettangolo specificato utilizzando il vettore specificato.

Offset(Vector)

Sposta il rettangolo in base al vettore specificato.

Parse(String)

Crea un nuovo rettangolo dalla rappresentazione di stringa specificata.

Scale(Double, Double)

Moltiplica le dimensioni del rettangolo corrente in base ai valori x e y specificati.

ToString()

Restituisce una rappresentazione di stringa del rettangolo.

ToString(IFormatProvider)

Restituisce una rappresentazione di stringa del rettangolo utilizzando il provider di formato specificato.

Transform(Matrix)

Trasforma il rettangolo applicando la matrice specificata.

Transform(Rect, Matrix)

Restituisce il rettangolo risultante dall'applicazione della matrice specificata al rettangolo specificato.

Union(Point)

Espande esattamente il rettangolo corrente per contenere il punto specificato.

Union(Rect, Point)

Crea un rettangolo di dimensioni sufficienti per includere il rettangolo specificato e il punto specificato.

Union(Rect, Rect)

Crea un rettangolo di dimensioni sufficienti per contenere i due rettangoli specificati.

Union(Rect)

Espande esattamente il rettangolo corrente per contenere il rettangolo specificato.

Operatori

Nome Descrizione
Equality(Rect, Rect)

Confronta due rettangoli per verificare l'uguaglianza esatta.

Inequality(Rect, Rect)

Confronta due rettangoli per verificare la disuguaglianza.

Implementazioni dell'interfaccia esplicita

Nome Descrizione
IFormattable.ToString(String, IFormatProvider)

Formatta il valore dell'istanza corrente utilizzando il formato specificato.

Si applica a