Rectangle.IntersectsWith(Rectangle) 方法

定义

确定此矩形是否与 rect 相交。Determines if this rectangle intersects with rect.

public:
 bool IntersectsWith(System::Drawing::Rectangle rect);
public bool IntersectsWith (System.Drawing.Rectangle rect);
member this.IntersectsWith : System.Drawing.Rectangle -> bool
Public Function IntersectsWith (rect As Rectangle) As Boolean

参数

rect
Rectangle

要测试的矩形。The rectangle to test.

返回

Boolean

如果有重叠,此方法将返回 true;否则将返回 falseThis method returns true if there is any intersection, otherwise false.

示例

下面的代码示例演示了 Intersect IsEmptyIntersectsWith 成员。The following code example demonstrates the Intersect, IsEmpty and the IntersectsWith members. 此示例应与 Windows 窗体一起使用。This example should be used with a Windows Form. 将此代码粘贴到窗体中,并在处理窗体事件时调用此方法,并将 Paint e 作为传递 PaintEventArgsPaste this code into a form and call this method when handling the form's Paint event, passing e as PaintEventArgs.

private:
   void InstanceRectangleIntersection( PaintEventArgs^ e )
   {
      Rectangle rectangle1 = Rectangle(50,50,200,100);
      Rectangle rectangle2 = 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 );
         }
      }
   }
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);
        }
    }
}
  Private Sub InstanceRectangleIntersection( _
      ByVal e As PaintEventArgs)

      Dim rectangle1 As New Rectangle(50, 50, 200, 100)
      Dim rectangle2 As New Rectangle(70, 20, 100, 200)

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

      If (rectangle1.IntersectsWith(rectangle2)) Then
          rectangle1.Intersect(rectangle2)
          If Not (rectangle1.IsEmpty) Then
              e.Graphics.FillRectangle(Brushes.Green, rectangle1)
          End If
      End If
  End Sub

适用于