Embedded Color Cursors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • H. Williams

    Embedded Color Cursors

    I know the .Net Cursor class doesn't work with color cursors. So I'm
    currently using the LoadCursorFromF ile API with reflection to set color
    cursors:

    here is my code:

    [DllImport("user 32.dll")] public static extern IntPtr LoadCursorFromF ile(
    string fileName );
    IntPtr hwdCursor= LoadCursorFromF ile( "color.cur" );
    myCursor.GetTyp e().InvokeMembe r("handle",Bind ingFlags.Public |
    BindingFlags.No nPublic |BindingFlags.I nstance |
    System.Reflecti on.BindingFlags .SetField,null, myCursor,new object [] {
    hwdCursor} );

    It works great, but I'd like to be able to embed the cursors. I've been
    trying to get theLoadCursor API to work with embedded resources and I am not
    having any luck.

    here is the code that doesn't work:

    [DllImport("user 32.dll")] public static extern IntPtr LoadCursor( long hwd,
    string fileName );
    private static IntPtr hInstance =
    Marshal.GetHINS TANCE(Assembly. GetExecutingAss embly().GetModu les()[0]);
    private static IntPtr hwdRedCursor=
    LoadCursor((lon g)hInstance,"co lor.cur" );

    Any suggestions?


  • AndrewEames

    #2
    RE: Embedded Color Cursors

    This works for me
    Andrew

    public static Cursor LoadColorCursor (string resourceName,Po int hotspot)
    {
    Assembly ass = Assembly.GetCal lingAssembly();
    Stream stream = ass.GetManifest ResourceStream( resourceName);
    // In Windows 2000 in 16 bit color mode, the cursor mask doesn't work
    properly
    // I have no idea why but the simple loading the cursor from the
    resource stream
    // works in this case - again, I've no idea why!!!
    if((Environment .OSVersion.Vers ion.Major < 5 ||
    (Environment.OS Version.Version .Major == 5 &&
    Environment.OSV ersion.Version. Minor == 0)) &&
    GetColorDepth() == 16)

    return new Cursor(stream);
    else
    {
    using(Bitmap bm = new Bitmap(stream))
    {
    NativeMethods.I CONINFO ii = new NativeMethods.I CONINFO();
    ii.IsIcon = false;
    ii.xHotspot = hotspot.X;
    ii.yHotspot = hotspot.Y;
    IntPtr imgHandle = bm.GetHbitmap() ;
    try
    {
    ii.MaskBitmap = imgHandle;
    ii.ColorBitmap = imgHandle;
    IntPtr handle = SafeNativeMetho ds.CreateIconIn direct(ref ii);
    return new Cursor(handle);
    }
    finally
    {
    SafeNativeMetho ds.DeleteObject (imgHandle);
    }
    }
    }
    }


    "H. Williams" wrote:
    [color=blue]
    > I know the .Net Cursor class doesn't work with color cursors. So I'm
    > currently using the LoadCursorFromF ile API with reflection to set color
    > cursors:
    >
    > here is my code:
    >
    > [DllImport("user 32.dll")] public static extern IntPtr LoadCursorFromF ile(
    > string fileName );
    > IntPtr hwdCursor= LoadCursorFromF ile( "color.cur" );
    > myCursor.GetTyp e().InvokeMembe r("handle",Bind ingFlags.Public |
    > BindingFlags.No nPublic |BindingFlags.I nstance |
    > System.Reflecti on.BindingFlags .SetField,null, myCursor,new object [] {
    > hwdCursor} );
    >
    > It works great, but I'd like to be able to embed the cursors. I've been
    > trying to get theLoadCursor API to work with embedded resources and I am not
    > having any luck.
    >
    > here is the code that doesn't work:
    >
    > [DllImport("user 32.dll")] public static extern IntPtr LoadCursor( long hwd,
    > string fileName );
    > private static IntPtr hInstance =
    > Marshal.GetHINS TANCE(Assembly. GetExecutingAss embly().GetModu les()[0]);
    > private static IntPtr hwdRedCursor=
    > LoadCursor((lon g)hInstance,"co lor.cur" );
    >
    > Any suggestions?
    >
    >
    >[/color]

    Comment

    • H. Williams

      #3
      Re: Embedded Color Cursors

      thanks for your reply, but the Cursor class doesn't work with COLOR cursors.
      In your code, the line 'return new Cursor(stream)' causes the color cursor
      to be converted to black and white. Also the line
      'using(Bitmap bm = new Bitmap(stream)) ' throws a 'System.Argumen tException,
      Invalid Parameter Used'
      I think the only way to get the cursors in color is to load the cursor and
      use reflection to point the current cursor handle to the handle of the color
      cursor. This is easy with the LoadCursorFromF ile API method because the
      cursor files return a handle. However, I don't want to distribute cursors
      with my program so I need help on how to get a handle to an embedded
      resource.


      "AndrewEame s" <AndrewEames@di scussions.micro soft.com> wrote in message
      news:D78D1E35-29B5-4B09-9C28-1F584CF53E2B@mi crosoft.com...[color=blue]
      > This works for me
      > Andrew
      >
      > public static Cursor LoadColorCursor (string resourceName,Po int hotspot)
      > {
      > Assembly ass = Assembly.GetCal lingAssembly();
      > Stream stream = ass.GetManifest ResourceStream( resourceName);
      > // In Windows 2000 in 16 bit color mode, the cursor mask doesn't work
      > properly
      > // I have no idea why but the simple loading the cursor from the
      > resource stream
      > // works in this case - again, I've no idea why!!!
      > if((Environment .OSVersion.Vers ion.Major < 5 ||
      > (Environment.OS Version.Version .Major == 5 &&
      > Environment.OSV ersion.Version. Minor == 0)) &&
      > GetColorDepth() == 16)
      >
      > return new Cursor(stream);
      > else
      > {
      > using(Bitmap bm = new Bitmap(stream))
      > {
      > NativeMethods.I CONINFO ii = new NativeMethods.I CONINFO();
      > ii.IsIcon = false;
      > ii.xHotspot = hotspot.X;
      > ii.yHotspot = hotspot.Y;
      > IntPtr imgHandle = bm.GetHbitmap() ;
      > try
      > {
      > ii.MaskBitmap = imgHandle;
      > ii.ColorBitmap = imgHandle;
      > IntPtr handle = SafeNativeMetho ds.CreateIconIn direct(ref ii);
      > return new Cursor(handle);
      > }
      > finally
      > {
      > SafeNativeMetho ds.DeleteObject (imgHandle);
      > }
      > }
      > }
      > }
      >
      >
      > "H. Williams" wrote:
      >[color=green]
      >> I know the .Net Cursor class doesn't work with color cursors. So I'm
      >> currently using the LoadCursorFromF ile API with reflection to set color
      >> cursors:
      >>
      >> here is my code:
      >>
      >> [DllImport("user 32.dll")] public static extern IntPtr LoadCursorFromF ile(
      >> string fileName );
      >> IntPtr hwdCursor= LoadCursorFromF ile( "color.cur" );
      >> myCursor.GetTyp e().InvokeMembe r("handle",Bind ingFlags.Public |
      >> BindingFlags.No nPublic |BindingFlags.I nstance |
      >> System.Reflecti on.BindingFlags .SetField,null, myCursor,new object [] {
      >> hwdCursor} );
      >>
      >> It works great, but I'd like to be able to embed the cursors. I've been
      >> trying to get theLoadCursor API to work with embedded resources and I am
      >> not
      >> having any luck.
      >>
      >> here is the code that doesn't work:
      >>
      >> [DllImport("user 32.dll")] public static extern IntPtr LoadCursor( long
      >> hwd,
      >> string fileName );
      >> private static IntPtr hInstance =
      >> Marshal.GetHINS TANCE(Assembly. GetExecutingAss embly().GetModu les()[0]);
      >> private static IntPtr hwdRedCursor=
      >> LoadCursor((lon g)hInstance,"co lor.cur" );
      >>
      >> Any suggestions?
      >>
      >>
      >>[/color][/color]


      Comment

      • James Park

        #4
        Re: Embedded Color Cursors

        Assuming no one comes up with anything better, you could embed the cursor,
        write it to a temp file, and then use LoadCursorFromF ile on that.

        "H. Williams" <hwilliams@osla w.com> wrote in message
        news:ESesf.4733 7$tV6.11027@new ssvr27.news.pro digy.net...[color=blue]
        > thanks for your reply, but the Cursor class doesn't work with COLOR
        > cursors. In your code, the line 'return new Cursor(stream)' causes the
        > color cursor to be converted to black and white. Also the line
        > 'using(Bitmap bm = new Bitmap(stream)) ' throws a
        > 'System.Argumen tException, Invalid Parameter Used'
        > I think the only way to get the cursors in color is to load the cursor and
        > use reflection to point the current cursor handle to the handle of the
        > color cursor. This is easy with the LoadCursorFromF ile API method because
        > the cursor files return a handle. However, I don't want to distribute
        > cursors with my program so I need help on how to get a handle to an
        > embedded resource.
        >
        >
        > "AndrewEame s" <AndrewEames@di scussions.micro soft.com> wrote in message
        > news:D78D1E35-29B5-4B09-9C28-1F584CF53E2B@mi crosoft.com...[color=green]
        >> This works for me
        >> Andrew
        >>
        >> public static Cursor LoadColorCursor (string resourceName,Po int
        >> hotspot)
        >> {
        >> Assembly ass = Assembly.GetCal lingAssembly();
        >> Stream stream = ass.GetManifest ResourceStream( resourceName);
        >> // In Windows 2000 in 16 bit color mode, the cursor mask doesn't
        >> work
        >> properly
        >> // I have no idea why but the simple loading the cursor from the
        >> resource stream
        >> // works in this case - again, I've no idea why!!!
        >> if((Environment .OSVersion.Vers ion.Major < 5 ||
        >> (Environment.OS Version.Version .Major == 5 &&
        >> Environment.OSV ersion.Version. Minor == 0)) &&
        >> GetColorDepth() == 16)
        >>
        >> return new Cursor(stream);
        >> else
        >> {
        >> using(Bitmap bm = new Bitmap(stream))
        >> {
        >> NativeMethods.I CONINFO ii = new NativeMethods.I CONINFO();
        >> ii.IsIcon = false;
        >> ii.xHotspot = hotspot.X;
        >> ii.yHotspot = hotspot.Y;
        >> IntPtr imgHandle = bm.GetHbitmap() ;
        >> try
        >> {
        >> ii.MaskBitmap = imgHandle;
        >> ii.ColorBitmap = imgHandle;
        >> IntPtr handle = SafeNativeMetho ds.CreateIconIn direct(ref ii);
        >> return new Cursor(handle);
        >> }
        >> finally
        >> {
        >> SafeNativeMetho ds.DeleteObject (imgHandle);
        >> }
        >> }
        >> }
        >> }
        >>
        >>
        >> "H. Williams" wrote:
        >>[color=darkred]
        >>> I know the .Net Cursor class doesn't work with color cursors. So I'm
        >>> currently using the LoadCursorFromF ile API with reflection to set color
        >>> cursors:
        >>>
        >>> here is my code:
        >>>
        >>> [DllImport("user 32.dll")] public static extern IntPtr
        >>> LoadCursorFromF ile(
        >>> string fileName );
        >>> IntPtr hwdCursor= LoadCursorFromF ile( "color.cur" );
        >>> myCursor.GetTyp e().InvokeMembe r("handle",Bind ingFlags.Public |
        >>> BindingFlags.No nPublic |BindingFlags.I nstance |
        >>> System.Reflecti on.BindingFlags .SetField,null, myCursor,new object [] {
        >>> hwdCursor} );
        >>>
        >>> It works great, but I'd like to be able to embed the cursors. I've been
        >>> trying to get theLoadCursor API to work with embedded resources and I am
        >>> not
        >>> having any luck.
        >>>
        >>> here is the code that doesn't work:
        >>>
        >>> [DllImport("user 32.dll")] public static extern IntPtr LoadCursor( long
        >>> hwd,
        >>> string fileName );
        >>> private static IntPtr hInstance =
        >>> Marshal.GetHINS TANCE(Assembly. GetExecutingAss embly().GetModu les()[0]);
        >>> private static IntPtr hwdRedCursor=
        >>> LoadCursor((lon g)hInstance,"co lor.cur" );
        >>>
        >>> Any suggestions?
        >>>
        >>>
        >>>[/color][/color]
        >
        >[/color]


        Comment

        • H. Williams

          #5
          Re: Embedded Color Cursors

          That appears to be the only solution.
          I found this note:
          'The only thing that can't be done as things stand is to instantiate a color
          cursor from a .NET resource file without writing it to an intermediate
          temporary file on disk. This is because LoadImage (or LoadCursor)
          unfortunately does not provide a stream or byte array based overload.'


          "James Park" <someone@hotmai l.com> wrote in message
          news:%238HDEbxC GHA.2704@TK2MSF TNGP11.phx.gbl. ..[color=blue]
          > Assuming no one comes up with anything better, you could embed the cursor,
          > write it to a temp file, and then use LoadCursorFromF ile on that.
          >
          > "H. Williams" <hwilliams@osla w.com> wrote in message
          > news:ESesf.4733 7$tV6.11027@new ssvr27.news.pro digy.net...[color=green]
          >> thanks for your reply, but the Cursor class doesn't work with COLOR
          >> cursors. In your code, the line 'return new Cursor(stream)' causes the
          >> color cursor to be converted to black and white. Also the line
          >> 'using(Bitmap bm = new Bitmap(stream)) ' throws a
          >> 'System.Argumen tException, Invalid Parameter Used'
          >> I think the only way to get the cursors in color is to load the cursor
          >> and use reflection to point the current cursor handle to the handle of
          >> the color cursor. This is easy with the LoadCursorFromF ile API method
          >> because the cursor files return a handle. However, I don't want to
          >> distribute cursors with my program so I need help on how to get a handle
          >> to an embedded resource.
          >>
          >>
          >> "AndrewEame s" <AndrewEames@di scussions.micro soft.com> wrote in message
          >> news:D78D1E35-29B5-4B09-9C28-1F584CF53E2B@mi crosoft.com...[color=darkred]
          >>> This works for me
          >>> Andrew
          >>>
          >>> public static Cursor LoadColorCursor (string resourceName,Po int
          >>> hotspot)
          >>> {
          >>> Assembly ass = Assembly.GetCal lingAssembly();
          >>> Stream stream = ass.GetManifest ResourceStream( resourceName);
          >>> // In Windows 2000 in 16 bit color mode, the cursor mask doesn't
          >>> work
          >>> properly
          >>> // I have no idea why but the simple loading the cursor from the
          >>> resource stream
          >>> // works in this case - again, I've no idea why!!!
          >>> if((Environment .OSVersion.Vers ion.Major < 5 ||
          >>> (Environment.OS Version.Version .Major == 5 &&
          >>> Environment.OSV ersion.Version. Minor == 0)) &&
          >>> GetColorDepth() == 16)
          >>>
          >>> return new Cursor(stream);
          >>> else
          >>> {
          >>> using(Bitmap bm = new Bitmap(stream))
          >>> {
          >>> NativeMethods.I CONINFO ii = new NativeMethods.I CONINFO();
          >>> ii.IsIcon = false;
          >>> ii.xHotspot = hotspot.X;
          >>> ii.yHotspot = hotspot.Y;
          >>> IntPtr imgHandle = bm.GetHbitmap() ;
          >>> try
          >>> {
          >>> ii.MaskBitmap = imgHandle;
          >>> ii.ColorBitmap = imgHandle;
          >>> IntPtr handle = SafeNativeMetho ds.CreateIconIn direct(ref ii);
          >>> return new Cursor(handle);
          >>> }
          >>> finally
          >>> {
          >>> SafeNativeMetho ds.DeleteObject (imgHandle);
          >>> }
          >>> }
          >>> }
          >>> }
          >>>
          >>>
          >>> "H. Williams" wrote:
          >>>
          >>>> I know the .Net Cursor class doesn't work with color cursors. So I'm
          >>>> currently using the LoadCursorFromF ile API with reflection to set color
          >>>> cursors:
          >>>>
          >>>> here is my code:
          >>>>
          >>>> [DllImport("user 32.dll")] public static extern IntPtr
          >>>> LoadCursorFromF ile(
          >>>> string fileName );
          >>>> IntPtr hwdCursor= LoadCursorFromF ile( "color.cur" );
          >>>> myCursor.GetTyp e().InvokeMembe r("handle",Bind ingFlags.Public |
          >>>> BindingFlags.No nPublic |BindingFlags.I nstance |
          >>>> System.Reflecti on.BindingFlags .SetField,null, myCursor,new object [] {
          >>>> hwdCursor} );
          >>>>
          >>>> It works great, but I'd like to be able to embed the cursors. I've
          >>>> been
          >>>> trying to get theLoadCursor API to work with embedded resources and I
          >>>> am not
          >>>> having any luck.
          >>>>
          >>>> here is the code that doesn't work:
          >>>>
          >>>> [DllImport("user 32.dll")] public static extern IntPtr LoadCursor( long
          >>>> hwd,
          >>>> string fileName );
          >>>> private static IntPtr hInstance =
          >>>> Marshal.GetHINS TANCE(Assembly. GetExecutingAss embly().GetModu les()[0]);
          >>>> private static IntPtr hwdRedCursor=
          >>>> LoadCursor((lon g)hInstance,"co lor.cur" );
          >>>>
          >>>> Any suggestions?
          >>>>
          >>>>
          >>>>[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • James Park

            #6
            Re: Embedded Color Cursors

            I just tried out another solution, but it's a bit of a pain. Create a
            resource file (.rc) that includes the cursor using VC++, create a win32
            resource file (.res) from it with Resource Compiler (rc.exe), use the
            compiler option /win32res to attach it, and then use LoadCursor in normal
            fashion. I used IntPtr instead of string for lpCursorName argument though.

            "H. Williams" <hwilliams@osla w.com> wrote in message
            news:%ggsf.4084 4$q%.6932@newss vr12.news.prodi gy.com...[color=blue]
            > That appears to be the only solution.
            > I found this note:
            > 'The only thing that can't be done as things stand is to instantiate a
            > color cursor from a .NET resource file without writing it to an
            > intermediate temporary file on disk. This is because LoadImage (or
            > LoadCursor) unfortunately does not provide a stream or byte array based
            > overload.'
            >
            >
            > "James Park" <someone@hotmai l.com> wrote in message
            > news:%238HDEbxC GHA.2704@TK2MSF TNGP11.phx.gbl. ..[color=green]
            >> Assuming no one comes up with anything better, you could embed the
            >> cursor, write it to a temp file, and then use LoadCursorFromF ile on that.
            >>
            >> "H. Williams" <hwilliams@osla w.com> wrote in message
            >> news:ESesf.4733 7$tV6.11027@new ssvr27.news.pro digy.net...[color=darkred]
            >>> thanks for your reply, but the Cursor class doesn't work with COLOR
            >>> cursors. In your code, the line 'return new Cursor(stream)' causes the
            >>> color cursor to be converted to black and white. Also the line
            >>> 'using(Bitmap bm = new Bitmap(stream)) ' throws a
            >>> 'System.Argumen tException, Invalid Parameter Used'
            >>> I think the only way to get the cursors in color is to load the cursor
            >>> and use reflection to point the current cursor handle to the handle of
            >>> the color cursor. This is easy with the LoadCursorFromF ile API method
            >>> because the cursor files return a handle. However, I don't want to
            >>> distribute cursors with my program so I need help on how to get a handle
            >>> to an embedded resource.
            >>>
            >>>
            >>> "AndrewEame s" <AndrewEames@di scussions.micro soft.com> wrote in message
            >>> news:D78D1E35-29B5-4B09-9C28-1F584CF53E2B@mi crosoft.com...
            >>>> This works for me
            >>>> Andrew
            >>>>
            >>>> public static Cursor LoadColorCursor (string resourceName,Po int
            >>>> hotspot)
            >>>> {
            >>>> Assembly ass = Assembly.GetCal lingAssembly();
            >>>> Stream stream = ass.GetManifest ResourceStream( resourceName);
            >>>> // In Windows 2000 in 16 bit color mode, the cursor mask doesn't
            >>>> work
            >>>> properly
            >>>> // I have no idea why but the simple loading the cursor from the
            >>>> resource stream
            >>>> // works in this case - again, I've no idea why!!!
            >>>> if((Environment .OSVersion.Vers ion.Major < 5 ||
            >>>> (Environment.OS Version.Version .Major == 5 &&
            >>>> Environment.OSV ersion.Version. Minor == 0)) &&
            >>>> GetColorDepth() == 16)
            >>>>
            >>>> return new Cursor(stream);
            >>>> else
            >>>> {
            >>>> using(Bitmap bm = new Bitmap(stream))
            >>>> {
            >>>> NativeMethods.I CONINFO ii = new NativeMethods.I CONINFO();
            >>>> ii.IsIcon = false;
            >>>> ii.xHotspot = hotspot.X;
            >>>> ii.yHotspot = hotspot.Y;
            >>>> IntPtr imgHandle = bm.GetHbitmap() ;
            >>>> try
            >>>> {
            >>>> ii.MaskBitmap = imgHandle;
            >>>> ii.ColorBitmap = imgHandle;
            >>>> IntPtr handle = SafeNativeMetho ds.CreateIconIn direct(ref
            >>>> ii);
            >>>> return new Cursor(handle);
            >>>> }
            >>>> finally
            >>>> {
            >>>> SafeNativeMetho ds.DeleteObject (imgHandle);
            >>>> }
            >>>> }
            >>>> }
            >>>> }
            >>>>
            >>>>
            >>>> "H. Williams" wrote:
            >>>>
            >>>>> I know the .Net Cursor class doesn't work with color cursors. So I'm
            >>>>> currently using the LoadCursorFromF ile API with reflection to set
            >>>>> color
            >>>>> cursors:
            >>>>>
            >>>>> here is my code:
            >>>>>
            >>>>> [DllImport("user 32.dll")] public static extern IntPtr
            >>>>> LoadCursorFromF ile(
            >>>>> string fileName );
            >>>>> IntPtr hwdCursor= LoadCursorFromF ile( "color.cur" );
            >>>>> myCursor.GetTyp e().InvokeMembe r("handle",Bind ingFlags.Public |
            >>>>> BindingFlags.No nPublic |BindingFlags.I nstance |
            >>>>> System.Reflecti on.BindingFlags .SetField,null, myCursor,new object [] {
            >>>>> hwdCursor} );
            >>>>>
            >>>>> It works great, but I'd like to be able to embed the cursors. I've
            >>>>> been
            >>>>> trying to get theLoadCursor API to work with embedded resources and I
            >>>>> am not
            >>>>> having any luck.
            >>>>>
            >>>>> here is the code that doesn't work:
            >>>>>
            >>>>> [DllImport("user 32.dll")] public static extern IntPtr LoadCursor( long
            >>>>> hwd,
            >>>>> string fileName );
            >>>>> private static IntPtr hInstance =
            >>>>> Marshal.GetHINS TANCE(Assembly. GetExecutingAss embly().GetModu les()[0]);
            >>>>> private static IntPtr hwdRedCursor=
            >>>>> LoadCursor((lon g)hInstance,"co lor.cur" );
            >>>>>
            >>>>> Any suggestions?
            >>>>>
            >>>>>
            >>>>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • H. Williams

              #7
              Re: Embedded Color Cursors

              Here's my somewhat long solution using the temp file. It's fast, there are
              no additional files to include with the program, and it works.

              private static IntPtr GetCursorHandle (string sResource)
              {
              //Load cursor from Manifest Resource to Stream
              Stream streamFrom =
              Assembly.GetExe cutingAssembly( ).GetManifestRe sourceStream(sR esource);
              Stream streamTo =
              File.Create(Env ironment.GetEnv ironmentVariabl e("TEMP")+@"\~c ur.tmp");
              BinaryReader br = new BinaryReader(st reamFrom);
              BinaryWriter bw = new BinaryWriter(st reamTo);
              //Write cursor to temporary file
              bw.Write(br.Rea dBytes((int)str eamFrom.Length) );
              bw.Flush();
              bw.Close();
              br.Close();
              bw=null;
              br=null;
              streamFrom.Clos e();
              streamTo.Close( );
              streamFrom=null ;
              streamTo=null;
              //Load handle of temporary cursor file
              IntPtr hwdCursor = LoadCursorFromF ile(
              Environment.Get EnvironmentVari able("TEMP")+@" \~cur.tmp" );
              //Delete temporary cursor file
              File.Delete(Env ironment.GetEnv ironmentVariabl e("TEMP")+@"\~c ur.tmp");
              return hwdCursor;
              }


              Comment

              • AndrewEames

                #8
                Re: Embedded Color Cursors

                Did you actually try this? The Cursor class does actually work with color
                cursors in some circumstances.

                The line 'return new Cursor(stream)' is only executed under Windows 2000 on
                16 bit displays and as the comment indicates, I dont why this trick works in
                that case
                Andrew

                "H. Williams" wrote:
                [color=blue]
                > thanks for your reply, but the Cursor class doesn't work with COLOR cursors.
                > In your code, the line 'return new Cursor(stream)' causes the color cursor
                > to be converted to black and white. Also the line
                > 'using(Bitmap bm = new Bitmap(stream)) ' throws a 'System.Argumen tException,
                > Invalid Parameter Used'
                > I think the only way to get the cursors in color is to load the cursor and
                > use reflection to point the current cursor handle to the handle of the color
                > cursor. This is easy with the LoadCursorFromF ile API method because the
                > cursor files return a handle. However, I don't want to distribute cursors
                > with my program so I need help on how to get a handle to an embedded
                > resource.
                >
                >
                > "AndrewEame s" <AndrewEames@di scussions.micro soft.com> wrote in message
                > news:D78D1E35-29B5-4B09-9C28-1F584CF53E2B@mi crosoft.com...[color=green]
                > > This works for me
                > > Andrew
                > >
                > > public static Cursor LoadColorCursor (string resourceName,Po int hotspot)
                > > {
                > > Assembly ass = Assembly.GetCal lingAssembly();
                > > Stream stream = ass.GetManifest ResourceStream( resourceName);
                > > // In Windows 2000 in 16 bit color mode, the cursor mask doesn't work
                > > properly
                > > // I have no idea why but the simple loading the cursor from the
                > > resource stream
                > > // works in this case - again, I've no idea why!!!
                > > if((Environment .OSVersion.Vers ion.Major < 5 ||
                > > (Environment.OS Version.Version .Major == 5 &&
                > > Environment.OSV ersion.Version. Minor == 0)) &&
                > > GetColorDepth() == 16)
                > >
                > > return new Cursor(stream);
                > > else
                > > {
                > > using(Bitmap bm = new Bitmap(stream))
                > > {
                > > NativeMethods.I CONINFO ii = new NativeMethods.I CONINFO();
                > > ii.IsIcon = false;
                > > ii.xHotspot = hotspot.X;
                > > ii.yHotspot = hotspot.Y;
                > > IntPtr imgHandle = bm.GetHbitmap() ;
                > > try
                > > {
                > > ii.MaskBitmap = imgHandle;
                > > ii.ColorBitmap = imgHandle;
                > > IntPtr handle = SafeNativeMetho ds.CreateIconIn direct(ref ii);
                > > return new Cursor(handle);
                > > }
                > > finally
                > > {
                > > SafeNativeMetho ds.DeleteObject (imgHandle);
                > > }
                > > }
                > > }
                > > }
                > >
                > >
                > > "H. Williams" wrote:
                > >[color=darkred]
                > >> I know the .Net Cursor class doesn't work with color cursors. So I'm
                > >> currently using the LoadCursorFromF ile API with reflection to set color
                > >> cursors:
                > >>
                > >> here is my code:
                > >>
                > >> [DllImport("user 32.dll")] public static extern IntPtr LoadCursorFromF ile(
                > >> string fileName );
                > >> IntPtr hwdCursor= LoadCursorFromF ile( "color.cur" );
                > >> myCursor.GetTyp e().InvokeMembe r("handle",Bind ingFlags.Public |
                > >> BindingFlags.No nPublic |BindingFlags.I nstance |
                > >> System.Reflecti on.BindingFlags .SetField,null, myCursor,new object [] {
                > >> hwdCursor} );
                > >>
                > >> It works great, but I'd like to be able to embed the cursors. I've been
                > >> trying to get theLoadCursor API to work with embedded resources and I am
                > >> not
                > >> having any luck.
                > >>
                > >> here is the code that doesn't work:
                > >>
                > >> [DllImport("user 32.dll")] public static extern IntPtr LoadCursor( long
                > >> hwd,
                > >> string fileName );
                > >> private static IntPtr hInstance =
                > >> Marshal.GetHINS TANCE(Assembly. GetExecutingAss embly().GetModu les()[0]);
                > >> private static IntPtr hwdRedCursor=
                > >> LoadCursor((lon g)hInstance,"co lor.cur" );
                > >>
                > >> Any suggestions?
                > >>
                > >>
                > >>[/color][/color]
                >
                >
                >[/color]

                Comment

                Working...