共用方式為


RectangleF.Union(RectangleF, RectangleF) 方法

定義

建立最小可能的第三個矩形,這個矩形包含形成聯集的兩個矩形。

public:
 static System::Drawing::RectangleF Union(System::Drawing::RectangleF a, System::Drawing::RectangleF b);
public static System.Drawing.RectangleF Union (System.Drawing.RectangleF a, System.Drawing.RectangleF b);
static member Union : System.Drawing.RectangleF * System.Drawing.RectangleF -> System.Drawing.RectangleF
Public Shared Function Union (a As RectangleF, b As RectangleF) As RectangleF

參數

a
RectangleF

要聯集的矩形。

b
RectangleF

要聯集的矩形。

傳回

第三個 RectangleF 結構,包含形成聯集的兩個矩形。

範例

此範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgs e 事件OnPaint物件。 程序代碼會建立兩 RectangleF 個 ,並以黑色和紅色將它們繪製到畫面。 請注意,它們必須轉換成 Rectangle ,以供繪圖之用。 然後,程式代碼會使用 Union 方法建立第三RectangleF個 ,並將它Rectangle轉換成 ,並以藍色將它繪製到畫面。 請注意,第三個 (藍色) 矩形是可同時包含其他兩個矩形的最小可能矩形:

public:
   void RectangleFUnionExample( PaintEventArgs^ e )
   {
      // Create two rectangles and draw them to the screen.
      RectangleF firstRectangleF = RectangleF(0,0,75,50);
      RectangleF secondRectangleF = RectangleF(100,100,20,20);

      // Convert the RectangleF structures to Rectangle structures and draw them to the
      // screen.
      Rectangle firstRect = Rectangle::Truncate( firstRectangleF );
      Rectangle secondRect = Rectangle::Truncate( secondRectangleF );
      e->Graphics->DrawRectangle( Pens::Black, firstRect );
      e->Graphics->DrawRectangle( Pens::Red, secondRect );

      // Get the union rectangle.
      RectangleF unionRectangleF = RectangleF::Union( firstRectangleF, secondRectangleF );

      // Draw the unionRectangleF to the screen.
      Rectangle unionRect = Rectangle::Truncate( unionRectangleF );
      e->Graphics->DrawRectangle( Pens::Blue, unionRect );
   }
public void RectangleFUnionExample(PaintEventArgs e)
{
             
    // Create two rectangles and draw them to the screen.
    RectangleF firstRectangleF = new RectangleF(0, 0, 75, 50);
    RectangleF secondRectangleF = new RectangleF(100, 100, 20, 20);
             
    // Convert the RectangleF structures to Rectangle structures and draw them to the
             
    // screen.
    Rectangle firstRect = Rectangle.Truncate(firstRectangleF);
    Rectangle secondRect = Rectangle.Truncate(secondRectangleF);
    e.Graphics.DrawRectangle(Pens.Black, firstRect);
    e.Graphics.DrawRectangle(Pens.Red, secondRect);
             
    // Get the union rectangle.
    RectangleF unionRectangleF = RectangleF.Union(firstRectangleF,
        secondRectangleF);
             
    // Draw the unionRectangleF to the screen.
    Rectangle unionRect = Rectangle.Truncate(unionRectangleF);
    e.Graphics.DrawRectangle(Pens.Blue, unionRect);
}
Public Sub RectangleFUnionExample(ByVal e As PaintEventArgs)

    ' Create two rectangles and draw them to the screen.
    Dim firstRectangleF As New RectangleF(0, 0, 75, 50)
    Dim secondRectangleF As New RectangleF(100, 100, 20, 20)

    ' Convert the RectangleF structures to Rectangle structures and

    ' draw them to the screen.
    Dim firstRect As Rectangle = Rectangle.Truncate(firstRectangleF)
    Dim secondRect As Rectangle = Rectangle.Truncate(secondRectangleF)
    e.Graphics.DrawRectangle(Pens.Black, firstRect)
    e.Graphics.DrawRectangle(Pens.Red, secondRect)

    ' Get the union rectangle.
    Dim unionRectangleF As RectangleF = _
    RectangleF.Union(firstRectangleF, secondRectangleF)

    ' Draw the unionRectangleF to the screen.
    Dim unionRect As Rectangle = Rectangle.Truncate(unionRectangleF)
    e.Graphics.DrawRectangle(Pens.Blue, unionRect)
End Sub

備註

當兩個矩形的其中一個是空的,表示其所有值都是零時, Union 此方法會傳回一個矩形,其起點為 (0、0) ,以及非空白矩形的高度和寬度。 例如,如果您有兩個矩形 A = (0, 0;0、 0) 和 B = (1, 1;2,2) ,然後 A 和 B 的聯集 (0, 0;2,2) 。

適用於