I assume that there is another control on top of your form in the area where you want to trigger the event. If so, control captures the MouseMove .
For example, here I added a green 200x200 panel at position 0, 0 in the upper left corner. If the mouse moves around the panel, the MouseMove form MouseMove will no longer capture the position of the mouse cursor. In my mouse_move form event, I set the form text to display the coordinates of the mouse. Please note that the coordinates in the window text are still 200, 200 when the mouse was actually at 0, 0 (my cursor cannot see due to the need to click on SnippingTool.exe to get a screenshot).

To fix this, use the same code that you posted on the MouseMove event form in the MouseMove event panel (or whatever control you use). This results in the correct coordinates in the form text.

And here is the code (this, obviously, can be reorganized into one method):
public partial class Form1 : Form { [DllImport("user32.dll")] static extern bool SetCursorPos(int X, int Y); public Form1() { InitializeComponent(); } private void Form1_MouseMove(object sender, MouseEventArgs e) { this.Text = string.Format("X: {0}, Y: {1}", eX, eY); if (eX >= 0 && eX <= 200) { if (eY >= 0 && eY <= 200) { SetCursorPos(500, 500); } } } private void panel1_MouseMove(object sender, MouseEventArgs e) { this.Text = string.Format("X: {0}, Y: {1}", eX, eY); if (eX >= 0 && eX <= 200) { if (eY >= 0 && eY <= 200) { SetCursorPos(500, 500); } } } }
Inisheer
source share