Check mouse position on WPF Control Drag and Drop after a delay - c#

I am trying to implement drag and drop behaviour within a WPF Treeview. As part of this I need to expand the currently hovered over node if the mouse has been over the node for a set period of time once the DragOver event has been triggered.
I have been able to successful delay the expansion using a DelayedAction on the node that triggered, but I have been unable to successful check the current position of the mouse once the delayed action has triggered. Currently, even if the mouse has moved off the node and the click released, after the delay, the node will still expand.
I need to be able to check the current position of the mouse after the delay, and only expand the node if the mouse is still over the node.

I was able to solve this with an answer from a different question here. The thread I used is available here:
How do I get the current mouse screen coordinates in WPF?
I used the Win32 approach to get the correct mouse position after the delay and use this to get the current TreeViewItem under the mouse by using a hit test.
If the current TreeViewItem is the same as the TreeViewItem that triggered the DragOver event then I expand the node, or else do nothing.

Related

Change pointer cursor when hovering over map elements

My UWP application contains a map with several POI. I am trying to change the mouse cursor from an arrow to a hand when hovering over specific poi to indicate its clickable.
This would change the cursor as soon as it enters the map still, as a simple test, I added a PointerEntered event for the mapcontrol and within it I have the following to change the cursor:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 0);
It appears though the cursor does change however immediately gets overridden back to the pointer cursor.
Edit: Just realised When a poi is clicked (i.e. is selected) the cursor changes to a hand even when not over the map control until the poi is unselected. No good as I would like the cursor to change dynamically when hovering over a poi and revert back to cursor when moved away.
Change pointer cursor when hovering over map elements
I'm afraid you can't edit the default cursor for map element, Because it has handled internally, it will not fired, even you has listen PointerEntered event, it consumed by the control and not passed up the control chain. If you do want this feature, the better way is post this feature with windows feed backhub app.
I don't know if it works just like WinForms, I had to do something like this to click on labels (couldn't use link-labels), what I used was in the Mouse_Move event of the label and it was basically
if (Cursor.Current == Cursors.Default)
{
Cursor.Current = Cursors.Hand;
}
and similar changes and behaviors due to the various conditions. This however got me a small issue: this statement changes the mouse graphic anytime you move on the control, but personally on Windows settings I use the trail graphic function for the mouse (leaving a trail of pointers whenever I move the mouse on the screen), what I suggested you disables this function, or better, it conceals it, since it "recreates" the mouse graphic for every move you do onto the control, and thus it "undoes" the graphic for the mouse and recreates it as a Hand (in my instance). If it doesn't concern you though, it works just fine.
Just I repeat myself: I use this on WinForms, but since it's C# I suppose it just will work(?)

WPF: Keep custom cursor while mouse is down

I have created a custom cursor and applied it to my control via the Cursor property and it displays correctly while the mouse is over the control, however once I click, the cursor changes back to default. What I would like is the custom cursor to be persisted while the mouse is down (and potentially being moved) and return to normal whenever the mouse comes up (over any possible control). I realize I could do this by setting the Mouse.OverrideCursor on mouse down, but I'm not sure how I would set the override cursor back when the mouse comes up because it could be released anywhere on the screen. The behavior I want is similar to clicking in a cell and dragging in Excel. Does anyone have any suggestions for how I can implement this behavior?
Edit:
I tried following the advice in this answer: https://stackoverflow.com/a/2986757/3818295 however my PreviewMouseLeftButtonUp handler never gets invoked.
If you want to change the cursor globally for your entire application, then use the Mouse.SetCursor method. The cursor will remain changed until you explicitly change it again. To change it back, call Mouse.SetCursor(Cursors.Arrow); don't pass in null or Cursors.None, as those will give you a special 'invisible' cursor.
If you want to change the cursor only while the mouse is down, then the control that initiated the 'drag' operation will need to capture the mouse so that it continues to receive events even after the cursor leaves the original control. This ensures the source control eventually receives the mouse up event (unless the capture is lost). To capture the mouse, call either c.CaptureMouse() or Mouse.Capture(c), where c is your control. You will need to release the capture when the drag operation terminates, e.g., via c.ReleaseCapture().
It would be a good idea to temporarily subscribe to the drag source's LostMouseCapture event for the duration of the drag, and cancel the drag operation if capture is lost. If capture is lost, you aren't guaranteed to receive the mouse up event, so at that point you should just give up. Remember to unsubscribe from LostMouseCapture after the drag terminates.

How to catch MouseOver while the mouse is captured by another control

I have some user controls on a wpf form. I have set a trigger for them on IsMouseOverProperty so that every time the mouse is over any of them, the Background color get changed. I also have a trigger of IsPressedProperty so that every time that the user press the mouse button, the Background color get changed to another color.
Furthermore on the OnMouseLeftButtonDown event I set this. CaptureMouse() and in the OnPreviewMouseLeftButtonUp event I set this.ReleaseMouseCapture() so that when the user clicks anywhere else on the window, the the UserControl lost its mouse capture.
Every thing seems good so far unless when I press the mouse button on a user control and I move the mouse over the other user controls, their Background color never get changed because the mouse is captured by the control on which the mouse is captured.
Now I need to know is there any way to capture mouse on a user control and still be able to find if the mouse is over other controls?

Trigger MouseMove Event for Specific Number of Time

I have a custom CustomToolStripMenuItem which is derived from ToolStripMenuItem Class. In that custom class 'CustomToolStripMenuItem', I override OnPaint(), OnMouseMove() and OnMouseDown() Events.
Here, I want to add a rectangle to show on each sub menu to delete that menu item from customized tool strip menu items. When user move mouse to rectangle area, it will change back color which shows that user want to delete that item. I add these menus by reading an xml file.
The main problem here is that, when I move the mouse from one menu item to other quickly, previous item also shown as selected. How can I ensure that when mouse move to other menu item, previous selection should erase.
What I guess, I need to repeat that mouse move event for specific times (total items in xml file), but How can I do this with events of mouse ???
Any Help ?
You can also use MouseLeave event with current events, if fast mouse move bypasses your current MouseMove event!

Display tooltip when mouse movement stop

I have a grid-view with merged cells and rows which look like this.
I am using grid view mouse enter cell and mouse leave cell events to call the tool-tip show and hide methods.
Problem I am having is, since the cells are very closer tooltip get displayed when I moving across the grid. What I am trying to achieve is display the tooltip only when mouse is stop moving.
Is it possible to capture current state of mouse where I can capture mouse is moving or not.
I have looked at the Control.MouseMove Event, but can I find the current state of the mouse using that.
Update
I did try something like this
In MouseMove event of the gridview tooltip.Hide()
In
MouseHoverCell tooltip.Show() but had no luck with it
Thanks
What you need here is to leverage the OnMouseHover and OnMouseMove events.
The OnMouseHover event is fired when the mouse stops moving. So, in your case, you can fire this event when the mouse stops, pick up the containing cell and show your tooltip. When the OnMouseMove event fires teardown the tooltip and start again.
I hope this helps.

Categories