SDG Custom Cursors

<< Back to the SDG Toolkit page

How to: Create a Custom Cursor for your SDG Mouse.

SDG by default gives you a simple arrow for each mouse. You can change this to a custom cursor by setting the sdgManager1.Mice[i].Cursor property to:

  • a predefined Windows cursor
  • a cursor that you design
Source: SdgCustomCursorsSrc.zip

Executable: SdgTicTacToe.zip
Unzip all files (executables plus dependancies) into a single folder

The examples below show to do this.

Using a predefined .NET cursor

Its easy to set an SDG cursor to one already available in .NET. The easiest way is to use the .Net Cursors class to find them.

For example, the following code, inserted after the InitializeComponent(); function in tthe Form constructor, will modify the cursor of Mouse 0 to the standard 'Cross' cursor. Because the cursor already contains a hot spot, we don't have to set it.

Cursor cross = System.Windows.Forms.Cursors.Cross;
if (sdgManager1.Mice.Count > 0) //make sure there is at least one attached mouse
{
    sdgManager1.Mice[0].Cursor = cross;
}

Using a cursor you design

If you want to design your own cursor, you have to do a bunch of work to get it into your project. Almost all of the description is concerns how you use an image as a resource in .NET and convert it into a cursor object. The actual SDG part is trivial.

Please note that The .NET Framework Cursor class does not support animated cursors or cursors beyond the default 32x32 shape.

1. To add an image to your project as an embedded resource

  • On the Project menu, choose Add Existing Item.
  • Navigate to the bitmap(a .bmp file) you want to add to your project.
  • Click the ''Open' button to add the bitmap to your project's file list.
  • Right-click the bitmap in your project's file list and choose Properties. The Properties window appears.
  • From this window, find the Build Action property in the Properties window. Change its value to Embedded Resource.
  • At this point, you can build the project. Your image will be compiled into your project's assembly.

2. To use and convert this resource as an SDG Cursor... the example below assumes that the project name is SDGCustomCursor, and that the cursor resource was a file called sampleCursor.bmp. This code would be used similar to the previous example, and effects Mouse 1

if (sdgManager1.Mice.Count > 1) //Make sure Mouse 1 exists
{
  //This is how you access an embedded bitmap
  System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
  System.IO.Stream cursorStream = myAssembly.GetManifestResourceStream("SDGCustomCursors.sampleCursor.bmp");
  Bitmap cursorBitmap = new Bitmap (cursorStream);

  //Make the white bits around the cursor transparent
  //This assumes surrounding colors are white. If yours are not, you
  // have to set the correct color
  cursorBitmap.MakeTransparent(Color.White);

  //Now we need to get a handle to the icon from the bitmap
  //and create a new .NET cursor from it
  IntPtr ptr = cursorBitmap.GetHicon();
  Cursor c = new Cursor(ptr);

  //Set the SDG cursor to the .Net cursor
  //Also set the hot spot, which for this cursor should be in the top left corner
  sdgManager1.Mice[1].Cursor = c;
  sdgManager1.Mice[1].Hotspot = new Point (0,0);
}