1

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

下面是 OnResize 方法的代码,但要完全理解问题,您可以按照此Google Drive Link操作,它会为您提供我正在处理的问题的简短版本/应用程序。

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

谢谢,

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
4

2 回答 2

1

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

于 2021-07-05T06:45:42.890 回答
0

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

这个想法是找出我的移动/调整红色矩形的左上角在 PictureBox 矩形 ABCD 中的位置。它是在矩形 ABC 中(影响右侧)还是在矩形 ADC 中(影响底部)。然后,根据情况调整正确的编码。

请按照此链接获取有关数学方面的更多说明。

    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
于 2021-07-09T20:09:43.510 回答