Rect Struct-datatyp
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Beskriver bredd, höjd och plats för en rektangel.
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
- Arv
- Attribut
- Implementeringar
Exempel
I följande exempel visas hur du använder en Rect struktur för att ange dimensioner och plats för en rektangel med 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>
I följande exempel visas hur du använder kod för att skapa en rektangel och lägga till den på sidan. Exemplet illustrerar också hur du hittar storlek och samordnar information om den nya rektangeln och återger informationen i en TextBox under rektangeln.
// 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;
}
Kommentarer
XAML-attributanvändning
<object property="x,y,width,height"/>
XAML-värden
X-koordinatplatsen för den vänstra sidan av rektangeln.
Y-koordinatplatsen för den övre sidan av rektangeln.
BreddSystem.Double
Ett icke-negativt värde som representerar Width rektangelns värde.
HöjdSystem.Double
Ett icke-negativt värde som representerar Height rektangelns värde.
Konstruktorer
| Name | Description |
|---|---|
| Rect(Double, Double, Double, Double) |
Initierar en ny instans av Rect strukturen som har den angivna x-koordinaten, y-koordinaten, bredden och höjden. |
| Rect(Point, Point) |
Initierar en ny instans av Rect strukturen som är exakt tillräckligt stor för att innehålla de två angivna punkterna. |
| Rect(Point, Size) |
Initierar en ny instans av Rect strukturen som har den angivna platsen i det övre vänstra hörnet och den angivna bredden och höjden. |
| Rect(Point, Vector) |
Initierar en ny instans av Rect strukturen som är exakt tillräckligt stor för att innehålla den angivna punkten och summan av den angivna punkten och den angivna vektorn. |
| Rect(Size) |
Initierar en ny instans av strukturen Rect som är av den angivna storleken och som finns på (0,0). |
Egenskaper
| Name | Description |
|---|---|
| Bottom |
Hämtar y-axelns värde längst ned i rektangeln. |
| BottomLeft |
Hämtar positionen för det nedre vänstra hörnet i rektangeln. |
| BottomRight |
Hämtar positionen för rektangelns nedre högra hörn. |
| Empty |
Hämtar ett särskilt värde som representerar en rektangel utan position eller område. |
| Height |
Hämtar eller anger rektangelns höjd. |
| IsEmpty |
Hämtar ett värde som anger om rektangeln är Empty rektangeln. |
| Left |
Hämtar x-axelns värde på den vänstra sidan av rektangeln. |
| Location |
Hämtar eller anger positionen för det övre vänstra hörnet i rektangeln. |
| Right |
Hämtar x-axelns värde på höger sida av rektangeln. |
| Size |
Hämtar eller anger rektangelns bredd och höjd. |
| Top |
Hämtar y-axelns position överst i rektangeln. |
| TopLeft |
Hämtar positionen för det övre vänstra hörnet i rektangeln. |
| TopRight |
Hämtar positionen för det övre högra hörnet i rektangeln. |
| Width |
Hämtar eller anger rektangelns bredd. |
| X |
Hämtar eller anger x-axelns värde på den vänstra sidan av rektangeln. |
| Y |
Hämtar eller anger y-axelns värde på den övre sidan av rektangeln. |
Metoder
| Name | Description |
|---|---|
| Contains(Double, Double) |
Anger om rektangeln innehåller den angivna x-koordinaten och y-koordinaten. |
| Contains(Point) |
Anger om rektangeln innehåller den angivna punkten. |
| Contains(Rect) |
Anger om rektangeln innehåller den angivna rektangeln. |
| Equals(Object) |
Anger om det angivna objektet är lika med den aktuella rektangeln. |
| Equals(Rect, Rect) |
Anger om de angivna rektanglarna är lika med. |
| Equals(Rect) |
Anger om den angivna rektangeln är lika med den aktuella rektangeln. |
| GetHashCode() |
Skapar en hash-kod för rektangeln. |
| Inflate(Double, Double) |
Expanderar eller krymper rektangeln med hjälp av angivna bredd- och höjdmängder i alla riktningar. |
| Inflate(Rect, Double, Double) |
Skapar en rektangel som resulterar i att den angivna rektangeln expanderas eller krymps med angivna bredd- och höjdbelopp i alla riktningar. |
| Inflate(Rect, Size) |
Returnerar den rektangel som resulterar i att den angivna rektangeln expanderas av den angivna Size, i alla riktningar. |
| Inflate(Size) |
Expanderar rektangeln med hjälp av den angivna Size, i alla riktningar. |
| Intersect(Rect, Rect) |
Returnerar skärningspunkten för de angivna rektanglarna. |
| Intersect(Rect) |
Hittar skärningspunkten för den aktuella rektangeln och den angivna rektangeln och lagrar resultatet som den aktuella rektangeln. |
| IntersectsWith(Rect) |
Anger om den angivna rektangeln korsar den aktuella rektangeln. |
| Offset(Double, Double) |
Flyttar rektangeln med de angivna vågräta och lodräta beloppen. |
| Offset(Rect, Double, Double) |
Returnerar en rektangel som förskjuts från den angivna rektangeln med hjälp av de angivna vågräta och lodräta beloppen. |
| Offset(Rect, Vector) |
Returnerar en rektangel som förskjuts från den angivna rektangeln med hjälp av den angivna vektorn. |
| Offset(Vector) |
Flyttar rektangeln efter den angivna vektorn. |
| Parse(String) |
Skapar en ny rektangel från den angivna strängrepresentationen. |
| Scale(Double, Double) |
Multiplicerar storleken på den aktuella rektangeln med de angivna x- och y-värdena. |
| ToString() |
Returnerar en strängrepresentation av rektangeln. |
| ToString(IFormatProvider) |
Returnerar en strängrepresentation av rektangeln med hjälp av den angivna formatprovidern. |
| Transform(Matrix) |
Transformerar rektangeln genom att använda den angivna matrisen. |
| Transform(Rect, Matrix) |
Returnerar den rektangel som resulterar i att den angivna matrisen tillämpas på den angivna rektangeln. |
| Union(Point) |
Expanderar den aktuella rektangeln exakt så att den innehåller den angivna punkten. |
| Union(Rect, Point) |
Skapar en rektangel som är exakt tillräckligt stor för att inkludera den angivna rektangeln och den angivna punkten. |
| Union(Rect, Rect) |
Skapar en rektangel som är exakt tillräckligt stor för att innehålla de två angivna rektanglarna. |
| Union(Rect) |
Expanderar den aktuella rektangeln exakt tillräckligt för att innehålla den angivna rektangeln. |
Operatorer
| Name | Description |
|---|---|
| Equality(Rect, Rect) |
Jämför två rektanglar för exakt likhet. |
| Inequality(Rect, Rect) |
Jämför två rektanglar för ojämlikhet. |
Explicita gränssnittsimplementeringar
| Name | Description |
|---|---|
| IFormattable.ToString(String, IFormatProvider) |
Formaterar värdet för den aktuella instansen med det angivna formatet. |