Switch On The Code RSS Button - Click to Subscribe
Apr
23

Flex Custom Cursor Tutorial

flex icon

On our site here we are kinda obsessed with showing how to use your own cursors in everything - and so now its time for a tutorial on how to do the basic cursor manipulation in flex. In this tutorial you will learn how to replace the cursor with your own custom image. There are some more complex techniques that can be used for cursor management in Flex but I am going to hold these off for a later tutorial, as most people probably won't need them.



Below you can check out the demonstration of the application we are going to build today. The application is very simple - it simply has two buttons. The first button changes the cursor to my super sweet custom cursor and the second button changes the cursor back to normal. Ok, now take a few seconds and be amazed by my cursor creation genus. The source code for the tutorial.


To start off we are going to build the simple interface that is in the application. This will not include the little button for the source. Ok so there is a panel and two buttons, very simple. Check out the code below.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  width="350" height="200" layout="absolute">

  <mx:Panel title="Having Fun with Cursors"
    x="0" y="0" width="100%" height="100%" layout="absolute">

    <mx:Button label="Show Cursor"
      x="10" y="59" width="115"/>

    <mx:Button label="Remove Cursor"
      x="205" y="59" width="115"/>

  </mx:Panel>

Next up is the the code to show the the cursor and remove them. The first thing is setting up the click event listener for both buttons. I have named these functions showCursor and removeCursor. The updated code is below.

<mx:Button label="Show Cursor" click="showCursor()"
  x="10" y="59" width="115"/>

<mx:Button label="Remove Cursor" click="removeCursor()"
  x="205" y="59" width="115"/>

Now we get to the actionscript code. We add a script tag and the functions for the cursors:

<mx:Script>
<![CDATA[
  import mx.managers.CursorManagerPriority;
  import mx.managers.CursorManager;
 
  [Embed("/cursors/cc.png")]
  private var customCursor:Class;
 
  private function showCursor():void
  {
    CursorManager.setCursor(
      customCursor,
      CursorManagerPriority.HIGH,
      3,
      2);
  }
 
  private function removeCursor():void
  {
    CursorManager.removeAllCursors();
  }
]]>
</mx:Script>

To get the cursor to show up we need to do two things. The first is to create a private variable that holds the cursor which is of type Class. It is here that we embed the image we want to use. This can be any of several image types including jpg, png, swf, and more. In the showCursor function we use the CursorManager to set the cursor, as seen above. The parameters we pass are first the cursor, next is the priority (this can always be high for simple cases), the last two are the offset for the hot spot of the pointer in x and y. So basically where the actual pixel is that should be used for the pointer. And that is it for showing a cursor.

Removing the cursor is just as easy. You simply need to call CursorManager.removeAllCursors() to remove any custom cursors. There are some more complex ways to remove individual cursors but you will have to wait for a later tutorial for this.

Well I know it was short but I hope that you learned something useful from it. And look for a more complex tutorial explaining some of the other intricacies of the CursorManager class. As always drop a comment if you have any issues with the code or have any questions at all.

References



Posted in Flex, All Tutorials by The Fattest |

7 Responses

  1. amar shukla Says:

    nice work :) ..thnx..its usefull for me like novice

  2. Ganesh Says:

    Good one. Very easy to understand. Many thanks.

  3. Katy Says:

    Is it possible to use this to remove the white circle with the red cross that appears when a user is dragging an item that is not over a drop target? If it is could you tell me where to place it as I’ve been playing around with it but can’t seem to remove it. Thanks

  4. Mariosh Says:

    Great tutorial. Thanks

  5. noel Says:

    an issue with this and the CursorManager in general is that when you press the button the default cursor appears on the screen with the custom cursor and only disappears after a mouse move event…
    can’t seem to find a workaround for it…

  6. The Fattest Says:

    This example doesn’t work like that noel, the cursor will appear as soon as the click event is completed and removed the same. No movement of the mouse is needed.

  7. GT Says:

    Nice job. The thing I’m struggling with at the moment is getting the cursor in a chart to display as a crosshair - just a vertical line 100% height, and a horizontal line 100% width, intersecting at the cursor coordinates. (It’s an absolute doddle in Flash, but I can’t get it happening in Flex).

    I’ve got a sense that your tutorial just gave me a pointer (ha ha).

    Cheers

    GT
    France (but I’m an Aussie… hence the bad English)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Powered by WP Hashcash