Export (0) Print
Expand All

Cursor Class

Represents the image used to paint the mouse pointer.

System.Object
  System.Windows.Forms.Cursor

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

[SerializableAttribute]
[TypeConverterAttribute(typeof(CursorConverter))]
public sealed class Cursor : IDisposable, 
	ISerializable

The Cursor type exposes the following members.

  NameDescription
Public methodCursor(IntPtr)Initializes a new instance of the Cursor class from the specified Windows handle.
Public methodCursor(Stream)Initializes a new instance of the Cursor class from the specified data stream.
Public methodCursor(String)Initializes a new instance of the Cursor class from the specified file.
Public methodCursor(Type, String)Initializes a new instance of the Cursor class from the specified resource with the specified resource type.
Top

  NameDescription
Public propertyStatic memberClipGets or sets the bounds that represent the clipping rectangle for the cursor.
Public propertyStatic memberCurrentGets or sets a cursor object that represents the mouse cursor.
Public propertyHandleGets the handle of the cursor.
Public propertyHotSpotGets the cursor hot spot.
Public propertyStatic memberPositionGets or sets the cursor's position.
Public propertySizeGets the size of the cursor object.
Public propertyTagGets or sets the object that contains data about the Cursor.
Top

  NameDescription
Public methodCopyHandleCopies the handle of this Cursor.
Public methodDisposeReleases all resources used by the Cursor.
Public methodDrawDraws the cursor on the specified surface, within the specified bounds.
Public methodDrawStretchedDraws the cursor in a stretched format on the specified surface, within the specified bounds.
Public methodEqualsReturns a value indicating whether this cursor is equal to the specified Cursor. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeRetrieves the hash code for the current Cursor. (Overrides Object.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberHideHides the cursor.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberShowDisplays the cursor.
Public methodToStringRetrieves a human readable string representing this Cursor. (Overrides Object.ToString().)
Top

  NameDescription
Public operatorStatic memberEqualityReturns a value indicating whether two instances of the Cursor class are equal.
Public operatorStatic memberInequalityReturns a value indicating whether two instances of the Cursor class are not equal.
Top

  NameDescription
Explicit interface implemetationPrivate methodISerializable.GetObjectDataInfrastructure. Serializes the object.
Top

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.

NoteNote

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.

NoteNote

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.

The following code example displays a form that demonstrates using a custom cursor. The custom Cursor is embedded in the application's resource file. The example requires 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


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 a bitmap
            //        1. Add a new cursor file to your project: 
            //                Project->Add New Item->General->Cursor File

            // --- 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 Resources"

            // 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");  

        }
    }
}


The following code 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 requires that you have a cursor file named MyWait.cur in the application directory. It also requires 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.



// The basic Customer class.
public class Customer : System.Object
{
   private string custName = "";
   protected ArrayList custOrders = new ArrayList();

   public Customer(string customername)
   {
      this.custName = customername;
   }

   public string CustomerName
   {      
      get{return this.custName;}
      set{this.custName = value;}
   }

   public ArrayList CustomerOrders 
   {
      get{return this.custOrders;}
   }

} // End Customer class 


// The basic customer Order class.
public class Order : System.Object
{
   private string ordID = "";

   public Order(string orderid)
   {
      this.ordID = orderid;
   }

   public string OrderID
   {      
      get{return this.ordID;}
      set{this.ordID = value;}
   }

} // End Order class

// 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();
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Community Additions

ADD
Show:
© 2014 Microsoft