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
Summary

C# Signature:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetCursorPos(int x, int y);
    [DllImport("user32.dll")]
    static extern IntPtr LoadCursorFromFile(string lpFileName);

VB.Net Signature:

Declare Auto Function SetCursorPos Lib "user32.dll" (ByVal x As Integer, ByVal y As Integer) As Integer

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetCursorPos(ByVal X As Integer, ByVal Y As Integer) As Boolean
End Function

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    [DllImport("user32.dll")]
    static extern bool ClientToScreen(IntPtr hwnd, ref Point lpPoint);
    static extern IntPtr SetCursor(IntPtr hCursor);

    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int x, int y);
    static extern bool SetSystemCursor(IntPtr hcur, uint id);

    public void UnmanagedMoveCursorOverButton(Button button)
    {
        IntPtr handle = IntPtr.Zero;
        Point point;
        int x = 0;
        int y = 0;
        int width = 0;
        int height = 0;
        bool coordinatesFound = false;
    private const uint OCR_NORMAL = 32512;

    static Cursor ColoredCursor;

        if (button != null)
        {
        width = button.Size.Width;
        height = button.Size.Height;
        handle = button.Handle;
        }

        if ((width > 0) && (height > 0))
        {
        point = new Point();
        x = (width / 2);
        y = (height / 2);

VB Signature:

<DllImport("user32.dll")> _
Public Shared Function SetCursor(ByVal hCursor As IntPtr) As IntPtr
End Function

User-Defined Types:

None.

        coordinatesFound = ClientToScreen(handle, ref point);

Notes:

None.

        if (coordinatesFound == true)
        {
            SetCursorPos(point.X + x, point.Y + y);
        }
        }
    }

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/

    public void ManagedMoveCursorOverButton(Button button)
    {
        IntPtr handle = IntPtr.Zero;
        Point point;
        int x = 0;
        int y = 0;
        int width = 0;
        int height = 0;
        bool coordinatesFound = false;

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

        if (button != null)
        {
        width = button.Size.Width;
        height = button.Size.Height;
        handle = button.Handle;
        }

//========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========================================

        if ((width > 0) && (height > 0))
        {
        point = new Point();
        x = (width / 2);
        y = (height / 2);

//========CONVERT System.Windows.Forms.Cursor TO System.Windows.Input.Cursor FOR WPF ========

        using System.Drawing;
        using Microsoft.Win32.SafeHandles;

        coordinatesFound = ClientToScreen(handle, ref point);

        if (coordinatesFound == true)
        {
          point.X += x;
          point.Y += y;
          System.Windows.Forms.Cursor.Position = point;
        }
        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);
        }
    }

//===========================================================================================

Alternative Managed API:

System.Windows.Forms.Cursor.Position

//========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;

Documentation

        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
Edit This Page
Find References
Show Printable Version
Revisions