Adding event (mouseclick) handlers to GDI+ shapes?


K

Kristian Frost

Hi,
I'm trying to add, as you might guess, mouseclick listeners to the shapes
I am drawing using the GDI+ commands in a similar sort of way as could be
done with the old VB "shapes".
Problem is, I'm really not sure of the best way to go about it.
I was hoping I could do it directly like the old shape_onclick events, but
that doesn't seem to be available for GDIs so maybe I could hack something
together using collections of GDI objects and checking to see if
mouseclick events occurred within the boundaries of the shapes stored in
the collection...
I'm not sure how that would work with irregular shapes such as curved
lines, among other problems.

Any advice you could give on this topic would be gratefully received.
New, less-fiddly-to-code directions to follow would also be appreciated.

KF
 
Ad

Advertisements

S

Steven Nagy

Well it sounds to me like you are creating a custom control.
So perhaps you should consider having your control inherit from
UserControl
(Damn its been too long, is that even the right class???)
Then, you will have basic functionality for events; I'm sure
OnMouseDown is one of them. Then you customise how your control works
by handling the OnPaint event and customising what gets drawn.

So say you draw a circle. When you run your onmousedown code, you can
write some mathematical code that takes the event arguments x/y mouse
click location, and detects if its inside the boundary of your shape.

Its a starting point I guess...
 
K

Kristian Frost

Yes, that was pretty much what I was thinking I'd have to do. Shame the
Shape object is dead, really.

Thanks
 
C

Chris Dunaway

If you search theCodeProject.com, you should find some shape controls
made by others. You could probably look at their code to see how they
are handling their events.
 
S

Steven Nagy

Its not dead while you still believe...
Yes, that was pretty much what I was thinking I'd have to do. Shame the
Shape object is dead, really.
Anyway, its going to be the same result in the end. Just a different
implementation.
Create yourself a shapecontrol class that inherits from UserControl and
add some key properties for defining the clickable boundaries and the
drawing details. I never knew older non-OO versions of VB so I don't
know what the exact nature of your old shape objects were. But I bet
you can still simulate it. Once you do, let me know... I'd like to see
it in action.
 
K

Kristian Frost

Little bit of test-code that seems to be doing what I want it to.
Starts by creating a collection of the class written at the bottom of the
file, which contains a SolidBrush, Rectangle and a Boolean.
MouseDown and MouseUp are handled and their click-coordinates stored, and
then checks are made against each shape in the collection in turn to see
if the click has fallen within the bounds of any of them. If the click was
within the bounds of a shape, it changes colour. If a shape is clicked a
second time, a third colour is used, showing that the shape is "off" now,
but was "on" at some time in the past.
It's all very quick-and-dirty, hurts my eyes after a few minutes, and it
only works for Rectangles at the moment, but it does what I need at a
level where Button objects just won't cut it.

Another question, though:
Can I get the Graphics object to not actually refresh until *after* it has
added all the Rectangles? At the moment it seems to be wasting a lot of
time on refreshing when each component is added, which is fairly
unneccessary.

All going well so far, thanks for the help.
KF
 
Ad

Advertisements

K

Kristian Frost

Note to self: Attach test code.

Its not dead while you still believe...


Anyway, its going to be the same result in the end. Just a different
implementation.
Create yourself a shapecontrol class that inherits from UserControl and
add some key properties for defining the clickable boundaries and the
drawing details. I never knew older non-OO versions of VB so I don't
know what the exact nature of your old shape objects were. But I bet
you can still simulate it. Once you do, let me know... I'd like to see
it in action.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Adding an event handler in a sub 6
Adding keydown event handler 4
Store added event handlers 5
Adding an event handler for a socket object 1
Event Handlers 1
Event Handler 2
Event handlers 6
Event Handler 2

Top