Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Zooming with left click, panning with right click

I succeeded in getting the zoom- or pan-mode to work with buttons that set the InterActionMode Property of the waveFormGraph accordingly, in conjunction with the helper class AutoInteractionMode found in this forum.

This works well, but for a more intuitive user interface i want to be able to just zoom with the left mouse button(click,hold+move,release) and pan with the right mouse button(click,hold+move,release). I don't want to click once for activating the mode, and then click once again to zoom/pan.

Is this achievable via the helper class AutoInterActionMode? I tried it, but i think it is not possible the way i want it to be, or is it?
0 Kudos
Message 1 of 2
(3,499 Views)
You can use AutoInteractionMode for the zooming and then use another helper class for panning with the right mouse button. One way that you can do this is by handling the MouseDown, MouseUp, and MouseMove events on the graph, determining how much the mouse has moved when the right mouse button is down, then use the graph's PanXY method to programmatically pan the graph. Below is a helper class called RightMouseButtonPan that will work with either the WaveformGraph or ScatterGraph. To use it in your project, create a class member variable for RightMouseButtonPan, then initialize it in your constructor after the InitializeComponent call, like this:

public class MyForm : Form
{
private RightMouseButtonPan _rightMouseButtonPan;

// This could be a ScatterGraph as well.
private NationalInstruments.UI.WindowsForms.WaveformGraph waveformGraph1;
// ...

public MyForm()
{
InitializeComponent();

_rightMouseButtonPan = new RightMouseButtonPan(waveformGraph1);
_rightMouseButtonPan.Enabled = true;
}
}

Once this is configured, you can right-click in the plot area of the graph and pan as long as the RightMouseButtonPan.Enabled property is true. To disable this behavior, set RightMouseButtonPan.Enabled to false. Hope this helps.

- Elton

private class RightMouseButtonPan
{
private bool _enabled;
private XYGraph _graph;
private bool _isPanning;
private Point _previousPoint;

public RightMouseButtonPan(XYGraph graph)
{
if (graph == null)
throw new ArgumentNullException("graph");

_enabled = false;
_graph = graph;
_isPanning = false;
_previousPoint = Point.Empty;
}

public bool Enabled
{
get
{
return _enabled;
}

set
{
if (value && !_enabled)
{
_graph.MouseDown += new MouseEventHandler(OnGraphMouseDown);
_graph.MouseMove += new MouseEventHandler(OnGraphMouseMove);
_graph.MouseUp += new MouseEventHandler(OnGraphMouseUp);
}
else if (!value && _enabled)
{
_graph.MouseDown -= new MouseEventHandler(OnGraphMouseDown);
_graph.MouseMove -= new MouseEventHandler(OnGraphMouseMove);
_graph.MouseUp -= new MouseEventHandler(OnGraphMouseUp);
}

_enabled = value;
}
}

private void OnGraphMouseDown(object sender, MouseEventArgs e)
{
Point currentPoint = new Point(e.X, e.Y);
if ((e.Button == MouseButtons.Right) && _graph.PlotAreaBounds.Contains(currentPoint))
{
_isPanning = true;
_previousPoint = new Point(e.X, e.Y);
_graph.Capture = true;
}
}

private void OnGraphMouseMove(object sender, MouseEventArgs e)
{
if (_isPanning)
{
Point currentPoint = new Point(e.X, e.Y);
float xFactor = (float)(_previousPoint.X - currentPoint.X) / _graph.PlotAreaBounds.Width;
float yFactor = (float)(currentPoint.Y - _previousPoint.Y) / _graph.PlotAreaBounds.Height;

_graph.PanXY(xFactor, yFactor);
_previousPoint = currentPoint;
}
}

private void OnGraphMouseUp(object sender, MouseEventArgs e)
{
if (_isPanning)
{
_isPanning = false;
_previousPoint = Point.Empty;
_graph.Capture = false;
}
}
}
Message 2 of 2
(3,499 Views)