Jump to content
  • Advertisement
Sign in to follow this  
bengaltgrs

C# OnMouseMove

This topic is 4558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been trying to use the OnMouseMove function in C#, but it rarely gets called. I need for it to get called every frame, but it will only get called if the cursor is in certain places in the window which seem to be random. Here is the code to get the curson position.
protected override void OnMouseMove(MouseEventArgs e)
{
	labelMousePosition.Text = "Mouse Position: X: " +  e.X + " Y: " + e.Y;
}

-Chris

Share this post


Link to post
Share on other sites
Advertisement
an't events usually bound by going control.onmousemove += new Delegate( function )?

you won't get onMouseMove firing every frame, you'll simply need to call your function from inside your game loop
and just check delta mouse position != 0 to see if the mouse has "moved"

Share this post


Link to post
Share on other sites
EDIT: This and the next post by me is not the prefered way of doing it. My fourth reply contains code for the prefered way.



As rfterdarc said, you are not supposed to override the OnMouseMove, but instead invoke a method on the MouseMove-event. Like this:


public Form1()
{
// ...


// This line tells the form that whenever the mouse moves, we want
// Form1_MouseMove to be called.
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}

// This method gets called when the mouse moves.
void Form1_MouseMove(object sender, MouseEventArgs e)
{
labelMousePosition.Text = "Mouse Position: X: " + e.X + " Y: " + e.Y;
}








[Edited by - Enselic on January 28, 2006 12:51:54 PM]

Share this post


Link to post
Share on other sites
I attempted to use the method Enselic showed, but I get the same results. The mouse position only updates sometimes, not very often. If it makes a difference I have many different UI objects including a panel which is being used as a Direct3D window.

-Chris

Share this post


Link to post
Share on other sites
Since the MouseEvent is fired only on the control receiving it, you need to let the parents know. You could for instance achive this by calling the OnMouseMove() method of the Form, with the X and Y transformed.

Something like this:

public Form1()
{
// ...


this.MouseMove += new MouseEventHandler(Form1_MouseMove);
this.panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
}

void panel1_MouseMove(object sender, MouseEventArgs e)
{
// Let the form know a MouseMove event occured,
// and give it the correct X and Y values
this.OnMouseMove(new MouseEventArgs(e.Button, e.Clicks, e.X + panel1.Left, e.Y + panel1.Top, e.Delta));
}

void Form1_MouseMove(object sender, MouseEventArgs e)
{
labelMousePosition.Text = "Mouse Position: X: " + e.X + " Y: " + e.Y;
}





[Edited by - Enselic on January 28, 2006 5:02:19 AM]

Share this post


Link to post
Share on other sites
I tried adding this but it does the same thing the other attempts did, except now it will crash after about 5 seconds. Thank you for the help, by the way.

-Chris

Share this post


Link to post
Share on other sites
Did you remove the overide void OnMouseMove()?

What OnMouseMove() does is to raise the MouseMove event, so if you still have it overidden without calling base.OnMouseMove(), the event will not be raised when it should.

Refer to MSDN - Raising an Event

Share this post


Link to post
Share on other sites
Microsoft actually recommend using the OnMouseMove method opposed to attaching an delegate to the MouseMove event. (The same with most Form events iirc)

Control.OnMouseMove

Quote:

The OnMouseMove method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors When overriding OnMouseMove in a derived class, be sure to call the base class's OnMouseMove method so that registered delegates receive the event.

Share this post


Link to post
Share on other sites
Oh I see. I should have read that page a bit more carefully [smile]

Bengaltgrs, forget about my other code. This is the prefered way:

public Form1()
{
// ...

panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
}

void panel1_MouseMove(object sender, MouseEventArgs e)
{
OnMouseMove(new MouseEventArgs(e.Button, e.Clicks, e.X + panel1.Left, e.Y + panel1.Top, e.Delta));
}

protected override void OnMouseMove(MouseEventArgs e)
{
labelMousePosition.Text = "Mouse Position: X: " + e.X + " Y: " + e.Y;
base.OnMouseMove(e);
}

Share this post


Link to post
Share on other sites
On the contary, the prefferred way is simply:


protected override void OnMouseMove(MouseEventArgs e)
{
labelMousePosition.Text = "Mouse Position: X: " + e.X + " Y: " + e.Y;
base.OnMouseMove(e);
}




:)

The OnMouseMove is already registered to the MouseMove in the Control constuctor. To the OP, a quick test reveals the MouseMove is only raised when another mouse event happens, for example, a click or the mouse entering the control.

edit: ugh, nevermind i misread :) been a long day

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!