Save Cursor as Bitmap

How to save the current Windows moise pointer (cursor) as a bitmap during application run-time using VB.net or C#?
jr_barros_jrAsked:
Who is Participating?
 
liorfrCommented:
static Bitmap CaptureCursor(ref int x, ref int y)
{
   Bitmap bmp;
   IntPtr hicon;
   Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
   Win32Stuff.ICONINFO icInfo;
   ci.cbSize = Marshal.SizeOf(ci);
   if(Win32Stuff.GetCursorInfo(out ci))
   {
       if (ci.flags == Win32Stuff.CURSOR_SHOWING)
       {
           hicon = Win32Stuff.CopyIcon(ci.hCursor);
           if(Win32Stuff.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;
}
0
 
AshokCommented:
In VB.NET

Shared Function CaptureCursor(ByRef x As Integer, ByRef y As Integer) As Bitmap
      Dim bmp As Bitmap
      Dim hicon As IntPtr
      Dim ci As New Win32Stuff.CURSORINFO()
      Dim icInfo As Win32Stuff.ICONINFO
      ci.cbSize = Marshal.SizeOf(ci)
      If Win32Stuff.GetCursorInfo(ci) Then
            If ci.flags = Win32Stuff.CURSOR_SHOWING Then
                  hicon = Win32Stuff.CopyIcon(ci.hCursor)
                  If Win32Stuff.GetIconInfo(hicon, icInfo) Then
                        x = ci.ptScreenPos.x - (DirectCast(icInfo.xHotspot, Integer))
                        y = ci.ptScreenPos.y - (DirectCast(icInfo.yHotspot, Integer))
                        Dim ic As Icon = Icon.FromHandle(hicon)
                        bmp = ic.ToBitmap()

                        Return bmp
                  End If
            End If
      End If
      Return Nothing
End Function

HTH
AShok
0
 
jr_barros_jrAuthor Commented:
I am having "Error    2    Type 'Win32Stuff.CURSORINFO' is not defined."

How can i define Win32Stuff? Where is it?

Thank you,

JR

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jr_barros_jrAuthor Commented:
To avoid discussion, I would like to congratulate both for the help.

Best regards.
0
 
AshokCommented:
Here is source code for the Win32Stuff class

Class Win32Stuff

      #region Class Variables

      Public Const SM_CXSCREEN As Integer = 0
      Public Const SM_CYSCREEN As Integer = 1

      Public Const CURSOR_SHOWING As Int32 = &H1

      <StructLayout(LayoutKind.Sequential)> _
      Public Structure ICONINFO
            Public fIcon As Boolean
            ' Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
            Public xHotspot As Int32
            ' Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
            Public yHotspot As Int32
            ' Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
            Public hbmMask As IntPtr
            ' (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
            Public hbmColor As IntPtr
            ' (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
      End Structure
      <StructLayout(LayoutKind.Sequential)> _
      Public Structure POINT
            Public x As Int32
            Public y As Int32
      End Structure

      <StructLayout(LayoutKind.Sequential)> _
      Public Structure CURSORINFO
            Public cbSize As Int32
            ' Specifies the size, in bytes, of the structure.
            Public flags As Int32
            ' Specifies the cursor state. This parameter can be one of the following values:
            Public hCursor As IntPtr
            ' Handle to the cursor.
            Public ptScreenPos As POINT
            ' A POINT structure that receives the screen coordinates of the cursor.
      End Structure
#End Region


      #region Class Functions

      <DllImport("user32.dll", EntryPoint := "GetDesktopWindow")> _
      Public Shared Function GetDesktopWindow() As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "GetDC")> _
      Public Shared Function GetDC(ptr As IntPtr) As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "GetSystemMetrics")> _
      Public Shared Function GetSystemMetrics(abc As Integer) As Integer
      End Function

      <DllImport("user32.dll", EntryPoint := "GetWindowDC")> _
      Public Shared Function GetWindowDC(ptr As Int32) As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "ReleaseDC")> _
      Public Shared Function ReleaseDC(hWnd As IntPtr, hDc As IntPtr) As IntPtr
      End Function


      <DllImport("user32.dll", EntryPoint := "GetCursorInfo")> _
      Public Shared Function GetCursorInfo(ByRef pci As CURSORINFO) As Boolean
      End Function

      <DllImport("user32.dll", EntryPoint := "CopyIcon")> _
      Public Shared Function CopyIcon(hIcon As IntPtr) As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "GetIconInfo")> _
      Public Shared Function GetIconInfo(hIcon As IntPtr, ByRef piconinfo As ICONINFO) As Boolean
      End Function
#End Region
End Class


HTH
Ashok
0
 
jr_barros_jrAuthor Commented:
Thank you so much for this extra help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.