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






4.80/5 (5 votes)
A detailed tutorial on how to embed multiple icons and color/animated cursors in VS2010 VB project assembly as native win32 resources.
Introduction
This is useful if you need your EXE or DLL to provide multiple icons for documents associated with your application. It also provides a way to load color and animated cursors for your forms and/or controls with LoadCursor API. Note: even .NET 4.0 does not support this natively, it can only load black and while cursors.The Process
Method 1 (simple, but requires repeating following steps after every build):
- Open your EXE or DLL in Visual Studio (either from File->Open File or solution explorer).
- Add icons and/or cursors to the EXE or DLL.
- Save file.
Method 2 (more involved, but done only once):
Note: once this is set up Visual Studio will stop updating assembly information in the output file (file version, description, and more) taken from AssemblyInfo.vb.
- Enter assembly information first (ex. Project Properties->Application->Assembly Information).
- Build the project.
- Open produced EXE or DLL in Visual Studio (either from File->Open File or solution explorer). This opens native resource editor.
- Go to File->Save As. Pick 32-bit resource file in Save As type and save file as assemblyWin32.res. Again, resource file (not exe or dll).
- Include assemblyWin32.res in your project, and set to compile it as content in Build Action.
- Add icons and/or cursors to the assemblyWin32.res file.
- Open your project file in notepad
(*.vbProj) and add the following block:
<PropertyGroup> <Win32Resource>assemblyWin32.res</Win32Resource> </PropertyGroup>
- Build the project.
- If you need to change assembly information open resource file and update version information manually. Alternatively, you can remove section added to the project file in step 7 and repeat from step 1.
You can put it after condition block. Here
is the example:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> ... ... </PropertyGroup>
Note: IDE will detect changes and ask you to reload your project file. Just press reload.
How to Load Embedded Color Cursor
'Usage
Dim HighlightPointer As Cursor = ColorCursor.LoadCursor(101)
''' <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
Similar Articles
Embedding Multiple Icons into .NET Executables By Ed.Poore Note: Ed’s article is based on C# and VS2005. The template to create *.res file is no longer available in VS2010.History
02/22/2011, Initial Rev.
04/04/2012, Updated title and method 2 to handle assembly information.