Animated Cursor-Custom Control


Introduction:

As we know, the cursor is pointing to an area at where it is placed. Now we are going to make an animated cursor as a custom control. Firstly I would like to tell you what is animated cursor? An animation is a sequence of images, frames that is displayed over time. A cursor which has some image like a moving ball and moving picture and any type of icon you want on a cursor.

Now we are going to do this specific task which show how we can make a animated cursor-custom control in Visual Studio 2010. For an Animated Cursor there is a special format named an .ANI file which is being used for reading and storing the Windows Animated Cursor. It has a structure format named as Microsoft RIFF that contains information about the animation( Author, Title, Steps, Length and order of each step).

Now we are going to create a animated curso :

Step 1: Open Visual Studio 2010 and create a window application named animated_cursor.

first_image-_step1.gif

Step 2: Further, we have to add two Namespaces.

using System.IO;

using System.Runtime.InteropServices;

Step 3: Now download a cursor image of format .ANI name as 7updot.ani.

Step 4: Right click on animated_cursor file and click to add existing file and select the image of cursor which you have loaded.

Step 5: After adding the .ANI file, further you are required to paste it into the bin folder of the project.

after_step-5.gif

Step 6: Firstly look at the code of the class in which we import a dll file named user32.dll and create a static method.

Let glimpse on a class code:

public class AnimatedCurs
{
    [DllImport("User32.dll")]
    private static extern IntPtr LoadCursorFromFile(String str);
    public static Cursor Create(string fname)
    {
        // how to handle cursor.
        IntPtr HC = LoadCursorFromFile(fname);
        // Check  wheather it is succeeded or not.
        if (!IntPtr.Zero.Equals(HC))
        {
            return new Cursor(HC);
        }
        else
        {
            throw new ApplicationException("cursor was not created from file " + fname);
        }
    }
}

Step 7: Now look slightly code of form_load event in which is implementing Exception handling by using a try{} catch{} block and throwing an Exception in a class if it occurs.

Glimpse of form_load code:

private void Form1_Load(object sender, EventArgs e)
 {
   try
      {
        this.Cursor = AnimatedCurs.Create(
        Path.Combine(Application.StartupPath, "7updot.ani"));
       }
    catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
 }

The complete Code of form1.cs file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace animated_cursor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = AnimatedCurs.Create(
                    Path.Combine(Application.StartupPath, "7updot.ani"));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
    public class AnimatedCurs
    {
       [DllImport("User32.dll")]
        private static extern IntPtr LoadCursorFromFile(String str);
        public static Cursor Create(string fname)
       {
            // how to handle cursor.
            IntPtr HC = LoadCursorFromFile(fname);
            // Check  wheather it is succeeded or not.
            if (!IntPtr.Zero.Equals(HC))
            {
                return new Cursor(HC);
            }
            else
            {
                throw new ApplicationException("cursor was not created from file " + fname);
            }
        }
    }
}

Code Description : Firstly We have to make a Class name as AnimatedCurs and import a .dll that indicates the attribute method is exposed by an Unmanaged Dynamic link library as a static entry point. After that make a static method name as Create and pass the variable name as (str). And After that check that cursor is work properly or not if not then it will throw an exception. And after that we have to call the method of class in to The form_load event.

Output :

output.gif

Up Next
    Ebook Download
    View all
    Learn
    View all