In WindowsForms
I just added event handlers as follows:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Debug.WriteLine($"=> Form1_MouseDown, Clicks: {e.Clicks}, Location: {e.Location}");
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
Debug.WriteLine($"=> Form1_MouseUp, Clicks: {e.Clicks}, Location: {e.Location}");
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Debug.WriteLine($"=> Form1_MouseMove, Clicks: {e.Clicks}, Location: {e.Location}");
}
And the output is:
=> Form1_MouseMove, Clicks: 0, Location: {X=17,Y=21}
=> Form1_MouseDown, Clicks: 1, Location: {X=17,Y=21}
=> Form1_MouseUp, Clicks: 1, Location: {X=17,Y=21}
=> Form1_MouseMove, Clicks: 0, Location: {X=17,Y=21}
You can see that all events occurs in the same location, So my question is why is there a MouseMove
event after MouseUp
event?
Also I tried similar code in WPF and MouseMove
event NOT occurred.
And I tried similar code in C++ and MouseMove
event NOT occurred:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
...
case WM_MOUSEMOVE:
OutputDebugString(L"WM_MOUSEMOVE\n");
break;
case WM_LBUTTONDOWN:
OutputDebugString(L"WM_LBUTTONDOWN\n");
break;
case WM_LBUTTONUP:
OutputDebugString(L"WM_LBUTTONUP\n");
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
click. Triggers after mousedown and then mouseup over the same element if the left mouse button was used. dblclick. Triggers after two clicks on the same element within a short timeframe.
MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.
MouseMove is a simple event that is executed when a pointer is moving over or around an element. Mousemove is a javascript event that inside a web page. The mousemove event can also be understood as a part of an event handler, whereupon some action or movement by a mouse pointer, an intended script is executed.
The mousemove event is fired at an element when a pointing device (usually a mouse) is moved while the cursor's hotspot is inside it.
MouseDown, MouseUp events. Occur when the user clicks a mouse button. MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.
We’ve already seen some of these events: Mouse button is clicked/released over an element. Mouse pointer comes over/out from an element. Every mouse move over an element triggers that event. Triggers after mousedown and then mouseup over the same element if the left mouse button was used.
The mousemove event is fired at an element when a pointing device (usually a mouse) is moved while the cursor's hotspot is inside it. The following example uses the mousedown, mousemove, and mouseup events to allow the user to draw on an HTML5 canvas. Its functionality is simple: the thickness of the line is set to 1, and the color is always black.
Every mouse move over an element triggers that event. Triggers after mousedown and then mouseup over the same element if the left mouse button was used. Triggers after two clicks on the same element within a short timeframe.
If your mouse had previously been focused on a separate window, then clicking on a new window and shifting the focus of the mouse will generate a mouse move event (even if the mouse didn't move immediately before or after you clicked your mouse).
Here is a link to a similar StackOverflow response "Ghost" MouseMove Event
This is because the mouse capture by the MouseDown
is released on MouseUp
. And this extra MouseMove
may be to ensure the cursor position. As a workaround you can do this
Point LastLocation = Point.Empty;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Debug.WriteLine("=> Form1_MouseDown, Clicks: " + e.Location + ", Location: " + e.Location + "");
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
Debug.WriteLine("=> Form1_MouseUp, Clicks: " + e.Location + ", Location: " + e.Location + "");
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (LastLocation != e.Location)
{
LastLocation = e.Location;
Debug.WriteLine("=> Form1_MouseMove, Clicks: " + e.Location + ", Location: " + e.Location + "");
}
}
This is the intended behavior and will also be trigger whenever app is being switched (Eg: Alt+Tab).
You should go with workaround as suggested by @VishnuBabu's workaround. And to ignore initial mousemove trigger, you can get the current position of cursor once window is loaded instead of setting the LastLocation to Empty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With