Mouse event handlers


R

Roger Crawfis

I am writing a 3D graphics demo for my students, and thought I had a really
slick solution for handling the interaction with the mouse. There are
several ways to interact with a 3D world, and I have attached a mouseDown
event handler to my OpenGL panel (control). What I want to do, is based upon
which mouse button was pressed associate the proper mouseMove event handler
with the control. OK, so I have a From with several controls, including a
few OpenGL controls I created. I attached the mousedown to an OpenGL
control. In the callback I want to associate the MouseMove and MouseUp event
handlers. I first tried something like this in the MouseDown event handler:

m_MouseMoveDelegate = new MouseEventHandler( camera.Trackball );

this.MouseMove += m_MouseMoveDelegate;

m_MouseUpDelegate = new MouseEventHandler( My_MouseUp );

this.MouseUp += m_MouseUpDelegate;

This associates the mouse handlers with the entire Form though, not the
control. I am rather clueless on the sender semantics. I tried casting it to
my UserControl (OpenGLPanel), but that led to very bad behavior. Not sure
why. I also need to retained the mouse position of the mouse down event.



The problem above is either the "this" identifiers, or trying to save the
mouse position from the mouse down event. Casting this to screen space
provides somewhat better behavior, but not ideal.

Questions:

1) Is there something fundamentally wrong with this design?

2) Is the sender one of the controls I registered the MouseDown event
handler to?

3) In particular, is it the control in which I pressed the mouse?

4) Can I associate a MouseMove event to the sender? Note, I do not want to
embed my form logic in here and test for which control, etc.





FYI. The mouseUp event simply removes the event handlers:

// Disconnect the MouseEventHandler

this.MouseMove -= m_MouseMoveDelegate;

this.MouseUp -= m_MouseUpDelegate;
 
Ad

Advertisements

S

Sijin Joseph

Hi Roger,

'this' refers to whichever class you are writing the code in, so if the
code you have given is written in the Form class then attaching the
handlers would mean attaching to the Forms mouse event handlers.

If you want to write the code in the form class then do something like

m_MouseMoveDelegate = new MouseEventHandler( camera.Trackball );
this.<OpenGLPanel>.MouseMove += m_MouseMoveDelegate;

m_MouseUpDelegate = new MouseEventHandler( My_MouseUp );
this.<OpenGLPanel>.MouseUp += m_MouseUpDelegate;

where OpenGLPanel is the name of the OpenGLPanel variable whose mouse
move event you want to handle.

The sender is the control in which the event has occured, so it should
be the panel itself.

Since the sender is of type object, make sure you cast sender as control
before hooking the event handlers to it.

Hope that helps, let me know if you face any difficulties.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Ad

Advertisements

R

Roger Crawfis

Unfortunately, I may have many OpenGLPanel controls all hooked to the same
MouseUp event handler. I tried to cast the sender to a control, but the
mouse positions seemed to be off. I will try it again.

I figured it out. I only cast the sender when attaching the events. I also
set this.Capture, when I should have set sender.Capture (after the cast).

Thanks though. I now have two trackball camera controls and two OpenGL
windows. I can attached both camera controls to one view and it will move
both views in unison thru events. Cool. I am beginning to like this .NET and
C# stuff. My Fortran skills are gradually waning though :).

Roger
 

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

Top