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. C#
  4. GetCursorInfo in C#?

GetCursorInfo in C#?

Scheduled Pinned Locked Moved C#
csharpcomjsontutorialquestion
9 Posts 2 Posters 1 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.
  • C Offline
    C Offline
    CyberKewl
    wrote on last edited by
    #1

    Does anyone know how i can use the GetCursorInfo API in C#? I'd like to know the dllimport and an example code. Click here to view the GetCursorInfo API.

    H 1 Reply Last reply
    0
    • C CyberKewl

      Does anyone know how i can use the GetCursorInfo API in C#? I'd like to know the dllimport and an example code. Click here to view the GetCursorInfo API.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You have to recreate the sturct and ref that in your P/Invoked statement:

      public struct CursorInfo
      {
      public int size; // Or uint, but probably not necessary and
      public int flags; // isn't CLS compliant.
      IntPtr cursor;
      System.Drawing.Point point; // Should already marshal correctly.
      }
      [DllImport("user32.dll", CharSet=CharSet.Auto)]
      public static extern bool GetCursorInfo(ref CursorInfo info);

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      C 1 Reply Last reply
      0
      • H Heath Stewart

        You have to recreate the sturct and ref that in your P/Invoked statement:

        public struct CursorInfo
        {
        public int size; // Or uint, but probably not necessary and
        public int flags; // isn't CLS compliant.
        IntPtr cursor;
        System.Drawing.Point point; // Should already marshal correctly.
        }
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern bool GetCursorInfo(ref CursorInfo info);

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        C Offline
        C Offline
        CyberKewl
        wrote on last edited by
        #3

        Thanks for the quick reply but i'd like to know how to use it too. I'd like to know how to get the cursor image (or icon) using GetCursorInfo.

        H 1 Reply Last reply
        0
        • C CyberKewl

          Thanks for the quick reply but i'd like to know how to use it too. I'd like to know how to get the cursor image (or icon) using GetCursorInfo.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          It's all in the documentation on MSDN. I suggest you read it. There's examples, too. Always consult the docs for questions like this, when asking "what is X" or "how does Y" work, otherwise you're just grabbing at straws and guessing, which doesn't build any skills. I don't mean to sound rude, but researching is half the development battle, and these questions are exactly what the Windows API functions, .NET class library, and other documentation is for. Besides, it'd be a waste of time to essentially copy and paste the topic you want. Just type GetCursorInfo in the index and you've got what you want. As far as managed code goes, however, read on... If all you're trying to do is read-in a cursor image, then you need to look at the Cursor class. It's better to use managed code than to start needlessly P/Invoking stuff from the Win32 API. It has everything you apparently need to read-in a cursor and display it (either as the mouse cursor or a bitmap (with some fiddling) such as a preview pic).

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          C 1 Reply Last reply
          0
          • H Heath Stewart

            It's all in the documentation on MSDN. I suggest you read it. There's examples, too. Always consult the docs for questions like this, when asking "what is X" or "how does Y" work, otherwise you're just grabbing at straws and guessing, which doesn't build any skills. I don't mean to sound rude, but researching is half the development battle, and these questions are exactly what the Windows API functions, .NET class library, and other documentation is for. Besides, it'd be a waste of time to essentially copy and paste the topic you want. Just type GetCursorInfo in the index and you've got what you want. As far as managed code goes, however, read on... If all you're trying to do is read-in a cursor image, then you need to look at the Cursor class. It's better to use managed code than to start needlessly P/Invoking stuff from the Win32 API. It has everything you apparently need to read-in a cursor and display it (either as the mouse cursor or a bitmap (with some fiddling) such as a preview pic).

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            C Offline
            C Offline
            CyberKewl
            wrote on last edited by
            #5

            I'm aware of that. The thing is, i don't understand how to use GetCursorInfo. I just need some help in getting the cursor image which is vital for my program, that's all. Anyway, i'll try reading again.

            H 1 Reply Last reply
            0
            • C CyberKewl

              I'm aware of that. The thing is, i don't understand how to use GetCursorInfo. I just need some help in getting the cursor image which is vital for my program, that's all. Anyway, i'll try reading again.

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              If all you want is the cursor image to draw on a surface or something, the Cursor class documentation provides all the information you need - no P/Invoke is necessary. As I said, use managed code whenever possible. See Cursor.Current and Cursor.Draw():

              // Get the current cursor.
              Cursor c = Cursor.Current;
              Graphics g = this.myControl.CreateGraphics();
              c.Draw(g, this.myControl.Bounds);
              g.Dispose();

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              C 1 Reply Last reply
              0
              • H Heath Stewart

                If all you want is the cursor image to draw on a surface or something, the Cursor class documentation provides all the information you need - no P/Invoke is necessary. As I said, use managed code whenever possible. See Cursor.Current and Cursor.Draw():

                // Get the current cursor.
                Cursor c = Cursor.Current;
                Graphics g = this.myControl.CreateGraphics();
                c.Draw(g, this.myControl.Bounds);
                g.Dispose();

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                C Offline
                C Offline
                CyberKewl
                wrote on last edited by
                #7

                No, i just need to know the name of the cursor whether it is a normal cursor showing or wait cursor showing - system-wide (not just within the current form). From my understanding, if you use Cursor.Current, you can only get the name of the cursor that is showing within the form. If the cursor moves outside of the form, the name of the cursor will not be changed.

                H 1 Reply Last reply
                0
                • C CyberKewl

                  No, i just need to know the name of the cursor whether it is a normal cursor showing or wait cursor showing - system-wide (not just within the current form). From my understanding, if you use Cursor.Current, you can only get the name of the cursor that is showing within the form. If the cursor moves outside of the form, the name of the cursor will not be changed.

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  As I stated before, recreate the structure and P/Invoke the interface:

                  [StructLayout(LayoutKind.Sequential)]
                  public struct CursorInfo
                  {
                  public int Size;
                  public int Flags;
                  public IntPtr Handle;
                  public Point Position;
                  }
                  public class NativeMethods
                  {
                  [DllImport("user32.dll")]
                  public static extern bool GetCursorInfo(out CursorInfo info);
                  }

                  Based on this code and the docs for GetCursorInfo, calling it should be no problem:

                  CursorInfo info = new CursorInfo();
                  info.Size = Marshal.SizeOf(info.GetType());
                  if (NativeMethods.GetCursorInfo(out info))
                  {
                  // info.Handle contains the global cursor handle.
                  Cursor c = new Cursor(info.Handle);
                  }

                  I've tested this and it does work fine.

                  -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                  C 1 Reply Last reply
                  0
                  • H Heath Stewart

                    As I stated before, recreate the structure and P/Invoke the interface:

                    [StructLayout(LayoutKind.Sequential)]
                    public struct CursorInfo
                    {
                    public int Size;
                    public int Flags;
                    public IntPtr Handle;
                    public Point Position;
                    }
                    public class NativeMethods
                    {
                    [DllImport("user32.dll")]
                    public static extern bool GetCursorInfo(out CursorInfo info);
                    }

                    Based on this code and the docs for GetCursorInfo, calling it should be no problem:

                    CursorInfo info = new CursorInfo();
                    info.Size = Marshal.SizeOf(info.GetType());
                    if (NativeMethods.GetCursorInfo(out info))
                    {
                    // info.Handle contains the global cursor handle.
                    Cursor c = new Cursor(info.Handle);
                    }

                    I've tested this and it does work fine.

                    -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                    C Offline
                    C Offline
                    CyberKewl
                    wrote on last edited by
                    #9

                    Thanks a lot. That did the trick.

                    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