Fire the MouseUp event even when outside control in WPF - wpf

I want to be able in my WPF application to detect the MouseUp event from anywhere. That is to say, if the user clicks in the control and holds his click, then releases it outside the Control, I want my MouseUp event to fire.
I have done the MouseDown event, it works, but the MouseUp event isn't fired if released outside the Window.

Add the CaptureMouse method in your MouseButtonDown handler
You can look up here on what it actually does.

Another way is using the windows API's for mouse_event described here:
Simulating Mouse Clicks
I would use the CaptureMouse listed above first but if that doesn't work for you this one should.

Related

Is it possible that Drag and Drop events in WPF are not firing reliable?

I wonder if D&D events not always firing (DragLeave/DragEnter).
I implemented a D&D feature in my WPF GUI. While dragging some element around I 'dragleave' a GUI element. Normally it fires an appropriate 'DragLeave' event, but not always. I fear, that for some speed reasons sometimes these events get not fired reliable. If that's the case, how can I overcome these issue?
Short Version:
I wrote some sample app to check it myself - and yes - the DragLeave event is not fired reliable.
Long Version:
What I did:
I placed a Label as a draggable object inside a StackPanel. Then I implemented the code for the MouseMove and DragLeave events. An additional TextBlock - added to the StackPanel as well - served as event output. If the DragLeave handler gets called, I changed the Text Property of the TextBlock to 'DragLeave occured'. Afterwards I tested it dragging the Label towards the StackPanel.
What I recognized:
If I was fast enough or pressed the mouse button close to the border of the Label, the DragLeave event was not fired. If I did it slow, everything worked fine as expected.
What I conclude:
I guess, if the mouse leaves the Label before the 'DoDragDrop' thread is initiated, the event is not fired, since the mouse is already outside the Label. So there is a small glitch between starting the D&D and the firing of the first event.
One can see the same behavior in the DragOver/MouseMove handler. The faster you move the mouse, the fewer points (e.GetPosition(...)) you can capture in the event handler.
Problem is, that for the DragLeave that missing event can be critical, since it is only fired once. However, the DragOver event is fired very often during a dragging and a missing event can be balanced by the next DragOver event directly afterwards.

wpf touchdown vs mousedown

In my application user can use touch and also a mouse.
I am listening for mousedown event:
obj->ImageContainer->MouseDown += ...
But there is a different behaviour when I click either I tap.
In the click handler there is a complex function.
Click works good, the touch tap stucks the application.
I can't understand why.
If I also listen for touchdown event - both of them called.
Anybody have an idea what may be a difference between both?
Thank you

WPF TouchUp Event Strange behaivor

My WPF app functions perfectly, but only when using a mouse. Troubles start when using it on a device with a touch screen..
I have a grid that handles MouseLeftButtonUp and TouchUp events.
Now, I press on the grid, it handles related events, then I press on some other control, that other control catches TouchUp event as expected, then TouchUp event is transformed into MouseLeftButtonUp event, which is also something to expect.
However, the newly fired MouseLeftButtonUp event is fired NOT for the control that I pressed on, but for the above mentioned grid! Why does it behave this way?
Thank you in advance...
This is normal behaviour for all RoutedEvents. From the UIElement.MouseLeftButtonUp event page on MSDN:
Although this routed event seems to follow a bubbling route through an element tree, it actually is a direct routed event that is raised and reraised along the element tree by each UIElement.
MSDN provides far more answers and far quicker than Stack Overflow.

Winforms : how to bind an event handler accepting MouseButtonEventArgs?

My form contains a UserControl, and for now on this I have a MouseDown event handler, which works with a MouseEventArgs. However, I'd like to get an event handler with a MouseButtonEventArgs, so that I can access ClicksCount property to distinguish between simple and double clicking. For now I've some code found on StackOverflow that ways for double click time to be elapsed to know how many times were clicked but this causes redraw being delayed, so it's not user-friendly. Redraw is speedy in itself.
The UserControl displays a background image and small images drawn onto, from user clicks.
So how can I bind an event to an event handler that accepts a MouseButtonEventArgs event? MouseDown, both on UserControl or parent Form, only likes a MouseEventArgs.
I use pure Winforms, not WPF for this project.
Thank you for helping me finding.
So how can I bind an event to an event handler that accepts a MouseButtonEventArgs event? MouseDown, both on UserControl or parent Form, only likes a MouseEventArgs.
You cannot. MouseButtonEventArgs is for WPF. WinForms uses MouseEventArgs.
If you want to catch double clicks, you should handle the DoubleClick event.
For now I've some code found on StackOverflow that ways for double click time to be elapsed to know how many times were clicked but this causes redraw being delayed
This is the only way to know that a click event was a double-click. The only way to tell the difference is to wait the system-specified double-click interval. If a second click happens within that time, it should be interpreted as a double-click. If there is no second click, or it happened outside of that time span, the original click should be interpreted as a single click.
If you don't want the operation to be delayed, you need to react to a single click.

How to get notified when a window get focus in WPF?

I want to get notified when I click a window in WPF (I use the GotFocus event), but it only triggers when I click on a Combobox in the window. What I want is to get notified when the the window or any of the controls in the window is clicked. Any ideas of how to do this?
The GotFocus event doesn't get fired when the Window gets focus, it's intended to be used with controls only. The Activated event serves this particular purpose.
Use Activated event instead of GotFocus.
You could try the IsKeyboardFocusWithinChanged event. It should trigger when the keyboard focus is taken by an element of the window, or when it is taken by another window

Resources