OnResize sub 在 VB.net 中没有完全工作

OnResize sub is not fully working in VB.net

我正在尝试调整包含图像的 PictureBox 中红色矩形的大小(通过继承 PictureBox 的 class),但我对 OnResize 方法几乎没有问题。 我只能使用右下角的角调整此框架的大小,这会将框架的比例保持为 1.5(横向)。但是,当我调整红色矩形的大小时,调整大小的动作应该在它触及右侧或底部时停止,但它只是部分工作:停止在右侧,但在底部继续(见图片)。

下面是 OnResize 方法的代码,但要完全理解这个问题,您可以按照此 Google Drive Link 进行操作,它将简短地 version/application 说明我对这个问题所做的工作。

显然欢迎任何想法,因为有些东西我不明白。

谢谢,

JLuc

    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
    Try
        ' Minimum limits
        If Me.Width < 40 Then Me.Width = CInt(40 * Form1.dRatioImageWH)
        If Me.Height < 40 Then Me.Height = CInt(40 / Form1.dRatioImageWH)
        ' Keeping the ratio Width/Height = 1.5 (Landscape)
        If Form1.dRatioImageWH > 1 Then Me.Height = CInt(Me.Width / Form1.dRatioImageWH)
        ' Effect on Resize event
        If Me.Width > Form1.PictureBox1.Width - Me.Location.X Then Me.Width = Form1.PictureBox1.Width - Me.Location.X
        If Me.Height > Form1.PictureBox1.Height - Me.Location.Y Then Me.Height = Form1.PictureBox1.Height - Me.Location.Y
        ' Control to be redrawn
        Me.Invalidate()
        ' Raise the Resize event
        MyBase.OnResize(e)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

我记得在 OnResize 上遇到过类似的问题,直到我意识到还存在 OnResizeEnd,它每次都会触发。

我找到了实现我想要的方法。但是,听起来有点复杂,如果你发现更好的,请不要犹豫告诉我。

我的想法是找出我的 moving/resizing 红色矩形的左上角在 PictureBox 矩形 ABCD 中的位置。它是在矩形 ABC 中(撞击右侧)还是在矩形 ADC 中(撞击底部)。然后,根据情况调整合适的编码。

关注此 LINK 以获得更多数学方面的解释。

    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
    ' To redraw the whole image based on Glass window
    Try
        ' Minimum limits of Glass window
        If Me.Width < 40 Then Me.Width = CInt(40 * Form1.dRatioImageWH)
        If Me.Height < 40 Then Me.Height = CInt(40 / Form1.dRatioImageWH)
        ' ------------------------------------------------------------
        ' Adjust the right coding after checking if the Point P is within the appropriate triangle 
        ' How do I check whether a given point lies inside a triangle whose coordinates are given?
        ' If Area of ABC == Area of (PAB + PBC + PAC), then Point P is inside the triangle ABC
        ' ------------------------------------------------------------
        Dim A As New Point
        A.X = 0
        A.Y = 0
        Dim B As New Point
        B.X = 400
        B.Y = 0
        Dim C As New Point
        C.X = 400
        C.Y = 266
        Dim P As New Point
        P.X = Me.Location.X
        P.Y = Me.Location.Y
        ' Area of Triangle ABC (upper half of the PictureBox)
        Dim areaABC As Integer
        areaABC = (A.X * (B.Y - C.Y) + B.X * (C.Y - A.Y) + C.X * (A.Y - B.Y)) / 2
        ' Area of 3 Triangles inside the Triangle ABC based on Point P
        Dim areaPAB As Integer
        areaPAB = (P.X * (A.Y - B.Y) + A.X * (B.Y - P.Y) + B.X * (P.Y - A.Y)) / 2
        Dim areaPBC As Integer
        areaPBC = (P.X * (B.Y - C.Y) + B.X * (C.Y - P.Y) + C.X * (P.Y - B.Y)) / 2
        Dim areaPAC As Integer
        areaPAC = (P.X * (A.Y - C.Y) + A.X * (C.Y - P.Y) + C.X * (P.Y - A.Y)) / 2
        ' Target: keep the ratio Width/Height when resizing
        If (areaABC > areaPAB + areaPBC + areaPAC) = True Then
            ' Point P in Triangle ABC (upper half of the PictureBox)
            If Me.Height > Form1.PictureBox1.Height - Me.Location.Y Then
                Me.Height = Form1.PictureBox1.Height - Me.Location.Y
            Else
                Me.Height = CInt(Me.Width / Form1.dRatioImageWH)
            End If
        Else
            ' Point P in Triangle ADC (lower half of the PictureBox)
            If Me.Width > Form1.PictureBox1.Width - Me.Location.X Then
                Me.Width = Form1.PictureBox1.Width - Me.Location.X
            Else
                Me.Width = CInt(Me.Height * Form1.dRatioImageWH)
            End If
        End If
        ' Control to be redrawn
        Me.Invalidate()
            ' Raise the Resize event
            MyBase.OnResize(e)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub