transform - 使用矩阵更新旋转后如何更新抓取手柄位置
问题描述
我可以移动或调整对象的大小,并找到拖动手柄矩形。使用矩阵变换旋转对象后,抓取手柄移动,但手柄的实际位置保持不变。旋转后如何更新把手矩形的位置?这是绘制拖动手柄矩形的代码
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
解决方案
推荐阅读
- javascript - 如何从成员那里获取消息然后将其发送到频道
- sql - 选择表中的最新记录
- asp.net-core - 如何将包含excel文件的表单数据发送到asp.net core中的控制器
- python - 基于多个条件和多个值分配变量
- ios - 如何在 M1 MacBook Pro 上的 Xcode 项目中下载 pod
- java - AES的计数器模式的计数器是如何递增的?
- python - 如何在 Python 中使用 Pandas 解析列中特定范围的数值?
- apache-spark - 重用pyspark缓存并在for循环中不持久
- excel - 为 Excel VBA 创建常量库的理想方法是什么?
- python - 无法使用 for 循环为我的值迭代列表