Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. Color issue with custom cursor

Color issue with custom cursor

Scheduled Pinned Locked Moved Graphics
graphicscsharpcomgame-devjson
5 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #1

    I successfully create a custom cursor from a 32x32 32bpppArgb (GDI) Bitmap with the following code:

    public static Cursor ToCursor(System.Drawing.Bitmap bmp, byte x, byte y)
    {
    var stream = new MemoryStream();
    var hIcon = bmp.GetHicon();
    try
    {
    System.Drawing.Icon.FromHandle(hIcon).Save(stream);
    }
    finally { NativeMethods.DestroyIcon(hIcon); }

    stream.Seek(2, SeekOrigin.Begin);
    stream.WriteByte(2);
    stream.Seek(10, SeekOrigin.Begin);
    stream.WriteByte(x);
    stream.Seek(12, SeekOrigin.Begin);
    stream.WriteByte(y);
    stream.Seek(0, SeekOrigin.Begin);
    
    return new Cursor(stream);
    

    }

    However the colors of my cursor are all wrong! It looks like it is using a palette / a limited set of 16/32/256 colors?! Is it possible to have 24bits/32bits colors cursor? (i.e. with millions of possible color value) [EDIT] Solved! I found a way to do it! With the following API in sequences: GetHIcon() CreateIconIndirect() CursorInteropHelper There was just some nagging issue to avoid GDI Object leaks which I solved by creating a class SafeIconHandle : SafeHandle

    All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

    L B 2 Replies Last reply
    0
    • S Super Lloyd

      I successfully create a custom cursor from a 32x32 32bpppArgb (GDI) Bitmap with the following code:

      public static Cursor ToCursor(System.Drawing.Bitmap bmp, byte x, byte y)
      {
      var stream = new MemoryStream();
      var hIcon = bmp.GetHicon();
      try
      {
      System.Drawing.Icon.FromHandle(hIcon).Save(stream);
      }
      finally { NativeMethods.DestroyIcon(hIcon); }

      stream.Seek(2, SeekOrigin.Begin);
      stream.WriteByte(2);
      stream.Seek(10, SeekOrigin.Begin);
      stream.WriteByte(x);
      stream.Seek(12, SeekOrigin.Begin);
      stream.WriteByte(y);
      stream.Seek(0, SeekOrigin.Begin);
      
      return new Cursor(stream);
      

      }

      However the colors of my cursor are all wrong! It looks like it is using a palette / a limited set of 16/32/256 colors?! Is it possible to have 24bits/32bits colors cursor? (i.e. with millions of possible color value) [EDIT] Solved! I found a way to do it! With the following API in sequences: GetHIcon() CreateIconIndirect() CursorInteropHelper There was just some nagging issue to avoid GDI Object leaks which I solved by creating a class SafeIconHandle : SafeHandle

      All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      http://stackoverflow.com/questions/4305800/using-custom-colored-cursors-in-a-c-sharp-windows-application[^] :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

      S 1 Reply Last reply
      0
      • L Lost User

        http://stackoverflow.com/questions/4305800/using-custom-colored-cursors-in-a-c-sharp-windows-application[^] :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

        S Offline
        S Offline
        Super Lloyd
        wrote on last edited by
        #3

        My cursor are created from code generated Bitmap, won't work! Anyhow I found a way to do it! With the following API in sequences: GetHIcon() CreateIconIndirect() CursorInteropHelper There was just some nagging issue to avoid GDI Object leaks which I solved by creating a class SafeIconHandle : SafeHandle

        All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

        1 Reply Last reply
        0
        • S Super Lloyd

          I successfully create a custom cursor from a 32x32 32bpppArgb (GDI) Bitmap with the following code:

          public static Cursor ToCursor(System.Drawing.Bitmap bmp, byte x, byte y)
          {
          var stream = new MemoryStream();
          var hIcon = bmp.GetHicon();
          try
          {
          System.Drawing.Icon.FromHandle(hIcon).Save(stream);
          }
          finally { NativeMethods.DestroyIcon(hIcon); }

          stream.Seek(2, SeekOrigin.Begin);
          stream.WriteByte(2);
          stream.Seek(10, SeekOrigin.Begin);
          stream.WriteByte(x);
          stream.Seek(12, SeekOrigin.Begin);
          stream.WriteByte(y);
          stream.Seek(0, SeekOrigin.Begin);
          
          return new Cursor(stream);
          

          }

          However the colors of my cursor are all wrong! It looks like it is using a palette / a limited set of 16/32/256 colors?! Is it possible to have 24bits/32bits colors cursor? (i.e. with millions of possible color value) [EDIT] Solved! I found a way to do it! With the following API in sequences: GetHIcon() CreateIconIndirect() CursorInteropHelper There was just some nagging issue to avoid GDI Object leaks which I solved by creating a class SafeIconHandle : SafeHandle

          All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

          B Offline
          B Offline
          Brisingr Aerowing
          wrote on last edited by
          #4

          You may want to look at Extended Cursors for .Net[^] as it has a whole bundle of features.

          What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

          S 1 Reply Last reply
          0
          • B Brisingr Aerowing

            You may want to look at Extended Cursors for .Net[^] as it has a whole bundle of features.

            What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #5

            Thanks you! :) Anyhow my problem was to make a cursor that like a zoomed version of the screen!! So it doesn't help! But anyhow, all working by now!

            All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups