Mouse events for captured element stop firing while dragging - c#

I have some code that draws some points on the screen and then allows them to be dragged while holding the left mouse button. This kinda works except constantly the mouse events stop firing and the point being dragged stops moving.
Because all of the events stop being caught(for some unknown reason), that means the user can release the left mouse button without anything happening.
The weird thing is that the user can then reposition the mouse over the point and it will start dragging again without holding the left mouse button down. It makes for a very poor user experience.
What is going on?
pinPoint.MouseLeftButtonDown += Point_MouseLeftButtonDown;
pinPoint.MouseLeftButtonUp += Point_MouseLeftButtonUp;
pinPoint.MouseMove += Point_MouseMove;
.
.
.
void Point_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
((UIElement)sender).CaptureMouse();
if (_isDragging == false)
{
_isDragging = true;
}
}
void Point_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_isDragging = false;
((UIElement)sender).ReleaseMouseCapture();
}
void Point_MouseMove(object sender, MouseEventArgs e)
{
//where the point gets moved and some other logic
//possibly this logic takes too long?
}

I've found a solution that works. I've still got no idea why it is continually losing the MouseCapture though.
pinPoint.LostMouseCapture += Point_LostMouseCapture;
.
.
.
void Point_LostMouseCapture(object sender, MouseEventArgs e)
{
//if we lost the capture but we are still dragging then just recapture it
if (_isDragging)
{
((UIElement)sender).CaptureMouse();
}
}

Related

MouseMove event prevents the MouseRightButtonUp event in WPF

I have drawn some lines using DrawingVisual. Now I'm trying to implement a simple hit testing.
When the mouse pointer moves over a line I'm redrawing it with twice its thickness to give the user a sense of highlighting (Magenta line in the image below)
This works very well until I also implement a MouseRightButtonUp event. I want to show a messagebox when the user right clicks the line. But somehow while the mouse is over the line and the line is highlighted, the MouseRightButtonUp event does not raise at all. What could be the problem?
MouseMove += OnMouseMove;
MouseRightButtonUp += OnMouseRightButtonUp;
private void OnMouseMove(object sender, MouseEventArgs e)
{
var p = e.GetPosition(Window);
// Check if the mouse hit any lines and redraw
ReDraw();
}
void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
// Check if any line is hit and show the message box
if (lineHit)
{
MessageBox.Show("You hit the line!");
}
}

Mouse events not fired

I'm making a C# WinForms application. The MouseMove and MouseClick events of the form aren't getting fired for some reason. (I'm probably going to feel like an idiot when I find out why.)
It is a transparent form (TransparencyKey is set to the background colour) with a semi-transparent animated gif in a Picture Box. I am making a screensaver.
Any suggestions?
EDIT:
MainScreensaver.cs
Random randGen = new Random();
public MainScreensaver(Rectangle bounds)
{
InitializeComponent();
this.Bounds = Bounds;
}
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle screen = Screen.PrimaryScreen.Bounds;
Point position = new Point(randGen.Next(0,screen.Width-this.Width)+screen.Left,randGen.Next(0,screen.Height-this.Height)+screen.Top);
this.Location = position;
}
private void MainScreensaver_Load(object sender, EventArgs e)
{
Cursor.Hide();
TopMost = true;
}
private Point mouseLocation;
private void MainScreensaver_MouseMove(object sender, MouseEventArgs e)
{
if (!mouseLocation.IsEmpty)
{
// Terminate if mouse is moved a significant distance
if (Math.Abs(mouseLocation.X - e.X) > 5 ||
Math.Abs(mouseLocation.Y - e.Y) > 5)
Application.Exit();
}
// Update current mouse location
mouseLocation = e.Location;
}
private void MainScreensaver_KeyPress(object sender, KeyPressEventArgs e)
{
Application.Exit();
}
private void MainScreensaver_Deactive(object sender, EventArgs e)
{
Application.Exit();
}
private void MainScreensaver_MouseClick(object sender, MouseEventArgs e)
{
Application.Exit();
}
Excerpt from MainScreensaver.Designer.cs InitialiseComponent()
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseClick);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseMove);
This isn't an answer to you question, but I'm leaving this answer in case anyone else stumbles upon this question while trying to debug this same issue (which is how I got here)
In my case, I had a class that was derived from Form.
I was also using TransparencyKey.
Some things I noticed
The events will not fire on the transparent parts of the form.
The events will not fire if the mouse cursor is over another control on the form.
The events will not fire if you override WndProc and set the result of a WM_NCHITTEST message. Windows doesn't even send out the corresponding mouse messages that would cause the .NET events.
My Solution
In my constructor, I had forgotten to call InitializeComponent().
Which was where the event handlers were being bound to my controls.
Events were not firing because the handlers were not being bound.
Are you sure that your form has focus? If your form does not have focus, the mouse events will not be fired.

How to prevent other mouse buttons from interrupting MouseMove?

If you place a panel in a new C# project and capture it's MouseMove event like this:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}
It tells you the client coordinates of the mouse while the left mouse button is held down, even if the cursor goes outside of the panel.
However, if you are holding down left mouse button in the container and then, while holding down left mouse button, click any other mouse button on your mouse, it no longer calls MouseMove while outside the bounds of the container.
Is there any way to change this? Thanks for reading.
1: If you are trying to get it to work only when the left button is down, try the following:
bool mouseDown = false;
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (!mouseDown)
return;
Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
mouseDown = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
mouseDown = false;
}
2: Otherwise, if you want it to work when any mouse button is down, try the following:
int mouseDown = 0;
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown == 0)
return;
Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown++;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown--;
}
In addition to Justin's solution will say that:
I think it's because, if during drag you go out of the panel right click out of the panel, forces panel to lose the focus, so control no more becomes an active one.
In case of when you're dragging mouse inside panel and click with right click inside panel, happens something like this.
I just captured with Spy++ windows explorer and did a test, so moved the mouse with LButton down and at some point without releasing it made a right click. And here is a result:
With arrows I sign the row where I clicked with right button, where WM_CAPTURECHANGED
message sent. This message according to documentation is:
Sent to the window that is losing the mouse capture.
Look on next line with arrow. The handle of the next window is 0, so there is no any window. So this means, like a simple command: You lost a capture on mouse.
Hope this helps.

Listbox events firing strangely

I'm confused. I am basically trying to tell when the user has clicked something in the listbox, held the button, and left the listbox. Here is a somewhat dumbed down version of what I am doing:
private bool itemHeld;
private void listOriginal_MouseDown(object sender, MouseEventArgs e)
{
itemHeld = true;
}
private void listOriginal_MouseUp(object sender, MouseEventArgs e)
{
itemHeld = false;
}
private void listOriginal_MouseLeave(object sender, EventArgs e)
{
if (itemHeld)
MessageBox.Show("OHH YEAH");
}
To me that seems like it should turn itemHeld true when you press the mousebutton, turn it false only if you lift it, and display ohh yeah if the value is true. If I break on the mouse down event to check the value, it is true and if I continue from there it displays the message. If I do not break, it does nothing. Is there something else at work here?
Edit:
Brief description: It would be difficult to explain what I am really trying to accomplish but imagine something almost like dragging a file off of a window. I need to simply be able to recognize when the user clicks inside of the listbox and then drags out of the listbox if that makes sense
You can not debug windows events by break point because when the Visual Studio get active to debug, the mouse leave event will be fired for the hovered control.
You can use Debug.WriteLine which writes information about the debug to the trace listeners.
private void button1_MouseLeave(object sender, EventArgs e)
{
Debug.WriteLine("Mouse leave");
}
private void button1_MouseEnter(object sender, EventArgs e)
{
Debug.WriteLine("Mouse enter");
}
private void button1_MouseHover(object sender, EventArgs e)
{
Debug.WriteLine("Mouse hover");
}
what about this?
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X > listBox1.Width - 1 || e.Y > listBox1.Height - 1 || e.X < 0 || e.Y < 0)
{
Console.WriteLine("drag out");
}
else
Console.WriteLine("mouse move {0}/{1}", e.X, e.Y);
}
it uses the fact that the Control is not left before the mousebutton is released ... but be aware that the drag out part will occur more than once so you probably will want to have a flag set the first time ... and have that flag cleared on mouse up or leave
For every mouse click, your MouseDown event will fire AND your MouseUp event will fire, so the sequence of operations is equivalent to
itemHeld = true;
itemHeld = false;
if(itemHeld)
MessageBox.Show("yay");
If you press the mouse button on the listbox and move the cursor out without releasing the button, switching focus to another window (e.g. Visual Studio) is what triggers the MouseLeave event to fire. This is why you're seeing the message box pop up when you're debugging.
I'm not sure what you're trying to accomplish, so I can't recommend another solution.

Creating A Continuous Action During A Windows Form Mouse Event

When I put a button on a form in C#, Visual Studio 2005, and have an action triggered by a button event, such as MouseHover or MouseDown, then the event triggers a single call to the function which defines the action despite the fact that I may continue to hover or keep the left button down. In this case I am trying to move a graphical object by rotating or translating it. I don't want to continue to click the mouse in order to get a repeated call to the transforming function, just keep the mouse hovering or hold the button down. What maintains the action until I cease my own action?
Set a flag on MouseEnter and keep doing the action while the flag remains true. Set the flag to false on MouseLeave.
In your case you need to use a combination of the events MouseDown, MouseMove and MouseUp.
Here a small simplified example to start:
private void OnMouseDown(object sender, EventArgs e)
{
//hit test to check if the mouse pointer is on a graphical object
_myHitObject = the_selected_object
}
private void OnMouseMove(object sender, EventArgs e)
{
if(_myHitObject != null)
//do your action relative to the mouse movements.
}
private void OnMouseUp(object sender, EventArgs e)
{
_myHitObject = null;
}
The solution is to use DoEvents() which allows for the MouseLeave event to be noted and the class variable "more" to be changed:
private void MouseEnter_ZoomIn(object sender, EventArgs e)
{
more = true;
while (more == true)
{
c1Chart3D1.ChartArea.View.ViewportScale *= ZoomMultiple;
Application.DoEvents();
}
} // MOUSEENTER_ZOOMIN()
//-------------------------------------
private void MouseLeave_Stop(object sender, EventArgs e)
{
more = false;
}

Categories