GDI +。未能尝试放大鼠标的位置或第一次平移。 [英] GDI +. Fail to try to zoom in on the position of the mouse or pan the first time.
问题描述
大家下午好,我已经开始了这个事实,但我不知道该怎么做。你看,我正在尝试使用GDI + Engineering图形创建一个控件(我是土木工程师),一切都很顺利,直到发生以下情况:
*当尝试相对于鼠标的当前位置进行缩放或平移时,我第一次想要在控件中执行它,它会以某种方式失败,因为它总是这样做(0,0)控件,我对其轴进行转换的图形,其原点我在控件的左下角,并且正Y轴向上。但是,在我第一次使用Zoom(MouseWheel)或Pan(MouseDown和MouseMove)进行交互后,它会产生奇迹。
代码有点长,所以如果有我可以帮助我解决它,我很感激,如果我通过Visual Studio解决方案会更容易,我很乐意帮助您获得解决方案,因为如果我通过它们或者他们给我的一些联系方式会更容易,所以我们可以交换知识。
我知道这是短暂的,它必须是有条件的或类似的东西失败,并且不需要太多时间来找到错误,因为我不能。
请,我感谢有人可以帮我一把。
上帝保佑你。
我的尝试:
我试图在OnPaint中验证如果在进行TraslateTransform时它是(0,0)转移到鼠标的当前位置,但它仍然不起作用。
我也尝试过改变了Paint中的Scale和Traslate顺序,但都没有。
Good afternoon everyone, the truth I've made head to this, but I do not know what to do. You see, I'm trying to create a control to draw with GDI + Engineering graphics (I'm a Civil Engineer), and everything went fine until the following happens:
* When trying to make Zoom or Pan with respect to the current position of the Mouse, the first time I want to do it in the control, it somehow fails, since it always does so with respect to (0,0) of the control, to which Graphics I do a transformation of its axes, and its origin I have it in the lower left corner of the control, and with the positive Y AXIS up. However, after I have the first interaction of Zoom (MouseWheel) or Pan (MouseDown and MouseMove), it works wonders.
The code is somewhat long, so if a person could help me solve it, I'd appreciate it, it's easier if I pass the Visual Studio solution, I'm happy to help you get the solution, because it's easier if I passed them, or some contact that they give me, so we can exchange knowledge.
I know it is something short, it must be a conditional or something like that that fails, and do not need much time to find the error, because I could not.
Please, I appreciate that someone can give me a hand.
God bless you.
What I have tried:
I tried to verify in the OnPaint if when doing the TraslateTransform it is (0,0) to make a transfer to the current position of the Mouse, but still it does not work.
I have also tried to change the Order of Scale and Traslate in Paint, but neither.
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
'
'The current position of the Mouse is obtained.
Me._mousePositionMove = e.Location
'Check that you have pressed the left mouse button.
If e.Button = MouseButtons.Left Then
'
'It is verified that there is no saved in the variable, a data
'which indicates that the mouse has been pressed in the control.
If Not Me._mousePress = True Then
'
'The mouse button was pressed.
Me._mousePress = True
'
'Obtiene las coordenadas del puntero del Mouse cuando se presionó el Mouse.
Me._mouseDown_Location = e.Location
'Traslation Old Axis X.
Me._TraslationX_Graphics_old = Me._TraslationX_Graphics
'Traslation Old Axis Y.
Me._TraslationY_Graphics_old = Me._TraslationY_Graphics
'
'In this case the drawing is only being adjusted with its Pan and Zoom.
IsZoomPan_MouseEvent = True
'I indicate in False the variable that indicates that the drawing fits
'to the current screen.
IsFitDrawingToControl_ClikButtonEvent = False
End If
'
End If
'
'I also indicate that the reference lines of the Mouse must be drawn.
LineasReferenciaMouse_Dibujar = True
'
'The control is redrawn in your Paint event.
Me.Invalidate()
'
'Invokes the MouseDown event of the control.
MyBase.OnMouseDown(e)
End Sub
推荐答案
猜测:您可以尝试在第一次平移/缩放之前将焦点设置到控件。
Wild guess: you could try to set the focus to the control before the first pan/zoom.
yourControl.Focus()
我希望你能帮助我。
事件MouseDown
I hope you can help me.
Event MouseDown
<pre> ''' <summary>
''' Overwrites the MouseDown event of the base class of the control.
''' </summary>
''' <param name="e"></param>
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
'
'The current position of the Mouse is obtained.
Me._mousePositionMove = e.Location
'Check that you have pressed the left mouse button.
If e.Button = MouseButtons.Left Then
'
'It is verified that there is no saved in the variable, a data
'which indicates that the mouse has been pressed in the control.
If Not Me._mousePress = True Then
'
'The mouse button was pressed.
Me._mousePress = True
'
'Obtiene las coordenadas del puntero del Mouse cuando se presionó el Mouse.
Me._mouseDown_Location = e.Location
'Traslation Old Axis X.
Me._TraslationX_Graphics_old = Me._TraslationX_Graphics
'Traslation Old Axis Y.
Me._TraslationY_Graphics_old = Me._TraslationY_Graphics
'
'In this case the drawing is only being adjusted with its Pan and Zoom.
IsZoomPan_MouseEvent = True
'I indicate in False the variable that indicates that the drawing fits
'to the current screen.
IsFitDrawingToControl_ClikButtonEvent = False
End If
'
End If
'
'I also indicate that the reference lines of the Mouse must be drawn.
LineasReferenciaMouse_Dibujar = True
'
'The control is redrawn in your Paint event.
Me.Invalidate()
'
'Invokes the MouseDown event of the control.
MyBase.OnMouseDown(e)
End Sub
事件MouseMove
Event MouseMove
''' <summary>
''' Overwrites the MouseMove event of the base class of the control.
''' </summary>
''' <param name="e"></param>
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
'
'The current position of the Mouse is obtained.
Me._mousePositionMove = e.Location
'Check that you have pressed the left mouse button.
If e.Button = MouseButtons.Left Then
'
'Change cursor icon.
Me.Cursor = Cursors.SizeAll
'
'Current position of the mouse pointer
Dim mousePosNow As PointF = e.Location
'
Dim deltaX, deltaY As Single
deltaX = mousePosNow.X - Me._mouseDown_Location.X
deltaY = (mousePosNow.Y - Me._mouseDown_Location.Y) * -1 'Change Axis Y sense.
Me._TraslationX_Graphics = (Me._TraslationX_Graphics_old + (deltaX / Me._zoom))
Me._TraslationY_Graphics = (Me._TraslationY_Graphics_old + (deltaY / Me._zoom))
'
IsZoomPan_MouseEvent = True
IsFitDrawingToControl_ClikButtonEvent = False
End If
LineasReferenciaMouse_Dibujar = True
Me.Invalidate()
MyBase.OnMouseMove(e)
End Sub
Evento MouseWheel
Evento MouseWheel
<pre> ''' <summary>
''' Se produce cuando la rueda del mouse se mueve mientras el control tiene el foco.
''' Overwrites the MouseWheel event of the base class of the control.
''' </summary>
''' <param name="e"></param>
Protected Overrides Sub OnMouseWheel(e As MouseEventArgs)
Me._zoom_old = Me._zoom
If e.Delta > 0 Then
'
Me._zoom = Math.Min((Me._zoom + _zoom_increment), _zoom_max)
Else
'
Me._zoom = Math.Max((Me._zoom - _zoom_increment), _zoom_min)
End If
'
Dim mousePosNow As PointF = e.Location
'
Dim deltaX, deltaY As Single
deltaX = mousePosNow.X - 0
'
deltaY = Me.ClientRectangle.Height - mousePosNow.Y - 0
'
'Variable that save the value considering the zoom immediately
'previous of the displacements that the Graphics suffered.
Dim oldGraphicsX As Single
Dim oldGraphicsY As Single
oldGraphicsX = ((deltaX / Me._zoom_old))
oldGraphicsY = ((deltaY / Me._zoom_old))
'
'Variable to save the new displacements that the Graphics must suffer,
'so that the focus center of the Zoom is the current location of the Mouse.
Dim newGraphicsX As Single
Dim newGraphicsY As Single
newGraphicsX = ((deltaX / Me._zoom))
newGraphicsY = ((deltaY / Me._zoom))
'
Me._TraslationX_Graphics = newGraphicsX - oldGraphicsX + Me._TraslationX_Graphics
Me._TraslationY_Graphics = newGraphicsY - oldGraphicsY + Me._TraslationY_Graphics
Me._mousePositionMove = mousePosNow
'
IsZoomPan_MouseEvent = True
'
LineasReferenciaMouse_Dibujar = True
IsFitDrawingToControl_ClikButtonEvent = False
Invalidate()
'
'Envoca el evento MouseWheel del control.
MyBase.OnMouseWheel(e)
End Sub
活动油漆
Event Paint
''' <summary>
''' Overwrites the Paint event of the base class of the control.
''' </summary>
''' <param name="e"></param>
Protected Overrides Sub OnPaint(e As PaintEventArgs)
'
Me._graphicsControl = e.Graphics
'
Me._graphicsControl.Clear(GraphicsControl_ClearColor)
Me._graphicsControl.SmoothingMode = If(Me.GraphicsControl_CalidadDibujo = SmoothingMode.Invalid, SmoothingMode.AntiAlias, Me. GraphicsControl_CalidadDibujo)
'
'I check if the user has decided to draw the axes of reference X-Y
'in the bottom left corner of the control.
If Me.EjesGlobales_Dibujar = True Then
'
Transforms the axes of the graph with its origin in the lower left part of the control and with the AXIS pointing upwards (positive)
TransformarEjesGlobalesGraphics(Me._graphicsControl, Me.ClientRectangle.Height)
'
'I call the event in charge of drawing the X-Y axes.
Me.DibujarEjesControl()
'
End If
'
'I check if the user has decided to draw the reference lines of the Mousecontrol.
If Me.LineasReferenciaMouse_Dibujar = True Then
'
TransformarEjesGlobalesGraphics(Me._graphicsControl, Me.ClientRectangle.Height)
'
Me._graphicsControl.ScaleTransform(Me._zoom, _zoom)
'
Me._graphicsControl.TranslateTransform(Me._TraslationX_Graphics, Me._TraslationY_Graphics)
'
DibujarLineasReferenciaMouse(Me._mousePositionMove, Me._graphicsControl)
'
End If
'A new instance of the Zapata Class is created.
Dim myzapata As New MyDrawings.ZapataPlanta(2.5D, 3.5D, e.Graphics) With {
.Contorno_Color = Color.DarkRed,
.Contorno_GrosorPen = 0.8D,
.Relleno_Color = Color.DarkBlue,
.Relleno_ColorTransparencia = 25
}
Dim myPath As GraphicsPath = myzapata.GraphicsPath_Dibujo
Dim myPointsAll As PointF() = myPath.PathPoints
'Center of the customer area or the control needed to center the drawing on the screenla.
Dim centerControl As New PointF With {
.X = (ClientRectangle.Left + ClientRectangle.Right) / 2.0F,
.Y = (ClientRectangle.Bottom + ClientRectangle.Top) / 2.0F
}
' 'First, I verify that the drawing you want to make in the Graphics
'want to adjust to the current size of the control and in the center of it.
If Me.IsFitDrawingToControl_ClikButtonEvent = True Then
TransformarEjesGlobalesGraphics(Me._graphicsControl, Me.ClientRectangle.Height)
'The coordinates of the World are transformed, for which the Graphics previously has a matrix of Transformation
'to coordinates of the page, the points that my drawing contains or Path to show.
_graphicsControl.TransformPoints(CoordinateSpace.Page, CoordinateSpace.World, myPointsAll)
'Drawing on the X axis and Y, Centro is obtained with the sum of the minimum and maximum coordinate in the
'axis, divided between two. These coordinates are already converted to page coordinates.
Dim centerDibujo As New PointF With {
.X = (myPointsAll.Min(Function(x) x.X) + myPointsAll.Max(Function(x) x.X)) / 2.0F,
.Y = (myPointsAll.Min(Function(y) y.Y) + myPointsAll.Max(Function(y) y.Y)) / 2.0F
}
_graphicsControl.TranslateTransform(-centerDibujo.X, -centerDibujo.Y, MatrixOrder.Append)
Dim scaleRatioPage As Single
scaleRatioPage = Math.Min((ClientRectangle.Width - 30) / myPath.GetBounds.Width, (ClientRectangle.Height - 30) / myPath.GetBounds.Height)
_graphicsControl.ScaleTransform(scaleRatioPage, scaleRatioPage, MatrixOrder.Append)
_graphicsControl.TranslateTransform(centerControl.X, centerControl.Y, MatrixOrder.Append)
_graphicsControl.FillPath(myzapata.Brush_RellenoZapata, myPath)
_graphicsControl.DrawPath(myzapata.Pen_ContornoZapata(CDec(scaleRatioPage)), myPath)
'
'If the user is using Mouse events, he means
'who is wanting to make Zoom or Pan.
ElseIf Me.IsZoomPan_MouseEvent = True Then
'
TransformarEjesGlobalesGraphics(Me._graphicsControl, Me.ClientRectangle.Height)
Dim scaleRatioPage As Single
scaleRatioPage = Math.Min((ClientRectangle.Width - 30) / myPath.GetBounds.Width, (ClientRectangle.Height - 30) / myPath.GetBounds.Height)
_graphicsControl.TranslateTransform(Me._TraslationX_Graphics * _zoom, Me._TraslationY_Graphics * _zoom)
_graphicsControl.ScaleTransform(scaleRatioPage * _zoom, scaleRatioPage * _zoom)
_graphicsControl.FillPath(myzapata.Brush_RellenoZapata, myPath)
_graphicsControl.DrawPath(myzapata.Pen_ContornoZapata(CDec(scaleRatioPage * _zoom)), myPath)
End If
MyBase.OnPaint(e)
'
End Sub
这篇关于GDI +。未能尝试放大鼠标的位置或第一次平移。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!