Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

LoadCursor (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

VB Signature:

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function LoadCursor( _
    ByVal hInstance As IntPtr, _
    ByVal lpCursorName As String) As IntPtr
End Function

Common VB Overloads:

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function LoadCursor( _
    ByVal hInstance As IntPtr, _
    ByVal lpCursorName As Integer) As IntPtr
End Function

User-Defined Types:

IDC_

Notes:

This function may not solve your problems when trying to load animated or colored cursors stored as embedded resources. I've wasted hours trying to get it to work to no avail. The LoadCursorFromFile method however works perfectly, so you might want to extract your cursor resource to a temp file and use the P/Invoke for that function.

Well, there is actually a way to do it!!! See VS2010 VB Sample code.

Tips & Tricks:

If you specify the hInstance as IntPtr.Zero, then you can specify one of the constants in IDC_ as the CursorName to utilize system defined cursors.

C# Sample Code:

    IntPtr handle = LoadCursor(IntPtr.Zero, IDC_Arrow);

VB Sample Code:

   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

How to Embed Multiple Icons and Color/Animated Cursors in VS2010 VB project as Native Win32 Resources:

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 custom cursors.

Method 1 (simple, but requires repeating following steps after every build):

1. Open your EXE or DLL in Visual Studio (either File->Open File or just drag and drop it into IDE).
2. Add icons and/or cursors to the EXE or DLL
3. Save file.

Method 2 (more involved, but done only once):

1. Find native win32 resource file (*.res) on the web. Google “Embedding Multiple Icons into .NET Executables” and download the sample project which contains *.res file. VS2010 does not have the template to create this file; however the editor is still there.

2. Rename file to assemblyWin32.res, include it in your project, and compile it as content in Build Action.

3. Add icons and/or cursors to the assemblyWin32.res file.

4. Open your project file in notepad (*.vbProj) and add the following block:

      <PropertyGroup>
     <Win32Resource>assemblyWin32.res</Win32Resource>
      </PropertyGroup>

    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.  

5. If you have included icons in the Win32 resource file for your EXE then remove application icon in project properties->Application Tab (this icon will no longer be used).

6. Build the project

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
LoadCursor on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions