GetCursorInfo in C#?
-
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.
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-----
-
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-----
-
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.
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 theCursor
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-----
-
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 theCursor
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-----
-
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.
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. SeeCursor.Current
andCursor.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-----
-
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. SeeCursor.Current
andCursor.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-----
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.
-
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.
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-----
-
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-----