对于整数鼠标事件,矩阵变换的2D图形太小了?

时间:2015-01-24 02:03:23

标签: vb.net 2d mouseevent gdi+

我正在设计一个简单的应用程序,使用DrawLine()和DrawEllipse()函数将一些笛卡尔类型的图形显示到PictureBox控件中。为了使坐标系更加“真实世界”而不是图片框,我使用矩阵来翻转Y轴,缩小所有内容并重新定位,使(0,0)位于屏幕的中心(+ 2,+ 2)位于右上角。一切都适用于绘制图形。但是,在尝试读取鼠标事件时,似乎MouseEventArgs变量(由大多数鼠标事件返回)将鼠标位置X和Y作为整数返回。我正在使用倒置矩阵来检索缩放值处的坐标,但是在我使用的比例下,这将不能作为整数工作,因为我需要在小数值(1.5,1.6等)中的屏幕位置。

有没有办法将鼠标值检索为浮点数或双/小数值,这将给出我需要的“分辨率”?

一些代码片段:

- 全球

Private MyTransform As Matrix

- 在图片框内

Paint() event  
Dim G As Graphics = e.Graphics  
Dim mx As New Matrix(1, 0, 0, -1, 0, 0) 'Y-axis orientation flipped to match Cartesian plane  
mx.Translate(PictureBox.Width / 2, -PictureBox.Height / 2) 'Move 0,0 to lower left corner  
mx.Scale(100, 100)  
G.Transform = mx  
MyTransform = G.Transform  

'所有绘图都在此时执行并且正常工作。

- 在MouseDown事件中

MyTransform.Invert()  
'Here is the issue--the Mouse points returned, being integers, cannot properly  
'show the mouse point if the transformation matrix has scaled up the drawing space at all.  
e.Location.x 'is an integer, so it cannot show .01 as the proper mouse location within the transformed viewspace.  
e.Location.y 'same issue.  
MyTransform.Invert()  

我已经找到了一个基于笛卡尔坐标的图片框替代无济于事,图表组件将无法工作,因为它们要求绘制的点包含在他们自己的专有容器/集中。我正在使用GDI类型的方法完成所有绘图。唯一的选择似乎是避免使用vb进行转换并自己进行所有翻译/解译,除非有人有替代或示例建议......?

1 个答案:

答案 0 :(得分:0)

Scale存储为属性。然后在更改比例时,将函数应用于鼠标坐标差异。示例将使用10/1平面,因此Scale将为10

Private Property Scale As Single 

功能:

Private Function CorrectForScale(coord As Integer) As Single
   Return (coord / Scale)
End Function

现在,如果鼠标位置之间的距离差异= 3,那么函数之后的结果将为0.3