Rect.Offset 方法

定义

按指定量移动矩形。

重载

名称 说明
Offset(Vector)

按指定的向量移动矩形。

Offset(Double, Double)

按指定的水平和垂直量移动矩形。

Offset(Rect, Vector)

使用指定的向量返回与指定矩形偏移的矩形。

Offset(Rect, Double, Double)

使用指定的水平和垂直量返回与指定矩形偏移的矩形。

Offset(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)

参数

offsetVector
Vector

一个矢量,指定要移动矩形的水平和垂直量。

例外

此方法在矩形上 Empty 调用。

示例

下面的示例演示如何使用 Offset(Vector) 该方法更改矩形的位置。

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

注解

不允许对空矩形 (Rect.Empty) 调用此方法。

请注意,仅当可以直接更改OffsetX属性时,调用Y该方法才会产生效果。 因为 Rect 是值类型,如果使用属性或索引器引用 Rect 对象,则获取对象的副本,而不是对对象的引用。 如果尝试更改或X更改Y属性或索引器引用,则会发生编译器错误。 同样,调用 Offset 属性或索引器不会更改基础对象。 如果要更改作为属性或索引器引用的值 Rect ,请创建新的 Rect字段,修改其字段,然后将返回 Rect 给属性或索引器。

适用于

Offset(Double, Double)

按指定的水平和垂直量移动矩形。

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)

参数

offsetX
Double

水平移动矩形的量。

offsetY
Double

垂直移动矩形的量。

例外

此方法在矩形上 Empty 调用。

示例

下面的示例演示如何使用 Offset(Double, Double) 该方法更改矩形的位置。

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

注解

不允许对空矩形 (Rect.Empty) 调用此方法。

请注意,仅当可以直接更改OffsetX属性时,调用Y该方法才会产生效果。 因为 Rect 是值类型,如果使用属性或索引器引用 Rect 对象,则获取对象的副本,而不是对对象的引用。 如果尝试更改或X更改Y属性或索引器引用,则会发生编译器错误。 同样,调用 Offset 属性或索引器不会更改基础对象。 如果要更改作为属性或索引器引用的值 Rect ,请创建新的 Rect字段,修改其字段,然后将返回 Rect 给属性或索引器。

适用于

Offset(Rect, 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

参数

rect
Rect

原始矩形。

offsetVector
Vector

一个矢量,指定新矩形的水平偏移量和垂直偏移量。

返回

生成的矩形。

例外

示例

下面的示例演示如何使用 Offset(Rect, Vector) 该方法更改矩形的位置。

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

注解

不允许使用空矩形 (Rect.Empty) 调用此方法。

适用于

Offset(Rect, Double, Double)

使用指定的水平和垂直量返回与指定矩形偏移的矩形。

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

参数

rect
Rect

要移动的矩形。

offsetX
Double

新矩形的水平偏移量。

offsetY
Double

新矩形的垂直偏移量。

返回

生成的矩形。

例外

示例

下面的示例演示如何使用 Offset(Rect, Double, Double) 该方法更改矩形的位置。

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

注解

不允许使用空矩形 (Rect.Empty) 调用此方法。

适用于