Click to See Complete Forum and Search --> : Events for classes
limpit
November 2nd, 2009, 12:05 AM
I am using Visual C++ Express Edition 2008, and i was wondering if there is any way to click an object drawn by a class in a canvas. Is there any way to add an event to each object generated by the class? For example link the object to the event click or something similar? I need help really urgently, thanks in advance.
cilu
November 2nd, 2009, 02:13 AM
Are you talking about a win forms application?
limpit
November 2nd, 2009, 03:25 AM
yes it is a windows forms application, which involves drawing rectangles in a canvas, and i want to be able to click them. Is there a way to code an event on the class or something like that?
cilu
November 2nd, 2009, 04:33 AM
As I understand correctly you draw rectangles on the canvas and you want to treat them as objects and be able to select or move them. Well, it's not that simple. What you have to do is storing information about all the objects that you draw (like position, size, color, text, etc.). You add events for clicking on the canvas, and in the handler you check if the cursor is over one of the objects. In that case you do something; maybe you draw a tracking rectangle around it, or paint it in another color, to show a "selection". It all depends on what you want to do. But there is no other way to handle this.
cilu
November 2nd, 2009, 04:34 AM
[ redirected ]
limpit
November 2nd, 2009, 05:07 AM
yes that is exactly what i am trying to do. I want the rectangle to change color once it has been clicked. Anyway, how do i get the mouse position as a coordinate inside the canvas? I do have the position for the objects.
Alex F
November 2nd, 2009, 05:36 AM
Take a look at this sample:
http://www.codeproject.com/KB/graphics/drawtools.aspx
It is written in C#, you need to translate it to C++/CLI. Though, for such kind of tasks, C# is much better.
limpit
November 2nd, 2009, 06:34 AM
Thanks very much, I found the way to make it work although i still have a little problem. The position that is stored as a property for the class object which stands for its X and Y position is relative to the panel it is drawn in. On the other hand, the coordinate that you get by the click event of the mouse in the panel, returns the position of the mouse relative to the screen. I need to get both the same way (I would prefer relative to the panel) and dont know how to do this.
Alex F
November 2nd, 2009, 07:06 AM
See Control.PointToClient and Control.PointToScreen methods.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.