This document is archived and information here might be outdated.  Recommended version.


IMouseCursor.SetCursor Method (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > ArcObjects namespaces > Framework > ESRI.ArcGIS.Framework > Interfaces > IM > IMouseCursor Interface > IMouseCursor.SetCursor Method
ArcGIS Developer Help

IMouseCursor.SetCursor Method

Sets the application's cursor to cursor id or picture object specified by cursorID. The cursor is automatically reset when the MouseCursor instance is released.

[Visual Basic .NET]
Public Sub SetCursor ( _
    ByVal cursorID As Object _
)
[C#]
public void SetCursor (
    object cursorID
);

Product Availability

Available with ArcGIS Desktop.

Description

 

The values for cursorID are:

Remarks

You can use one of the built-in cursors, or you can set the cursor to a custom cursor. You can use either cursor files (.cur) or icon files (.ico). The cursor is automatically reset when the MouseCursor instance is released. The instance is released when the calling procedure ends, when the variable that references the mouse cursor is set to nothing, or when something, such as the display of a message box, overrides Windows messaging.

The following example uses the built-in wait cursor. The cursor is automatically reset when this Sub procedure ends. You would get m_app from the hook in ICommand::OnCreate().

[C#]

The following example uses the built-in wait cursor.

void WaitCursor(IApplication app)
{
    IMouseCursor appCursor = new MouseCursorClass();
    appCursor.SetCursor(2);
    for (int i = 0; i <= 10000; i++)
        app.StatusBar.set_Message(0, i.ToString());
}

To set a custom cursor, you can get a System.Drawing.Icon object by loading a .ico or a .cur file from its file path or one of your embedded resources.

The following sample loads an icon (.ico) file directlly:

void LoadCustomCursor(IApplication app)
{
    System.Drawing.Icon myIcon = new System.Drawing.Icon(@"C:\ArcGIS\Bin\Icons\style.ico");
    IMouseCursor appCursor = new MouseCursorClass();
    object curIcon = ESRI.ArcGIS.Utility.COMSupport.OLE.GetIPictureDispFromIcon(myIcon);
    appCursor.SetCursor(curIcon);
    for (int i = 0; i <= 10000; i++)
        app.StatusBar.set_Message(0, i.ToString());
    myIcon.Dispose();
}

The following sample loads a cursor (.cur) file first and converts it to an icon:

void LoadCustomCursor(IApplication app)
{
    System.Windows.Forms.Cursor cur = System.Windows.Forms.Cursors.SizeNESW;
    //cur = new System.Windows.Forms.Cursor(@"C:\icons\myCursor.cur");
    System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(cur.Handle);
    
    IMouseCursor appCursor = new MouseCursorClass();
    object curIcon = ESRI.ArcGIS.Utility.COMSupport.OLE.GetIPictureDispFromIcon(myIcon);
    appCursor.SetCursor(curIcon);
    for (int i = 0; i <= 10000; i++)
        app.StatusBar.set_Message(0, i.ToString());
    cur.Dispose();
    myIcon.Dispose();
}
[Visual Basic .NET]

The following example uses the built-in wait cursor.

Public Sub WaitCursor(app As IApplication)
    Dim appCursor As IMouseCursor = New MouseCursorClass
    appCursor.SetCursor(2)
    For i As Integer = 0 To 10000
        app.StatusBar.set_Message(0, i.ToString())
    Next
End Sub

To set a custom cursor, you can get a System.Drawing.Icon object by loading a .ico or a .cur file from its file path or one of your embedded resources.

The following sample loads an icon (.ico) file directlly:

Sub LoadCustomCursor(app As IApplication)
    Dim myIcon As System.Drawing.Icon = New System.Drawing.Icon("C:\ArcGIS\Bin\Icons\style.ico")
    Dim appCursor As IMouseCursor = New MouseCursorClass
    Dim curIcon As System.Object = ESRI.ArcGIS.Utility.COMSupport.OLE.GetIPictureDispFromIcon(myIcon)
    appCursor.SetCursor(curIcon)
    For i As Integer = 0 To 10000 
        app.StatusBar.set_Message(0, i.ToString())
    Next
    myIcon.Dispose()
End Sub

The following sample loads a cursor (.cur) file first and converts it to an icon:

Sub LoadCustomCursor(app As IApplication)
    Dim cur As System.Windows.Forms.Cursor = System.Windows.Forms.Cursors.SizeNESW
    //cur = New System.Windows.Forms.Cursor("C:\icons\myCursor.cur")
    Dim myIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(cur.Handle)
    
    Dim appCursor As IMouseCursor = New MouseCursorClass
    Dim curIcon As System.Object = ESRI.ArcGIS.Utility.COMSupport.OLE.GetIPictureDispFromIcon(myIcon)
    appCursor.SetCursor(curIcon)
    For i As Integer = 0 To 10000 
        app.StatusBar.set_Message(0, i.ToString())
    Next
    cur.Dispose()
    myIcon.Dispose()
End Sub

See Also

IMouseCursor Interface