I read this question, and found I had a similar problem:
checking-cursor-type
This comes too late to help ACS, but in case anyone else is looking for it, I found the solution by combining a few sources. First, I found a very good article on codeproject:
DesktopCaptureW ithMouse
That gave me the way of getting the system's mouse icon. Then on Dreaming In Code, I found a nice compare routine for bitmaps:
BitMap Compare
All together, I get this.
Note, you will need to add these to your project:
First you add this to your project
Next you need these functions:
Now you can call capture cursor, and compare it. BTW, it gets a little tricky if you are wanting to use this to determine when someone has entered a cell in a datagrid (for example). To be able to compare, you will first need something to compare against, and it is not a good idea to use the default windows cursor -- since the application might have created a custom cursor.
You should move the mouse pointer to an area you know will show the default cursor (ie an application window's title bar) and grab that bitmap to use as your baseline default icon.
FWIW.
checking-cursor-type
This comes too late to help ACS, but in case anyone else is looking for it, I found the solution by combining a few sources. First, I found a very good article on codeproject:
DesktopCaptureW ithMouse
That gave me the way of getting the system's mouse icon. Then on Dreaming In Code, I found a nice compare routine for bitmaps:
BitMap Compare
All together, I get this.
Note, you will need to add these to your project:
Code:
using System.Runtime.InteropServices; using System.Security.Cryptography;
Code:
public const Int32 CURSOR_SHOWING = 0x00000001;
[StructLayout(LayoutKind.Sequential)]
public struct ICONINFO
{
public bool fIcon; // Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
public Int32 xHotspot; // Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
public Int32 yHotspot; // Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
public IntPtr hbmMask; // (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
public IntPtr hbmColor; // (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
}
[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO
{
public Int32 cbSize; // Specifies the size, in bytes, of the structure.
public Int32 flags; // Specifies the cursor state. This parameter can be one of the following values:
public IntPtr hCursor; // Handle to the cursor.
public POINT ptScreenPos; // A POINT structure that receives the screen coordinates of the cursor.
}
[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
[DllImport("user32.dll", EntryPoint = "CopyIcon")]
public static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll", EntryPoint = "GetIconInfo")]
public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
Code:
public static Bitmap CaptureCursor()
{
Bitmap bmp;
IntPtr hicon;
CURSORINFO ci = new CURSORINFO();
ICONINFO icInfo;
ci.cbSize = Marshal.SizeOf(ci);
if (GetCursorInfo(out ci))
{
if (ci.flags == CURSOR_SHOWING)
{
hicon = CopyIcon(ci.hCursor);
if (GetIconInfo(hicon, out icInfo))
{
// x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
// y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);
Icon ic = Icon.FromHandle(hicon);
bmp = ic.ToBitmap();
return bmp;
}
}
}
return null;
}
/*************************************************************************
* method for comparing 2 cursor bitmaps to see if they are the same.
* (actually, this method works to compare any two botmaps) First
* we convert both images to a byte array, we then get their hash (their
* hash should match if the images are the same), we then loop through
* each item in the hash comparing with the 2nd Bitmap
**************************************************************************/
public static bool CompareCursorImages(ref Bitmap bmp1, ref Bitmap bmp2)
{
try
{
//create instance or System.Drawing.ImageConverter to convert
//each image to a byte array
ImageConverter converter = new ImageConverter();
//create 2 byte arrays, one for each image
byte[] imgBytes1 = new byte[1];
byte[] imgBytes2 = new byte[1];
//convert images to byte array
imgBytes1 = (byte[])converter.ConvertTo(bmp1, imgBytes2.GetType());
imgBytes2 = (byte[])converter.ConvertTo(bmp2, imgBytes1.GetType());
//now compute a hash for each image from the byte arrays
SHA256Managed sha = new SHA256Managed();
byte[] imgHash1 = sha.ComputeHash(imgBytes1);
byte[] imgHash2 = sha.ComputeHash(imgBytes2);
//now let's compare the hashes
for (int i = 0; i < imgHash1.Length && i < imgHash2.Length; i++)
{
//whoops, found a non-match, exit the loop
//with a false value
if (!(imgHash1[i] == imgHash2[i]))
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
//we made it this far so the images must match
return true;
}
}
You should move the mouse pointer to an area you know will show the default cursor (ie an application window's title bar) and grab that bitmap to use as your baseline default icon.
FWIW.