แก้ไข

แชร์ผ่าน


Rect.Offset Method

Definition

Moves a rectangle by the specified amount.

Overloads

Offset(Vector)

Moves the rectangle by the specified vector.

Offset(Double, Double)

Moves the rectangle by 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(Rect, Double, Double)

Returns a rectangle that is offset from the specified rectangle by using the specified horizontal and vertical amounts.

Offset(Vector)

Moves the rectangle by the specified vector.

public:
 void Offset(System::Windows::Vector offsetVector);
public void Offset (System.Windows.Vector offsetVector);
member this.Offset : System.Windows.Vector -> unit
Public Sub Offset (offsetVector As Vector)

Parameters

offsetVector
Vector

A vector that specifies the horizontal and vertical amounts to move the rectangle.

Exceptions

This method is called on the Empty rectangle.

Examples

The following example shows how to use the Offset(Vector) method to change the position of a rectangle.

private Point offsetExample1()
{
    // Initialize new rectangle.
    Rect myRectangle = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. 
    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);

    // Create a vector to use to offset the position of the rectangle.
    Vector vector1 = new Vector(20, 30);

    // The Offset method translates this rectangle by the specified vector.
    // myRectangle location changed from 10,5 to 30,35.
    myRectangle.Offset(vector1);

    // This rectangle's location changed from 10,5 to 30,35.
    return myRectangle.Location;
}

Remarks

Calling this method on an empty rectangle (Rect.Empty) is not allowed.

Note that calling the Offset method will only have an effect if you can change the X and Y properties directly. Because Rect is a value type, if you reference a Rect object by using a property or indexer, you get a copy of the object, not a reference to the object. If you attempt to change X or Y on a property or indexer reference, a compiler error occurs. Similarly, calling Offset on the property or indexer will not change the underlying object. If you want to change the value of a Rect that is referenced as a property or indexer, create a new Rect, modify its fields, and then assign the Rect back to the property or indexer.

Applies to

Offset(Double, Double)

Moves the rectangle by the specified horizontal and vertical amounts.

public:
 void Offset(double offsetX, double offsetY);
public void Offset (double offsetX, double offsetY);
member this.Offset : double * double -> unit
Public Sub Offset (offsetX As Double, offsetY As Double)

Parameters

offsetX
Double

The amount to move the rectangle horizontally.

offsetY
Double

The amount to move the rectangle vertically.

Exceptions

This method is called on the Empty rectangle.

Examples

The following example shows how to use the Offset(Double, Double) method to change the position of a rectangle.

private Point offsetExample2()
{
    // Initialize new rectangle.
    Rect myRectangle = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. 
    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);

    // The Offset method translates this rectangle by the specified horizontal and 
    // vertical amounts. 
    // myRectangle location changed from 10,5 to 30,35.
    myRectangle.Offset(20,30);

    // This rectangle's location changed from 10,5 to 30,35.
    return myRectangle.Location;
}

Remarks

Calling this method on an empty rectangle (Rect.Empty) is not allowed.

Note that calling the Offset method will only have an effect if you can change the X and Y properties directly. Because Rect is a value type, if you reference a Rect object by using a property or indexer, you get a copy of the object, not a reference to the object. If you attempt to change X or Y on a property or indexer reference, a compiler error occurs. Similarly, calling Offset on the property or indexer will not change the underlying object. If you want to change the value of a Rect that is referenced as a property or indexer, create a new Rect, modify its fields, and then assign the Rect back to the property or indexer.

Applies to

Offset(Rect, Vector)

Returns a rectangle that is offset from the specified rectangle by using the specified vector.

public:
 static System::Windows::Rect Offset(System::Windows::Rect rect, System::Windows::Vector offsetVector);
public static System.Windows.Rect Offset (System.Windows.Rect rect, System.Windows.Vector offsetVector);
static member Offset : System.Windows.Rect * System.Windows.Vector -> System.Windows.Rect
Public Shared Function Offset (rect As Rect, offsetVector As Vector) As Rect

Parameters

rect
Rect

The original rectangle.

offsetVector
Vector

A vector that specifies the horizontal and vertical offsets for the new rectangle.

Returns

The resulting rectangle.

Exceptions

Examples

The following example shows how to use the Offset(Rect, Vector) method to change the position of a rectangle.

private Point offsetExample3()
{
    // Initialize new rectangle.
    Rect myRectangle = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. 
    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);

    // Create a vector to use to offset the position of the rectangle.
    Vector vector1 = new Vector(20, 30);

    // The Offset method translates the specified rectangle by the specified amount 
    // and returns the resulting Rect. 
    // resultRect location changed from 10,5 to 30,35.
    Rect resultRect = Rect.Offset(myRectangle, vector1);

    // This rectangle's location changed from 10,5 to 30,35.
    return resultRect.Location;
}

Remarks

Calling this method with an empty rectangle (Rect.Empty) is not allowed.

Applies to

Offset(Rect, Double, Double)

Returns a rectangle that is offset from the specified rectangle by using the specified horizontal and vertical amounts.

public:
 static System::Windows::Rect Offset(System::Windows::Rect rect, double offsetX, double offsetY);
public static System.Windows.Rect Offset (System.Windows.Rect rect, double offsetX, double offsetY);
static member Offset : System.Windows.Rect * double * double -> System.Windows.Rect
Public Shared Function Offset (rect As Rect, offsetX As Double, offsetY As Double) As Rect

Parameters

rect
Rect

The rectangle to move.

offsetX
Double

The horizontal offset for the new rectangle.

offsetY
Double

The vertical offset for the new rectangle.

Returns

The resulting rectangle.

Exceptions

Examples

The following example shows how to use the Offset(Rect, Double, Double) method to change the position of a rectangle.

private Point offsetExample4()
{
    // Initialize new rectangle.
    Rect myRectangle = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. 
    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);

    // Create a vector to use to offset the position of the rectangle.
    Vector vector1 = new Vector(20, 30);

    // The Offset method translates the specified rectangle by the specified horizontal 
    // and vertical amounts and returns the resulting Rect. 
    // resultRect location changed from 10,5 to 30,35.
    Rect resultRect = Rect.Offset(myRectangle, 20, 30);

    // This rectangle's location changed from 10,5 to 30,35.
    return resultRect.Location;
}

Remarks

Calling this method with an empty rectangle (Rect.Empty) is not allowed.

Applies to