Creating custom animated wait cursors

Screenshot of standard Windows Mobile 6 wait cursor Screenshot of example animated wait cursor Screenshot of example animated wait cursor

Most .NET Compact Framework developers will be familiar with the Cursor.Current property and how to display the standard Wait Cursor, but did you know that you could easily display your own custom cursor? This blog entry discusses how you can replace the standard wait cursor with your own application specific cursor. This is an ideal way to brand kiosk style applications for example.

Creating a custom cursor

Recent versions of the Windows CE operating system (and hence Windows Mobile) support an OS API called LoadAnimatedCursor. This API enables you to specify a sequence of individual bitmap frames and will convert them into an animated cursor. For example an animated cursor of a rotating monkey could be made up of the following 4 bitmaps.

101 102 103 104

The more frames the cursor consists of the smoother the animation will be. Individual frames within the animation should be 48×48 pixel bitmap resources within a *.dll or *.exe file. The bitmap resources are identified by a numeric ID and must be in sequential order (such as the values 101, 102, 103 and 104 used in the example above).

The id of the first bitmap, the total number of frames and the period of time to delay between frames is then passed into the LoadAnimatedCursor API which will return a handle to the newly created cursor (an HCURSOR). Passing this handle to the SetCursor API will then make the cursor visible on the screen.

Screenshot showing bitmap resources within the CustomCursors dllUnfortunately the LoadAnimatedCursor function is not as easy to use from managed code as it should be. The API expects the bitmap images to be native bitmap resources meaning you can not store them within a *.resx resource file within your .NET Compact Framework application. The easiest way to store the bitmaps in the correct format is to create a native C++ DLL project. You can then remove all the C++ source files, leaving a sole Win32 *.rc resource file to which you can add the bitmaps to (as will be demonstrated later).

Sample Application

[Download animatedcursortest.zip - 37KB]

The sample application available for download consists of two projects. The first (called AnimatedCursors) demonstrates how to create a resource only DLL that contains the bitmap images required for the two custom cursors shown above.

The second project is a C# example demonstrating how to use Platform Invoke to access theLoadAnimatedCursor and SetCursor APIs to display the custom cursors. This second project loads the custom cursors from the AnimatedCursors.dll file built by the first project.

The C# sample wraps up the required Platform Invoke code within a class called AnimatedWaitCursor. This class implements the IDisposable interface so that the following syntax can be used to display a custom cursor. This code structure should be familiar to anyone who has used MFC’s CWaitCursor class.

// Use the animated cursor that has 4 frames starting with
// bitmap id 101, delaying 125 milliseconds between each frame.
string dll = @"\path\to\some.dll";
using (AnimatedWaitCursor cursor = new AnimatedWaitCursor(dll, 101, 4, 125))
{
  // do some long running task
}

7 Responses to “Creating custom animated wait cursors”

  1. tbmvp says:

    Thanks for article, this is useful for me.

    Regards.

  2. Stephen says:

    Nice blog in general. This post was especially useful.

    I love that you post examples as classes of reusable code.

    I will be watching for more cool posts!

    Thanks.

  3. Diego says:

    Very useful, thanks

  4. Name (required) says:

    Is this code in Public Domain ?
    http://creativecommons.org/licenses/publicdomain/

  5. Name (required) says:

    I’m sorry to post this question again, but I didn’t find any licence attached to your sample source code. Is this sample under public domain ? :)

  6. hibbert says:

    hi,
    i think this would be great if i could open the AnimatedCursors.sln… but i’ve got none of the required configurations… so how can i open this solution?

    thanks hibbert

  7. Please show me the way to create dll project says:

    Now i have created a native C++ dll project but when using then it dont work.Thanks you for give me any ideal

Leave a Reply