C# OnMouseMove

Started by
9 comments, last by smittix 18 years, 2 months ago
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
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"
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]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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
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]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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
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
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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.
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);	}
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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

This topic is closed to new replies.

Advertisement