首页 > 解决方案 > 使用矩阵更新旋转后如何更新抓取手柄位置

问题描述

在此处输入图像描述我可以移动或调整对象的大小,并找到拖动手柄矩形。使用矩阵变换旋转对象后,抓取手柄移动,但手柄的实际位置保持不变。旋转后如何更新把手矩形的位置?这是绘制拖动手柄矩形的代码

 Public Sub DrawSelectedObject(ByVal g As Graphics, ByVal selectedObject As GraphicObjects, ByVal Scale As Single)

        If selectedObject Is Nothing Then
            Exit Sub
        End If

        Dim gCon1, gCon2 As Drawing2D.GraphicsContainer
        gCon1 = g.BeginContainer
        g.ScaleTransform(Scale, Scale,
            Drawing.Drawing2D.MatrixOrder.Append)
        gCon2 = g.BeginContainer
        g.PageUnit = GraphicsUnit.Pixel

        If Not selectedObject Is Nothing Then

            Dim selectionPen As New Pen(Color.FromKnownColor(KnownColor.Black))
            selectionPen.DashStyle = Drawing2D.DashStyle.Solid
            selectionPen.Width = 1
            Dim GrabHandleBrush As Brush = New SolidBrush(Color.Black)
            Dim HandlePen As New Pen(Color.FromKnownColor(KnownColor.Black))

            If selectedObject.Rotation <> 0 Then
                Dim myMatrix As Drawing2D.Matrix
                myMatrix = g.Transform()
                myMatrix.RotateAt(selectedObject.Rotation, New PointF(selectedObject.X, selectedObject.Y), Drawing.Drawing2D.MatrixOrder.Append)
                g.Transform = myMatrix

                'RotatedBitmap = RotateImage(selectedObject.Image, selectedObject.Rotation)
            End If

            DragRectangleF = New RectangleF(selectedObject.X, selectedObject.Y, selectedObject.Width, selectedObject.Height)

            Dim RectHeight As Integer = 8

            If selectedObject.Height < 50 Then
                RectHeight = 4
            ElseIf selectedObject.Height < 25 Then
                RectHeight = 2
            End If

            Drag_Handles(0) = New RectangleF(DragRectangleF.X - RectHeight / 2, DragRectangleF.Y - RectHeight / 2, RectHeight, RectHeight)
            Drag_Handles(1) = New RectangleF(DragRectangleF.X + DragRectangleF.Width / 2 - RectHeight / 2, DragRectangleF.Y - RectHeight / 2, RectHeight, RectHeight)
            Drag_Handles(2) = New RectangleF(DragRectangleF.X + DragRectangleF.Width - RectHeight / 2, DragRectangleF.Y - RectHeight / 2, RectHeight, RectHeight)
            Drag_Handles(3) = New RectangleF(DragRectangleF.X + DragRectangleF.Width - RectHeight / 2, DragRectangleF.Y + DragRectangleF.Height / 2 - RectHeight / 2, RectHeight, RectHeight)
            Drag_Handles(4) = New RectangleF(DragRectangleF.X + DragRectangleF.Width - RectHeight / 2, DragRectangleF.Y + DragRectangleF.Height - RectHeight / 2, RectHeight, RectHeight)
            Drag_Handles(5) = New RectangleF(DragRectangleF.X + DragRectangleF.Width / 2 - RectHeight / 2, DragRectangleF.Y + DragRectangleF.Height - RectHeight / 2, RectHeight, RectHeight)
            Drag_Handles(6) = New RectangleF(DragRectangleF.X - RectHeight / 2, DragRectangleF.Y + DragRectangleF.Height - RectHeight / 2, RectHeight, RectHeight)
            Drag_Handles(7) = New RectangleF(DragRectangleF.X - RectHeight / 2, DragRectangleF.Y + DragRectangleF.Height / 2 - RectHeight / 2, RectHeight, RectHeight)
            'last 2 drag handles are for rotating object
            Drag_Handles(8) = New RectangleF(DragRectangleF.X + DragRectangleF.Width - RectHeight / 2, DragRectangleF.Y + 10, RectHeight, RectHeight)
            Drag_Handles(9) = New RectangleF(DragRectangleF.X + DragRectangleF.Width - RectHeight / 2, DragRectangleF.Y + DragRectangleF.Height - 20, RectHeight, RectHeight)

            g.DrawRectangle(New Pen(Color.Gray), DragRectangleF.X, DragRectangleF.Y, DragRectangleF.Width, DragRectangleF.Height)
            g.FillRectangles(Brushes.Gray, Drag_Handles)
            g.DrawRectangles(Pens.Black, Drag_Handles)

            Dim RotationDragHandle As New RectangleF()
            RotationDragHandle = Drag_Handles(8)
            g.FillRectangle(Brushes.YellowGreen, RotationDragHandle)

            Dim RotationDragHandle1 As New RectangleF()
            RotationDragHandle1 = Drag_Handles(9)
            g.FillRectangle(Brushes.YellowGreen, RotationDragHandle1)

        End If

        g.EndContainer(gCon2)
        g.EndContainer(gCon1)

    End Sub

下面是在 Mouse Move Sub 过程中查找抓取 Handle Rectangles 的代码

 Dim tmprect As RectangleF
        Dim i As Integer = 0
        Dim R As New RectangleF
        gobjc.LoadDrag_Handles()
        Dim OffSetX As Single = 0
        Dim OffSetY As Single = 0

        For Each tmprect In gobjc.Drag_Handles
            If tmprect.Contains(e.X / Zoom, e.Y / Zoom) Then
                MouseInRect = True
                ActiveDrag = i
                SetDragCursor()
                Invalidate()
                Return
            End If
            i += 1
        Next

        If gobjc.DragRectangleF.Contains(e.X / Zoom, e.Y / Zoom) Then
            MouseInRect = True
            Cursor = Cursors.Hand
            Invalidate()
            Return
        End If

标签: transformconfusion-matrix

解决方案


推荐阅读