如何计算一个框是位于 window 的上部还是下部?

How do I calculate if a box is located more in the upper part of a window or in the lower?

用我的眼睛,我可以立即判断一个物体是在盒子的上半部分还是下半部分。

马上就会说白框位于蓝框的下方。

但是我怎样才能有效地计算呢?

我是否可以将蓝色框分成上下两半,然后为白色框和两个部分创建相交矩形,然后看看哪个相交矩形的体积最大?

谢谢!

谢谢!

您的方法很好,也是最好的方法,Windows API 可以帮助我们。该代码比自己编写所有需要的比较更简单。

Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long

Private Sub Command1_Click()
   Dim UpperArea As RECT
   Dim LowerArea As RECT
   Dim WhiteBox As RECT
   Dim IntersectUpper As RECT
   Dim IntersectLower As RECT

   'define rectangles for upper area, lower area, and the white box
   SetRect UpperArea, Frame1.Left, Frame1.Top, Frame1.Left + Frame1.Width, Frame1.Top + (Frame1.Height * 0.5)
   SetRect LowerArea, Frame1.Left, Frame1.Top + (Frame1.Height * 0.5), Frame1.Left + Frame1.Width, Frame1.Top + Frame1.Height
   SetRect WhiteBox, Frame2.Left, Frame2.Top, Frame2.Left + Frame2.Width, Frame2.Top + Frame2.Height

   'check where the white box is located
   If IntersectRect(IntersectUpper, UpperArea, WhiteBox) > 0 Or IntersectRect(IntersectLower, LowerArea, WhiteBox) > 0 Then
      If IntersectUpper.Bottom - IntersectUpper.Top > IntersectLower.Bottom - IntersectLower.Top Then
         MsgBox "More in the upper"
      Else
         MsgBox "More in the lower"
      End If
   Else
      MsgBox "Outside the box"
   End If
End Sub