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.
Related
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(?)
I have a UWP app in which I have several buttons. Once the app starts to run, I set the focus in my code to the first button using a code like this:
firstButton.Focus(FocusState.Programmatic);
After this point, what I am interested in is that once the user use the mouse wheel, the UWP app automatically scroll to second, third, fourth, ... button(exactly like when we use tab key on keyboard to move between buttons).
However, when I use mouse wheel, nothing happens in the app.
I should also say that in firstbutton xaml, I use pointerwheelchanged event listener to change the focus to second button. However, this event handler does not work with mouse wheel UNTIL I MOVE THE MOUSE CURSOR INSIDE THE AREA OF FIRST BUTTON. What I am interested in is that this scrolling using mouse wheel becomes automatic exactly like the tab key of keyboard.
Any suggestions?
Place the event on the container control (like the Grid). If it's a control that already processes the event, use the AddHandler method with handledEventsToo set to true.
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?
In Excel, middle mouse button click work as a toggle. That is, if you press it once, it will allow you to scroll in your chosen direction till the time you click it again.
However, in Infragistics Ultragrid, this scrolling functionality is available only while middle mouse button remains pressed. How can I make Infragistics Ultragrid middle mouse button click work as in excel?
Otherwise also, what is the way to do it in winforms?
It's not as complicated as you might think. Clicking either the mouse wheel or middle button (depending on the type of Mouse the user has) fires off a MouseWheel event which must be handled and processed like any other event.
You'll need to configure a small "scrolling state machine" for you application. By this, I mean that the user is either scrolling in, say, a NormalMode, where using scroll bars or flicking up/down on the mouse wheel produces the same effect (scrolling up/down). Or, the application is in a HoverScrollingMode which occurs whenever the user has clicked the middle button (or mouse wheel) and is moving the mouse north or south of the click point.
I can't give you a programming example without seeing how your application currently handles other types of mouse events, but your overall strategy is to handle these MouseWheel events, use them to switch your application state (into, say, HoverScrollingMode) and then programmatically move the viewport up/down depending on the current position of their mouse.
Hope this helps, and good luck!
I have an app that incorporates a drop target to unarchive (file attribute) files. I would like to change the mouse cursor in the DragEnter event of the form to a custom cursor (.cur) that I have as an embedded resource.
The drop target is a transparent form with an image of a target. (The entire form is the drop target)
I know I can use the GiveFeedBack event when I have control of the drag source. However this is not the case as the source is Windows explorer. (and maybe I'm wrong and I can still use GiveFeedBack in this scenario, but I could not figure out how to trigger the event.)
To eliminate the possibility that my custom cursor was not valid or corrupt, I successfully set the custom cursor in the MouseOver event of the drop target form.
Functionally everything is working, I am now just wanting to "pretty things up" a little bit.
Thanks in advance for any insight.
You seem to know already that custom cursors are set in GiveFeedBack and how to do that. The problem then is that you are not getting any GiveFeedBack events. I don't know on what control you have a drop spot, but try attaching a listener to the underlying Form's GiveFeedBack event, and see if you get any feedback there.
You can also try subclassing the control you are using (Panel, PictureBox or whatever) and listening to its GiveFeedBack event.