Member Avatar for nssltd

Hi well i posted a earlier thread but no one has replied in three days i have one reply, but it's not what i wanted.

he cursor i am using is one from axialis cursor workshop examples. it is a .cur and it does work as i have changed my windows default cursors to them. it is a colored cursor blue if you want to see it find it here
i have used these methods;

this.Cursor = new Cursor(GetType(), "a.cur");
Cursor myCursor = new Cursor("myCursor.cur");
MyControl1.Cursor = myCursor
IntPtr ptr = IMP.Properties.Resources.zoom_in.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;

The last method does work but you need to put the ENTIRE image/Cursor over the component IE button. also you need to use an image file. What i would like to achieve would be to use a .cur/.ani file on a windows form that my cursor
(here)will become the cursor on my form instead of the windows default one

Any help is MUCH appreciated

Thanks in advance,

NSSLTD

What exactly is the problem when you try to use the .cur file?
Does it not load or does it load incorrectly? Bear in mind that the Cursor class only supports black and White cursors:

Note The Cursor class does not support animated cursors (.ani files) or cursors with colors other than black and white.
from msdn Cursor Class

Just found this that might be of interest. It shows how to load a bitmap as a custom cursor and include a hotspot. Setting the hotspot should resolve the issue you have with needing to place the whole image over a control to interact with it.

Member Avatar for nssltd

K will check it out, thanks.

Member Avatar for nssltd

hi well it works but it wont let me add any components to the form! and i cannot find the part of the code that prevents adding components the form! i'll post the code down and see if anyone can help me

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CursorTest
{
  public struct IconInfo
  {
    public bool fIcon;
    public int xHotspot;
    public int yHotspot;
    public IntPtr hbmMask;
    public IntPtr hbmColor;
  }

  public class CursorTest : Form
  {
      private Button button1;
  
    public CursorTest()
    {
      this.Text = "Cursor Test";

      Image newImage = Image.FromFile("Familly.png");

        
      Pen p;
      SolidBrush b, bT = new SolidBrush(Color.Black);
      Graphics gr = Graphics.FromHwnd(this.Handle);
      Color cP = Color.Gray;
      Color cB = Color.LightGray;
      p = new Pen(cP, 6);
      b = new SolidBrush(cB);

     

      // Create Point for upper-left corner of image.
      Point ulCorner = new Point(0, 0);
        Bitmap bitmap = new Bitmap(140, 25);
      Graphics g = Graphics.FromImage(bitmap);
      using (Font f = new Font(FontFamily.GenericSansSerif, 10))
          g.DrawImage(newImage, ulCorner);
      this.Cursor = CreateCursor(bitmap, 3, 3);

      bitmap.Dispose();
    }

    [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);
    }

    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(-6, 1);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(91, 84);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        // 
        // CursorTest
        // 
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.button1);
        this.Name = "CursorTest";
        this.Load += new System.EventHandler(this.CursorTest_Load);
        this.ResumeLayout(false);

    }

    private void CursorTest_Load(object sender, EventArgs e)
    {

    }
  }
}

You've missed out InitializeComponent from the form constructor.

commented: exactly it :p +2
Member Avatar for nssltd

Oh my i have what an stupid mistake... sorry to have bothered you. im really shocked i missed that.
Thanks for pointing out my error!
NSSLTD

Member Avatar for nssltd

Thanks everyone my cursor problems are solved... thanks to everyone that helped.
Happy CODING!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.