Export (0) Print
Expand All

Cursor Class

Represents the image used to paint the mouse pointer.

For a list of all members of this type, see Cursor Members.

System.Object
   System.Windows.Forms.Cursor

[Visual Basic]
<Serializable>
NotInheritable Public Class Cursor
   Implements IDisposable, ISerializable
[C#]
[Serializable]
public sealed class Cursor : IDisposable, ISerializable
[C++]
[Serializable]
public __gc __sealed class Cursor : public IDisposable,
   ISerializable
[JScript]
public
   Serializable
class Cursor implements IDisposable, ISerializable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

A cursor is a small picture whose location on the screen is controlled by a pointing device, such as a mouse, pen, or trackball. When the user moves the pointing device, the operating system moves the cursor accordingly.

Different cursor shapes are used to inform the user what operation the mouse will have. For example, when editing or selecting text, a Cursors.IBeam cursor is typically displayed. A wait cursor is commonly used to inform the user that a process is currently running. Examples of processes you might have the user wait for are opening a file, saving a file, or filling a control such as a DataGrid, ListBox or TreeView with a large amount of data.

All controls that derive from the Control class have a Cursor property. To change the cursor displayed by the mouse pointer when it is within the bounds of the control, assign a Cursor to the Cursor property of the control. Alternatively, you can display cursors at the application level by assigning a Cursor to the Current property. For example, if the purpose of your application is to edit a text file, you might set the Current property to Cursors.WaitCursor to display a wait cursor over the application while the file loads or saves to prevent any mouse events from being processed. When the process is complete, set the Current property to Cursors.Default for the application to display the appropriate cursor over each control type.

Note   If you call Application.DoEvents before resetting the Current property back to the Cursors.Default cursor, the application will resume listening for mouse events and will resume displaying the appropriate Cursor for each control in the application.

Cursor objects can be created from several sources, such as the handle of an existing Cursor, a standard Cursor file, a resource, or a data stream.

Note   The Cursor class does not support animated cursors (.ani files) or cursors with colors other than black and white.

If the image you are using as a cursor is too small, you can use the DrawStretched method to force the image to fill the bounds of the cursor. You can temporarily hide the cursor by calling the Hide method, and restore it by calling the Show method.

Example

[Visual Basic, C#, C++] The following example displays a form that demonstrates using a custom cursor by using the System.Windows.Forms.Cursor.#ctor constructor. The custom Cursor is embedded in the applications resource file. The example assumes that you have a cursor contained in a cursor file named MyCursor.cur. To compile this example using the command line, include the following flag: /res:MyCursor.Cur, CustomCursor.MyCursor.Cur

[Visual Basic] 
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace CustomCursor
   
   Public Class Form1
      Inherits System.Windows.Forms.Form
      
      <System.STAThread()> _
      Public Shared Sub Main()
         System.Windows.Forms.Application.Run(New Form1())
      End Sub 'Main

      Public Sub New()

         Me.ClientSize = New System.Drawing.Size(292, 266)
         Me.Text = "Cursor Example"
         
         ' Looks namespace.MyCursor.cur in the assemblies manifest.
         
         ' The following generates a cursor from an embedded resource.
         ' To add a custom cursor, create or use an existing 16x16 bitmap
         '        1. Add a new cursor file to your project: 
         '                File->Add New Item->Local Project Items->Cursor File
         '        2. Select 16x16 image type:
         '                Image->Current Icon Image Types->16x16
         ' --- To make the custom cursor an embedded resource  ---
         ' In Visual Studio:
         '        1. Select the cursor file in the Solution Explorer
         '        2. Choose View->Properties.
         '        3. In the properties window switch "Build Action" to "Embedded"
         ' On the command line:
         '        Add the following flag:
         '            /res:CursorFileName.Cur,Namespace.CursorFileName.Cur
         '
         ' The following line uses the namespace from the passed-in type
         ' and looks for CustomCursor.MyCursor.Cur in the assemblies manifest.
     ' NOTE: The cursor name is acase sensitive.        
     
         Me.Cursor = New Cursor(Me.GetType(), "MyCursor.Cur") 
      End Sub 'New       
   End Class 'Form1
End Namespace 'CustomCursor

[C#] 
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CustomCursor
{
    public class Form1 : System.Windows.Forms.Form
    {
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        public Form1()
        {
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Text = "Cursor Example";
            
            // The following generates a cursor from an embedded resource.
            
            // To add a custom cursor, create or use an existing 16x16 bitmap
            //        1. Add a new cursor file to your project: 
            //                File->Add New Item->Local Project Items->Cursor File
            //        2. Select 16x16 image type:
            //                Image->Current Icon Image Types->16x16

            // --- To make the custom cursor an embedded resource  ---
            
            // In Visual Studio:
            //        1. Select the cursor file in the Solution Explorer
            //        2. Choose View->Properties.
            //        3. In the properties window switch "Build Action" to "Embedded"

            // On the command line:
            //        Add the following flag:
            //            /res:CursorFileName.Cur,Namespace.CursorFileName.Cur
            //        
            //        Where "Namespace" is the namespace in which you want to use the cursor
            //        and   "CursorFileName.Cur" is the cursor filename.

            // The following line uses the namespace from the passed-in type
            // and looks for CustomCursor.MyCursor.Cur in the assemblies manifest.
        // NOTE: The cursor name is acase sensitive.
            this.Cursor = new Cursor(GetType(), "MyCursor.Cur");  
           
        }
    }
}

[C++] 

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace CustomCursor {
public __gc class Form1 : public System::Windows::Forms::Form {

public:
    Form1() {
        this->ClientSize = System::Drawing::Size(292, 266);
        this->Text = S"Cursor Example";

        // The following generates a cursor from an embedded resource.

        // To add a custom cursor, create or use an existing 16x16 bitmap
        //        1. Add a new cursor file to your project:
        //                File->Add New Item->Local Project Items->Cursor File
        //        2. Select 16x16 image type:
        //                Image->Current Icon Image Types->16x16

        // --- To make the custom cursor an embedded resource  ---

        // In Visual Studio:
        //        1. Select the cursor file in the Solution Explorer
        //        2. Choose View->Properties.
        //        3. In the properties window switch "Build Action" to "Embedded"

        // On the command line:
        //        Add the following flag:
        //            /res:CursorFileName.Cur, Namespace.CursorFileName.Cur
        //
        //        Where "Namespace" is the namespace in which you want to use the cursor
        //        and   "CursorFileName.Cur" is the cursor filename.

        // The following line uses the namespace from the passed-in type
        // and looks for CustomCursor.MyCursor.Cur in the assemblies manifest.
        // NOTE: The cursor name is case sensitive.
        this->Cursor = new System::Windows::Forms::Cursor(GetType(), S"MyCursor.Cur");

    }
};
}
[STAThread]
int main() {
    Application::Run(new CustomCursor::Form1());
}

[Visual Basic, C#, C++] The following example displays customer information in a TreeView control. The root tree nodes display customer names, and the child tree nodes display the order numbers assigned to each customer. In this example, 1,000 customers are displayed with 15 orders each. The repainting of the TreeView is suppressed by using the BeginUpdate and EndUpdate methods, and a wait Cursor is displayed while the TreeView creates and paints the TreeNode objects. This example assumes you have a cursor file named MyWait.cur in the application directory. It also assumes you have a Customer object that can hold a collection of Order objects, and that you have created an instance of a TreeView control on a Form.

[Visual Basic] 
' Create a new ArrayList to hold the Customer objects.
Private customerArray As New ArrayList()

Private Sub FillMyTreeView()
   ' Add customers to the ArrayList of Customer objects.
   Dim x As Integer
   For x = 0 To 999
      customerArray.Add(New Customer("Customer" + x.ToString()))
   Next x

   ' Add orders to each Customer object in the ArrayList.
   Dim customer1 As Customer
   For Each customer1 In customerArray
      Dim y As Integer
      For y = 0 To 14
         customer1.CustomerOrders.Add(New Order("Order" + y.ToString()))
      Next y
   Next customer1

   ' Display a wait cursor while the TreeNodes are being created.
   Cursor.Current = New Cursor("MyWait.cur")

   ' Suppress repainting the TreeView until all the objects have been created.
   treeView1.BeginUpdate()

   ' Clear the TreeView each time the method is called.
   treeView1.Nodes.Clear()

   ' Add a root TreeNode for each Customer object in the ArrayList.
   Dim customer2 As Customer
   For Each customer2 In customerArray
      treeView1.Nodes.Add(New TreeNode(customer2.CustomerName))

      ' Add a child TreeNode for each Order object in the current Customer object.
      Dim order1 As Order
      For Each order1 In customer2.CustomerOrders
         treeView1.Nodes(customerArray.IndexOf(customer2)).Nodes.Add( _
    New TreeNode(customer2.CustomerName + "." + order1.OrderID))
      Next order1
   Next customer2

   ' Reset the cursor to the default for all controls.
   Cursor.Current = System.Windows.Forms.Cursors.Default

   ' Begin repainting the TreeView.
   treeView1.EndUpdate()
End Sub 'FillMyTreeView

[C#] 
// Create a new ArrayList to hold the Customer objects.
private ArrayList customerArray = new ArrayList(); 

private void FillMyTreeView()
{
   // Add customers to the ArrayList of Customer objects.
   for(int x=0; x<1000; x++)
   {
      customerArray.Add(new Customer("Customer" + x.ToString()));
   }

   // Add orders to each Customer object in the ArrayList.
   foreach(Customer customer1 in customerArray)
   {
      for(int y=0; y<15; y++)
      {
         customer1.CustomerOrders.Add(new Order("Order" + y.ToString()));    
      }
   }

   // Display a wait cursor while the TreeNodes are being created.
   Cursor.Current = new Cursor("MyWait.cur");
        
   // Suppress repainting the TreeView until all the objects have been created.
   treeView1.BeginUpdate();

   // Clear the TreeView each time the method is called.
   treeView1.Nodes.Clear();

   // Add a root TreeNode for each Customer object in the ArrayList.
   foreach(Customer customer2 in customerArray)
   {
      treeView1.Nodes.Add(new TreeNode(customer2.CustomerName));
          
      // Add a child treenode for each Order object in the current Customer object.
      foreach(Order order1 in customer2.CustomerOrders)
      {
         treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
           new TreeNode(customer2.CustomerName + "." + order1.OrderID));
      }
   }

   // Reset the cursor to the default for all controls.
   Cursor.Current = Cursors.Default;

   // Begin repainting the TreeView.
   treeView1.EndUpdate();
}

[C++] 
void FillMyTreeView() {
    // Add customers to the ArrayList of Customer objects.
    for (int x=0; x<1000; x++) {
        customerArray->Add(new Customer(String::Concat(S"Customer ", __box(x))));
    }

    // Add orders to each Customer object in the ArrayList.
    IEnumerator* myEnum = customerArray->GetEnumerator();
    while (myEnum->MoveNext()) {
        Customer* customer1 = __try_cast<Customer*>(myEnum->Current);

        for (int y=0; y<15; y++) {
            customer1->CustomerOrders->Add(new Order(String::Concat(S"Order ", __box(y))));
        }
    }

    // Display a wait cursor while the TreeNodes are being created.
    Cursor::Current = new System::Windows::Forms::Cursor(S"MyWait.cur");

    // Suppress repainting the TreeView until all the objects have been created.
    treeView1->BeginUpdate();

    // Clear the TreeView each time the method is called.
    treeView1->Nodes->Clear();

    // Add a root TreeNode for each Customer object in the ArrayList.
    while (myEnum->MoveNext()) {
        Customer* customer2 = __try_cast<Customer*>(myEnum->Current);

        treeView1->Nodes->Add(new TreeNode(customer2->CustomerName));

        // Add a child treenode for each Order object in the current Customer object.
        IEnumerator* myEnum = customer2->CustomerOrders->GetEnumerator();
        while (myEnum->MoveNext()) {
            Order* order1 = __try_cast<Order*>(myEnum->Current);

            treeView1->Nodes->Item[customerArray->IndexOf(customer2)]->Nodes->Add(
                new TreeNode(String::Concat(customer2->CustomerName, S".", order1->OrderID)));
        }
    }

    // Reset the cursor to the default for all controls.
    Cursor::Current = Cursors::Default;

    // Begin repainting the TreeView.
    treeView1->EndUpdate();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

Cursor Members | System.Windows.Forms Namespace | Cursors

Show:
© 2014 Microsoft