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.
setcursor (user32)
.
Summary
C# Signature:
[DllImport("user32.dll")]
static extern IntPtr LoadCursorFromFile(string lpFileName);
[DllImport("user32.dll")]
static extern IntPtr SetCursor(IntPtr hCursor);
[DllImport("user32.dll")]
static extern bool SetSystemCursor(IntPtr hcur, uint id);
private const uint OCR_NORMAL = 32512;
static Cursor ColoredCursor;
VB Signature:
<DllImport("user32.dll")> _
Public Shared Function SetCursor(ByVal hCursor As IntPtr) As IntPtr
End Function
User-Defined Types:
None.
Notes:
None.
Tips & Tricks:
Go Here For Even More!! Look at the second post.
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/9ea0bf74-760f-4f40-b64c-0cf7b0a56939/
Sample Code:
//==========C# Only
//========SET WINDOWS CURSOR========================================
IntPtr cursor = LoadCursorFromFile("example.cur");
bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);
//========SET WINDOWS CURSOR========================================
//========SET FORM CURSOR========================================
IntPtr cursor = LoadCursorFromFile("example.cur");
ColoredCursor = new Cursor(cursor);
this.Cursor = ColoredCursor;
//========SET FORM CURSOR========================================
//========SET FORM CURSOR FROM IMAGE========================================
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
//========SET FORM CURSOR FROM IMAGE========================================
//========CONVERT System.Windows.Forms.Cursor TO System.Windows.Input.Cursor FOR WPF ========
using System.Drawing;
using Microsoft.Win32.SafeHandles;
public static Cursor CursorFromImage() // this returns a System.Windows.Input.Cursor
{
Bitmap image = (Bitmap)Bitmap.FromFile(@"c:\cursorImage.bmp");
IntPtr ptr = image.GetHicon();
System.Windows.Forms.Cursor c = new System.Windows.Forms.Cursor(ptr);
SafeFileHandle handle = new SafeFileHandle(c.Handle, false);
return yourCursor = System.Windows.Interop.CursorInteropHelper.Create(handle);
}
//===========================================================================================
//========USE AN IMAGE AS A RESOURCE IN WPF FROM AN "IMAGES" FOLDER (casts a PNG file to a Bitmap) =========================
using System.Drawing;
using Microsoft.Win32.SafeHandles;
using System.Windows.Resources;
public static Cursor CursorFromImage() // this returns a System.Windows.Input.Cursor
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri("pack://application:,,,/Images/cursorImage.png"));
Bitmap image = (Bitmap)Bitmap.FromStream(sri.Stream);
IntPtr ptr = image.GetHicon();
System.Windows.Forms.Cursor c = new System.Windows.Forms.Cursor(ptr);
SafeFileHandle handle = new SafeFileHandle(c.Handle, false);
return yourCursor = System.Windows.Interop.CursorInteropHelper.Create(handle);
}
//=============================================================================================================================
Apply a custom cursor using the WPF examples like this:
this.Cursor = CursorFromImage(); // whole page
txtMyTextBox.Cursor = CursorFromImage(); // individual controls on mouse over
Alternative Managed API:
System.Windows.Forms.Cursor.Current
Documentation
The SetCursor API
2/28/2019 6:23:58 PM - -203.62.211.6
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).