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 窗体 一起使用,它需要 PaintEventArgs e,一个OnPaint事件对象。 该代码创建两 RectangleF 个 ,并将它们绘制到黑色和红色的屏幕上。 请注意,它们必须转换为 Rectangle ,以便进行绘制。 然后,代码使用 Union 方法创建第三RectangleFRectangle,将其转换为 ,并将其绘制到蓝色屏幕。 请注意,第三个 (蓝色) 矩形是可以同时包含另外两个矩形的最小矩形:

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

适用于