Which cursor to use?


Someone on the forums was asking about how to set the mouse cursor programmatically, it reminded me about the different steps WPF uses to decide which cursor to display:


1)     When the mouse moves, we query what the cursor should be and set it to that.



a.      We raise the QueryCursor event on the element the mouse is over, which starts out assuming the cursor should be an arrow.  Elements fill in what they want the cursor to be.  This event bubbles up the tree.



i.     Anyone could respond to this event and fill in the cursor they want.


ii.     FrameworkElement fills in the cursor based on what the Cursor property is if the event is not handled or the ForceCursor property is true.


b.      We call SetCursor with the results of the QueryCursor event.


2)     You can, at any time, call SetCursor.  This sets the cursor immediately.



a.      The next mouse move could change the cursor


b.      Someone else calling SetCursor could change the cursor


c.      If someone has set an OverrideCursor, then the cursor is always set to this.


UpdateCursor() can be called to force Avalon to reevaluate what cursor to use, which can be useful if elements are moving underneath the mouse (e.g., animation).


SetCursor can be used to specify the cursor without waiting for a mouse move or UpdateCursor.  SetCursor’s effects are temporary, lasting only until the next time Avalon reconsiders which cursor to use.


 


Comments (3)

  1. Wahaha says:

    No middle finger cursor on Avalon?

  2. Eric Wilson says:

    From your description, does it sound like SetCursor should be removed to anyone else?  After all, whats the point in setting the cursor if it will be immediately discarded when the mouse move.  That’s just not intuitive from a developer’s standpoint.  When I call SetCursor, I mean "SetCursor until I tell you otherwise".  I NEVER mean, "SetCursor until the user interacts with my application again."

    Given the above, I also question how to code up the Windows standard op of "make the mouse an hourglass until we are through processing".  Is there a way?

  3. SetCursor was based on Win32’s SetCursor, which has most of the same issues.  It’s occasionally useful, mostly if you need to change the cursor during a time you aren’t going to be pumping messages, although mostly we stuck it in there to make sure we weren’t missing something that Win32 allowed.

    Since not pumping messages is frowned upon, the preferred way of doing an hour glass is handling the QueryCursor event.