none
Zoom in by dragging a rectangle RRS feed

  • Question

  • Hi.

    I'm trying to create my own toolbar to interact with MSAGL viewer, but it's missing a function that do the "Zoom in by dragging a rectangle" button action.

    Can anyone help me with this?

    Thanks.
    Paulo Azevedo
    Tuesday, November 11, 2008 7:33 PM

Answers

  • The mouse position in the graph coordinates is given by GraphViewerGdi.ScreenToSource().

    Thanks,

    Lev


    Lev Nachmanson

    Thank you very much. I figured it out and got a working result:

      Private zoomByRect As Boolean = False 'needed for mouseup event
      Private zoomRectLeft, zoomRectTop As Double 'start positions of the rect
    
      Private Sub Viewer_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Viewer.MouseDown
        If Not Me.isPanning Then 'this is an private attribute which i have declared in order to differ between Panning actions and zooming actions
          Me.zoomByRect = True 'set zooming action true -> will be checked in mouseupevent sub
          zoomRectLeft = CDbl(e.X) 'set start x pos
          zoomRectTop = CDbl(e.Y) 'set start y pos
        End If
      End Sub
    
      Private Sub Viewer_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Viewer.MouseUp
        If Me.zoomByRect Then 'check if the mouseup is zoomaction
          Dim newCenter As Microsoft.Msagl.Point 'ne screen center
          Dim newCenterX, newCenterY As Double 'bottem and right border positions
          Dim rectWidth, rectHeight As Double 'width and height of the rect
          rectWidth = Math.Max(zoomRectLeft, CDbl(e.X)) - Math.Min(zoomRectLeft, CDbl(e.X)) 'width = right border (max) - left border (min)
          rectHeight = Math.Max(zoomRectLeft, CDbl(e.X)) - Math.Min(zoomRectLeft, CDbl(e.X)) 'same as width
    
          newCenterX = Math.Min(zoomRectLeft, CDbl(e.X)) + rectWidth / 2 'the new x position of the center is the left border + the half of the width
          newCenterY = Math.Min(zoomRectTop, CDbl(e.Y)) + _
                     (Math.Max(zoomRectTop, CDbl(e.Y)) - Math.Min(zoomRectTop, CDbl(e.Y))) / 2 'same as newCenterX
          newCenter = New Microsoft.Msagl.Point(newCenterX, newCenterY) 'save the x and y in a msgal.point
    
          Dim zoomFactor As Double = Math.Min(Viewer.Width / rectWidth, Viewer.Height / rectHeight) 'the zoomfactor is calculated by width of the viewer screen divided by the width of the zoom rect (new screen width)
          Viewer.ZoomF *= zoomFactor 'zoom in first!
    
          newCenter = Viewer.ScreenToSource(newCenter) 'get new center
          Viewer.CenterToPoint(newCenter) 'after zoom, pan!
    
          Me.zoomByRect = False 'end zooming action
        End If
      End Sub
    

    Hope it works for you too :)

    kind regards

    Friday, March 25, 2011 8:47 AM

All replies

  • Paulo,

    You can try a combination of two members of Gviewer:  ZoomF and Pan. First you call ZoomF to set the correct scale and then you shift with Pan to the new center. I do not have a sample but I hope it will work.

    Thanks,

    Lev

     

    Monday, November 17, 2008 3:53 PM
  • i have the same problem... and i allready thought of this functions... but how can i get the mouse position? if i dont get the coordinations i can not pan ;)
    Thursday, March 24, 2011 9:45 AM
  • The mouse position in the graph coordinates is given by GraphViewerGdi.ScreenToSource().

    Thanks,

    Lev


    Lev Nachmanson
    Thursday, March 24, 2011 6:22 PM
  • The mouse position in the graph coordinates is given by GraphViewerGdi.ScreenToSource().

    Thanks,

    Lev


    Lev Nachmanson

    Thank you very much. I figured it out and got a working result:

      Private zoomByRect As Boolean = False 'needed for mouseup event
      Private zoomRectLeft, zoomRectTop As Double 'start positions of the rect
    
      Private Sub Viewer_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Viewer.MouseDown
        If Not Me.isPanning Then 'this is an private attribute which i have declared in order to differ between Panning actions and zooming actions
          Me.zoomByRect = True 'set zooming action true -> will be checked in mouseupevent sub
          zoomRectLeft = CDbl(e.X) 'set start x pos
          zoomRectTop = CDbl(e.Y) 'set start y pos
        End If
      End Sub
    
      Private Sub Viewer_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Viewer.MouseUp
        If Me.zoomByRect Then 'check if the mouseup is zoomaction
          Dim newCenter As Microsoft.Msagl.Point 'ne screen center
          Dim newCenterX, newCenterY As Double 'bottem and right border positions
          Dim rectWidth, rectHeight As Double 'width and height of the rect
          rectWidth = Math.Max(zoomRectLeft, CDbl(e.X)) - Math.Min(zoomRectLeft, CDbl(e.X)) 'width = right border (max) - left border (min)
          rectHeight = Math.Max(zoomRectLeft, CDbl(e.X)) - Math.Min(zoomRectLeft, CDbl(e.X)) 'same as width
    
          newCenterX = Math.Min(zoomRectLeft, CDbl(e.X)) + rectWidth / 2 'the new x position of the center is the left border + the half of the width
          newCenterY = Math.Min(zoomRectTop, CDbl(e.Y)) + _
                     (Math.Max(zoomRectTop, CDbl(e.Y)) - Math.Min(zoomRectTop, CDbl(e.Y))) / 2 'same as newCenterX
          newCenter = New Microsoft.Msagl.Point(newCenterX, newCenterY) 'save the x and y in a msgal.point
    
          Dim zoomFactor As Double = Math.Min(Viewer.Width / rectWidth, Viewer.Height / rectHeight) 'the zoomfactor is calculated by width of the viewer screen divided by the width of the zoom rect (new screen width)
          Viewer.ZoomF *= zoomFactor 'zoom in first!
    
          newCenter = Viewer.ScreenToSource(newCenter) 'get new center
          Viewer.CenterToPoint(newCenter) 'after zoom, pan!
    
          Me.zoomByRect = False 'end zooming action
        End If
      End Sub
    

    Hope it works for you too :)

    kind regards

    Friday, March 25, 2011 8:47 AM