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

請注意,如果您可以直接變更 和 Y 屬性, X 則呼叫 Offset 方法只會有作用。 因為 Rect 是實值型別,如果您使用屬性或索引子參考 Rect 物件,您就會取得物件的複本,而不是物件的參考。 如果您嘗試變更 XY 屬性或索引子參考,就會發生編譯器錯誤。 同樣地,在屬性或索引子上呼叫 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 () 。

請注意,如果您可以直接變更 和 Y 屬性, X 則呼叫 Offset 方法只會有作用。 因為 Rect 是實值型別,如果您使用屬性或索引子參考 Rect 物件,您就會取得物件的複本,而不是物件的參考。 如果您嘗試變更 XY 屬性或索引子參考,就會發生編譯器錯誤。 同樣地,在屬性或索引子上呼叫 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 () 。

適用於