RectangleF.Union(RectangleF, RectangleF) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建可包含构成联合的两个矩形中的两个矩形的最小可能第三个矩形。
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
参数
要联合的矩形。
要联合的矩形。
返回
第三 RectangleF 个结构,包含构成联合的两个矩形中的两个。
示例
此示例设计用于 Windows 窗体,它需要 PaintEventArgs e,OnPaint 事件对象。 该代码创建两 RectangleF 个 s,并用黑色和红色将其绘制到屏幕。 请注意,出于绘图目的,它们必须转换为 Rectangle s。 然后,代码使用RectangleF该方法创建第三Union个代码,将其转换为 aRectangle,并将其绘制为蓝色屏幕。 请注意,第三个(蓝色)矩形是可以包含其他两个矩形的最小矩形:
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)。