Nevron Forum

Zoom to current mouse co-ordinates using Mousewheel

https://www.nevron.com/Forum/Topic4188.aspx

By Pramod Sreekanthan - Wednesday, September 15, 2010

Is it possible to use mouse wheel to zoom-in/zoom-out to the current mouse coordinates. Default Zoom behavior is to zoom with reference to center. How to override this behavior and zoom to current mouse points?

By Nevron Support - Wednesday, September 15, 2010

Hi,

you can easily zoom to the location of the mouse using the following code for example:

view.Zoom(2, view.GetMousePositionInScene());

By Pramod Sreekanthan - Wednesday, September 15, 2010

Here the issue are:

1.Mouse wheel moves mouse point and hence every time mouse point changes and hence zooming-in/zoom-out does not work (cannot get the enlarged view of a point).

2. Scroll bar move instead of zooming in/out

We want to achieve the effect of clicking the zoom buttons of the tool bar (+ and - icons), when the mouse wheel used. How to achieve this?

By Pramod Sreekanthan - Wednesday, September 15, 2010

Updated Issues are :

1.Moving the mouse wheel, scrolls the screen and hence everytime mouse point changes and hence zooming-in/zoom-out does not work (cannot get the enlarged view of a point).

2. Scroll bar moves instead of zooming in/out

3. View point is changing with every mouse scroll

We want to achieve the effect of clicking the zoom buttons of the tool bar (+ and - icons), when the mouse wheel used and it should zoom from the current view point. How to achieve this?

By Nevron Support - Thursday, September 16, 2010

As every .NET control, the view must be focused in order to receive the MouseWheel event ! Again, as you can do with any .NET control that can have focus, you can use the Focus() method to move the focus to it (i.e. call view.Focus() when your form is shown).

By Pramod Sreekanthan - Thursday, September 16, 2010

Here Focus() does not help as the issue is

1.Let say before mouse is scrolled the co-ordinates are X,Y. When mouse wheel is turned co-ordinates will change to X+delta,Y+delta. Hence the new view points will be X+delta,Y+delta.

2.But what was required is to Zoom the view at X,Y and keep the view point constant but enlarge the view. Same as '+' and '-' icons for the zoom.

3. Assume, Mouse points are always on the drawing.

I've put the code below

this.nDrawingDocument1.MouseWheel += new NodeMouseEventHandler(nDrawingDocument1_MouseWheel);

void nDrawingDocument1_MouseWheel(NNodeMouseEventArgs args)
        {
            this.nDrawingDocument1.Settings.ZoomStep = 0.1f;
            if (args.Delta < 0)
            {
                //Zoom in
                nDrawingView1.Zoom(2, nDrawingView1.GetMousePositionInScene());
                                
            }
            else
            {
                //Zoom out
                nDrawingView1.Zoom(0.5f, nDrawingView1.GetMousePositionInScene());
               
            }

            nDrawingView1.Focus();
        }

Please let me know if this does not clarify the requirement.

By Nevron Support - Thursday, September 16, 2010

Hi, thanks for describing you requirement better. We recommend you create a class that inherits the mouse wheel tool:

 

public class MyWheelTool : NMouseWheelScrollAndZoomTool

{

      public MyWheelTool()

      {

            this.Enabled = true;

            this.ScrollByDefault = false;

            m_Location = NPointF.Empty;

      }

 

      protected override bool DoWheelZoom(int delta)

      {

            if (AllowWheelZooming == false)

                  return false;

 

            float zoom = View.ScaleX;

            zoom += delta > 0 ? Document.Settings.ZoomStep : -Document.Settings.ZoomStep;

            if (View.IsValidScaleFactor(zoom) == false)

                  return false;

 

            if (View.IsValidScaleFactor(zoom) == false)

                  return false;

 

            NPointF p1 = View.GetMousePositionInScene();

            if (m_Location == NPointF.Empty ||

                  m_Location.Distance(p1) > 300)

            {

                  m_Location = p1;

            }

 

 

            // Zoom the view

            View.Zoom(zoom, m_Location);

 

            return true;

      }

 

      private NPointF m_Location;

}

 

Then all you have to do is to replace the default mouse wheel tool with your custom tool:

 

private void ApplyMouseWheelTool()

{

      // replace the default mouse wheel tool

      NTool tool = view.Controller.Tools.GetToolByName(NDWFR.ToolMouseWheelScrollAndZoom);

      int index = view.Controller.Tools.IndexOf(tool);

      view.Controller.Tools.RemoveAt(index);

      view.Controller.Tools.Insert(index, new MyWheelTool());

}

By Pramod Sreekanthan - Friday, September 17, 2010

Thanks for the reply. This has answered the question but we face the following issues

1.The zoom point is not actually current mouse co-ordinate, is scenecoordinate different than current mouse points?

2. Over all zoom is random, screen moves randomly to the right/left/corner.

 

By Nevron Support - Friday, September 17, 2010

Hi,

regarding your questions:

1. The point we use is the point in scene coordinates which is different than the point in device (i.e. screen) coordinates. But this is the correct point that should be used because the viewport location is measured in the scene coordinates of the view.

2. The provided code is only an example, feel free to modify the logic in the DoWheelZoom method if you wish. But when there's no scrolling available the zooming base on a specified point is not possible, because the whole drawing view is visible, so it cannot be moved around.