Sign in to follow this  

C# OnMouseMove

This topic is 4339 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
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
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

This topic is 4339 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this