Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
<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
VB.Net Signature
<DllImport("user32.dll")> _
Private Shared Function LoadCursorFromFile(ByVal lpFileName As String) As IntPtr
End Function
If multiple cursor types are in the cursor file, this function appears to want to load the 32x32 first.
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.
Tips & Tricks:
Please add some!
Well, there is actually a way to do it!!! See VS2010 VB Sample code.
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.
private void Surface_MouseEnter(object sender, EventArgs e)
{//Assuming you have declared the function using the C# signature above, and have a control / form /etc with a MouseEnter handled by this function
//Assumes that a file (in this case a pencil cursor - PencilCursor.cur) is in Application.StartupPath (in debug mode that's app folder/bin/debug
Cursor myCursor = new Cursor(GetType(), "PencilCursor.cur");
IntPtr colorCursorHandle = LoadCursorFromFile("PencilCursor.cur");
myCursor.GetType().InvokeMember("handle",BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | System.Reflection.BindingFlags.SetField,null,myCursor,new object [] { colorCursorHandle } );
this.Cursor = myCursor;
}
Dim HighlightPointer as Cursor = ColorCursor.LoadCursor(101)
Imports System.Runtime.InteropServices
''' <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
Public Class Form1
Private Declare Function LoadCursorAPI Lib "user32.dll" Alias "LoadCursorA" (ByVal hInstance As IntPtr, ByVal lpCursorName As String) As IntPtr
<DllImport("user32.dll")> _
Private Shared Function DestroyCursor(ByVal hCursor As IntPtr) As Integer
End Function
Public Shared Function LoadCursor(ByVal embededWin32ResourceID As Integer) As Cursor
Dim cursor As Cursor = TryLoadCursor(embededWin32ResourceID)
If cursor Is Nothing Then
Throw New System.ComponentModel.Win32Exception(Err.LastDllError)
Else
Return cursor
End If
End Function
<DllImport("user32.dll")> _
Private Shared Function LoadCursorFromFile(ByVal lpFileName As String) As IntPtr
End Function
Public Shared Function TryLoadCursor(ByVal embededWin32ResourceID 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, embededWin32ResourceID)
cPtr = LoadCursorAPI(hInstance, "#" & embededWin32ResourceID)
If cPtr = IntPtr.Zero Then
Return Nothing
Else
Return New Cursor(cPtr)
End If
End Function
Dim mhAniCursor As IntPtr
End Class
Private Sub Form_Closed(ByVal sender As System.Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed
'Animierten bzw. farbigen Cursor entladen
Dim iResult As Integer = DestroyCursor(mhAniCursor)
End Sub
How to Embed Multiple Icons and Color/Animated Cursors in VS2010 VB project as Native Win32 Resources:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mhAniCursor = LoadCursorFromFile("c:\FarbCursor.cur")
If Not mhAniCursor.Equals(IntPtr.Zero) Then
Me.Cursor = New Cursor(mhAniCursor)
End If
End Sub
End Class
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. Note: even .NET 4.0 does not support this natively, it can only load black and while cursors.
Method 1 (simple, but requires repeating following steps after every build):
Alternative Managed API:
Do you know one? Please contribute it!
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:
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!
The LoadCursorFromFile API
3/16/2007 8:28:05 AM - Conipto-24.4.218.70
The LoadCursorFromFile API
5/14/2008 3:38:35 AM - CodeMajster-209.252.71.161
Defines some of the default system cursors.
5/15/2017 4:40:59 AM - -94.229.131.27
The LoadCursorFromFile API
3/16/2007 8:28:05 AM - Conipto-24.4.218.70
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Defines some of the default system cursors.
5/15/2017 4:40:59 AM - -94.229.131.27
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).