How do you allow the user to interact with the graphics with the mouse?

The application I am working on will be used to create charts of the data contained in the database. Currently, the objects in the diagram are controlled using a "control bar" - essentially a list of objects and a PropertyGrid for editing values. Users would also like to be able to interact with objects using mouse interactions - things like grabbing a corner of a chart and dragging to expand / shrink it, click on a number and get a text box to edit it, or right-click something. to get a menu of possible interactions. The diagram is drawn using GDI + in a metafile (requirement), which is then drawn in a custom form.

I'm not sure how to implement this. I had a few ideas:

  • Create some custom controls that overlay the graphical graph. Each control can be associated with a specific object or property of an object in the diagram and will update these values ​​based on how the user interacted with it.
  • Just keep track of where the objects are and when the user does something with the mouse, scrolls through the list and figures out which object should be at the location of the mouse, and from there.

I'm wondering how you guys have implemented this and would really appreciate some suggestions. Thank!

+2


source to share


4 answers


A good hit detection method is to have a different image off-screen. paint each clickable object in this image with a unique color. You must turn off anti-aliasing. When the user clicks, you get the color at that point from the offscreen image and define the object. If you have a list of objects, you can use the object's index as a color. This method will handle hit detection for irregularly shaped objects, but will be slightly slower.



PS. Using the controls will be slower than that.

+1


source


The latter - if it's a pie chart, for example, you still have to do most of the bump testing work to deal with irregularly shaped elements.



+2


source


The easiest one is to have a list of objects and their bounding boxes. When the mouse event fires, check the list that the object is clicked on.

0


source


The first is that if you overlay it on a form, then all test tests will run for you within the form. You just need to create some controls and then implement event handlers for them.

The above also describes any window shapes (or perhaps MPF for that matter) :)

Create your own list of objects, etc. tantamount to reimplementing a window framework, or at least a substantial part of it. You don't want to reinvent the wheel, especially since you have wheels already in your application.

0


source







All Articles