0

I wrote a code to draw a line using the mouse.

On mouse down i save where the user clicked.

    bool mZgc_MouseDownEvent(ZedGraphControl sender, MouseEventArgs e)
    {
        GraphPane graphPane = mZgc.GraphPane;
        graphPane.ReverseTransform(e.Location, out mMouseDownX, out mMouseDownY);
        return false;
    }

On mouse up i draw the line:

    bool zgc_MouseUpEvent(ZedGraphControl sender, MouseEventArgs e)
    {
        GraphPane graphPane = mZgc.GraphPane;
        double x, y;

        graphPane.ReverseTransform(e.Location, out x, out y);
        LineObj threshHoldLine = new LineObj(Color.Red, mMouseDownX, mMouseDownY, x, y);
        graphPane.GraphObjList.Add(threshHoldLine);
        mZgc.Refresh();

        return false;
    }

The problem is that while the mouse is down, the user don't see the line (because i draw it only on "up" event).

How can i solve it ?

Technically I can use "on hover" and draw/remove the line from the graph each second and refresh the graph, but it's a bit crazy.

Is there a "normal" way to do this ?

Thanks

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
OopsUser
  • 4,642
  • 7
  • 46
  • 71

1 Answers1

0

Ok, I've solved the problem I'll post the answer for future generations.

First of all we need to make a minor change in the zedgraph code to allow ourselves to change lines after they created, It probably breaks some "immutable" paradigm that the creators are trying to achieve, but if you want to avoid creating and deleting lines every time the mouse is moving that's the way.

In the file Location.cs edit the X2,Y2 properties (set was missing):

    public double X2
    {
        get { return _x+_width; }
        set { _width = value-_x; }
    }

    public double Y2
    {
        get { return _y+_height; }
        set { _height = value-_y; }
    }

from here it is easy, i won't post all the code but will explain the steps:

  1. Add a member to your class : private LineObj mCurrentLine;
  2. On mouse down create a line mCurrentLine = new LineObj(Color.Red, mMouseDownX, mMouseDownY, mMouseDownX, mMouseDownY);
  3. On mouse move change the line X2,Y2 coordinates mCurrentLine.Location.X2 = x;
  4. On mouse up just stop that drawing process (to avoid changing the line in "on mouse move"

If someone actually going to use it, and needs better explanation, just comment.

OopsUser
  • 4,642
  • 7
  • 46
  • 71