473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Adding event (mouseclick) handlers to GDI+ shapes?

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
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Sep 4 '06 #1
6 5142
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...

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

Thanks

On Tue, 05 Sep 2006 08:21:22 +0100, Steven Nagy <le*********@hotmail.com>
wrote:
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...


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Sep 5 '06 #3
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.

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

Thanks

On Tue, 05 Sep 2006 08:21:22 +0100, Steven Nagy <le*********@hotmail.com>
wrote:
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...

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Sep 5 '06 #4
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.

Sep 5 '06 #5
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

On Wed, 06 Sep 2006 00:24:49 +0100, Steven Nagy <le*********@hotmail.com>
wrote:
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.


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Sep 6 '06 #6
Note to self: Attach test code.

On Wed, 06 Sep 2006 00:24:49 +0100, Steven Nagy <le*********@hotmail.com>
wrote:
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.


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Sep 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: chandramohan.mani | last post by:
Does Event handlers work in netscape. If yes means can anyone please help me. <HTML><SCRIPT LANGUAGE="JScript"> function mouseclick() { alert("I was clicked on " +...
4
by: Bob Rock | last post by:
Hello, is there a way to dynamically query if a callback delegate has been tied to an event to avoid raising the event if none have been tied or raise an exception if proper for the situation???...
1
by: Adam | last post by:
In Winforms, in Visual Studio, how does one add multiple event handlers in the properties box for let's say, a textbox TextChanged event? Thanks in advance. Adam
6
by: Jacky Luk | last post by:
I want to include some code for the MouseDown Event. I would like to add this for the formview or a picturebox, how come there are no messages for me to choose from. Even I created a button, no...
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
5
by: Don | last post by:
Is there a way to capture click events in the form designer? I'm trying to create a control similar to the TabControl (which seems to handle clicks in design mode) and would like to be able to...
3
by: Ryan McFall | last post by:
Hi: I am trying to write a short program that demonstrates switching between two different event handlers when a mouse click occurs. Here's the relevant code: public delegate void...
21
by: John Henry | last post by:
Hi list, I have a need to create class methods on the fly. For example, if I do: class Dummy: def __init__(self): exec '''def method_dynamic(self):\n\treturn self.method_static("it's...
4
by: =?Utf-8?B?Z3JlZw==?= | last post by:
Hi. I'm trying to implement a function. Users are able to control-click on components. This turns the component red, and does multi-selection and does some copying/pasting. I'm having problems...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.