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

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
SetCursor 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