Rectangle 建構函式

定義

使用指定的位置和大小,初始化 Rectangle 類別的新執行個體。

多載

Rectangle(Point, Size)

使用指定的位置和大小,初始化 Rectangle 類別的新執行個體。

Rectangle(Int32, Int32, Int32, Int32)

使用指定的位置和大小,初始化 Rectangle 類別的新執行個體。

Rectangle(Point, Size)

來源:
Rectangle.cs
來源:
Rectangle.cs
來源:
Rectangle.cs

使用指定的位置和大小,初始化 Rectangle 類別的新執行個體。

C#
public Rectangle (System.Drawing.Point location, System.Drawing.Size size);

參數

location
Point

Point,代表矩形區域的左上角。

size
Size

Size,代表矩形區域的寬度和高度。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Rectangle(Int32, Int32, Int32, Int32)

來源:
Rectangle.cs
來源:
Rectangle.cs
來源:
Rectangle.cs

使用指定的位置和大小,初始化 Rectangle 類別的新執行個體。

C#
public Rectangle (int x, int y, int width, int height);

參數

x
Int32

矩形左上角的 X 座標。

y
Int32

矩形左上角的 Y 座標。

width
Int32

矩形的寬度。

height
Int32

矩形的高度。

範例

下列程式代碼範例示範RectangleIntersectIsEmpty、 和 IntersectsWith 成員。 此範例應該與 Windows Form 搭配使用。 將此程式代碼貼到表單中,並在處理表單的事件 Paint 時呼叫此方法,並 e 傳遞為 PaintEventArgs

C#
private void InstanceRectangleIntersection(PaintEventArgs e)
{

    Rectangle rectangle1 = new Rectangle(50, 50, 200, 100);
    Rectangle rectangle2 = new Rectangle(70, 20, 100, 200);

    e.Graphics.DrawRectangle(Pens.Black, rectangle1);
    e.Graphics.DrawRectangle(Pens.Red, rectangle2);

    if (rectangle1.IntersectsWith(rectangle2))
    {
        rectangle1.Intersect(rectangle2);
        if (!rectangle1.IsEmpty)
        {
            e.Graphics.FillRectangle(Brushes.Green, rectangle1);
        }
    }
}

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1