0

I could successfully change the mouse cursor in a vanilla C# app as explained here. I am using a C# app which uses Zedgraph dll for plotting charts. While the mouse pointer is on top of the chart it turns in to a crosswire. I need to change the cursor in to a another image. However i am unable to do this using the earlier code sample. I suspect this is because Zedgraph library already overloads the cursor change events. zgObj is the Zedgraph object in the code given below. Any ideas?

void ToggleCursor()
{
   Bitmap bitmap = new Bitmap(@"C:\Documents and Settings\Martin\My Documents\My Pictures\line.bmp");

   zgObj.Cursor = XCursor.CreateCursor(bitmap, 0, 0);

   bitmap.Dispose();
}

public class XCursor : Form
{
   [DllImport("user32.dll")]
   public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]

   public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

   public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
   {
      IntPtr ptr = bmp.GetHicon();
      IconInfo tmp = new IconInfo();
      GetIconInfo(ptr, ref tmp);
      tmp.xHotspot = xHotSpot;
      tmp.yHotspot = yHotSpot;
      tmp.fIcon = false;
      ptr = CreateIconIndirect(ref tmp);
      return new Cursor(ptr);
   }
}

public struct IconInfo
{
   public bool fIcon;
   public int xHotspot;
   public int yHotspot;
   public IntPtr hbmMask;
   public IntPtr hbmColor;
}
Martin
  • 3,396
  • 5
  • 41
  • 67

2 Answers2

1

The ZedGraph control has a Cursor property. Set it to whatever you want.

coffee_machine
  • 1,203
  • 14
  • 28
  • Doesn't work for me. Seems obvious but it's strange. I change the cursor on a "mouse down" event, but if I move the mouse at all, the cursor changes back to a cross, even though I do not do this in my code anywhere. – John Kroetch Oct 24 '13 at 19:25
0

Finally solved the problem as shown below

Cursor lineCursor = null; //declared in the main application class
    bShowLineCursor = false;  

private void zgObj_CursorChanged(object sender, EventArgs e)
{
  if (bShowLineCursor)
  {
     bShowLineCursor = false;
     Bitmap bitmap = new Bitmap(@"C:\Documents and Settings\My Documents\My Pictures\line.bmp");
     lineCursor= XCursor.CreateCursor(bitmap, 0, 0);
     bitmap.Dispose();
  }

  if (lineCursor != null)
     zgObj.Cursor = lineCursor;

}

void ToggleCursor() 
{
   bShowLineCursor = true;
}

public class XCursor : Form
{
   [DllImport("user32.dll")]
   public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]

   public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

   public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
   {
      IntPtr ptr = bmp.GetHicon();
      IconInfo tmp = new IconInfo();
      GetIconInfo(ptr, ref tmp);
      tmp.xHotspot = xHotSpot;
      tmp.yHotspot = yHotSpot;
      tmp.fIcon = false;
      ptr = CreateIconIndirect(ref tmp);
      return new Cursor(ptr);
   }
}

public struct IconInfo
{
   public bool fIcon;
   public int xHotspot;
   public int yHotspot;
   public IntPtr hbmMask;
   public IntPtr hbmColor;
}
Martin
  • 3,396
  • 5
  • 41
  • 67