Click here to Skip to main content
15,891,567 members
Articles / Desktop Programming / Windows Forms

How to Embed Multiple Icons and Color/Animated Cursors in .NET Executable or DLL Library as Native Win32 Resources using VS2010

Rate me:
Please Sign up or sign in to vote.
4.80/5 (5 votes)
4 Apr 2012CPOL2 min read 30.3K   285   10  
A detailed tutorial on how to embed multiple icons and color/animated cursors in VS2010 VB project assembly as native win32 resources.
Public Class Form1

   ''' <summary>
   '''   Provides ability to load color and/or animated cursor for the form or control. 
   '''   Note: cursor must be embedded as native win32 resource in the assembly.
   ''' </summary>
   Private Class ColorCursor

      Private Declare Function LoadCursorAPI Lib "user32.dll" Alias "LoadCursorA" (ByVal hInstance As IntPtr, ByVal lpCursorName As String) As IntPtr

      Public Shared Function LoadCursor(ByVal embeddedWin32ResourceID As Integer) As Cursor
         Dim cursor As Cursor = TryLoadCursor(embeddedWin32ResourceID)
         If cursor Is Nothing Then
            Throw New System.ComponentModel.Win32Exception(Err.LastDllError)
         Else
            Return cursor
         End If
      End Function

      Public Shared Function TryLoadCursor(ByVal embeddedWin32ResourceID As Integer) As Cursor
         Dim hInstance As IntPtr = System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0))
         Dim cPtr As IntPtr = LoadCursorAPI(hInstance, "#" & embeddedWin32ResourceID)
         If cPtr = IntPtr.Zero Then
            Return Nothing
         Else
            Return New Cursor(cPtr)
         End If
      End Function

   End Class

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Label1.Cursor = ColorCursor.LoadCursor(104)
   End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions