Cursor 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
代表用于绘制鼠标指针的图像。
public ref class Cursor sealed : IDisposable, System::Runtime::Serialization::ISerializable[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.CursorConverter))]
[System.Serializable]
public sealed class Cursor : IDisposable, System.Runtime.Serialization.ISerializable[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.CursorConverter))]
public sealed class Cursor : IDisposable, System.Runtime.Serialization.ISerializable[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.CursorConverter))>]
[<System.Serializable>]
type Cursor = class
    interface IDisposable
    interface ISerializable[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.CursorConverter))>]
type Cursor = class
    interface IDisposable
    interface ISerializablePublic NotInheritable Class Cursor
Implements IDisposable, ISerializable- 继承
- 
				Cursor
- 属性
- 实现
示例
下面的代码示例显示一个窗体,该窗体演示如何使用自定义游标。 自定义 Cursor 嵌入到应用程序的资源文件中。 该示例需要一 MyCursor.cur个名为 的游标文件中包含的游标。 若要使用命令行编译此示例,请包含以下标志: /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");  
        }
    }
}
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
      Public Sub New()
         Me.ClientSize = New System.Drawing.Size(292, 266)
         Me.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.
        Me.Cursor = New Cursor(Me.GetType(), "MyCursor.cur")
      End Sub
   End Class
End Namespace 'CustomCursor
下面的代码示例在 控件中 TreeView 显示客户信息。 根树节点显示客户名称,子树节点显示分配给每个客户的订单号。 在此示例中,将显示 1,000 个客户,每个客户有 15 个订单。 使用 和 方法抑制 的TreeView重新粉刷,并在 创建和绘制 TreeNode 对象时TreeView显示等待Cursor。EndUpdateBeginUpdate 此示例要求在应用程序目录中有一个名为 MyWait.cur 的游标文件。 它还需要一个Customer对象,该对象可以保存对象的集合Order,并且你已在 上Form创建了控件的TreeView实例。
// The basic Customer class.
ref class Customer: public System::Object
{
private:
   String^ custName;
protected:
   ArrayList^ custOrders;
public:
   Customer( String^ customername )
   {
      custName = "";
      custOrders = gcnew ArrayList;
      this->custName = customername;
   }
   property String^ CustomerName 
   {
      String^ get()
      {
         return this->custName;
      }
      void set( String^ value )
      {
         this->custName = value;
      }
   }
   property ArrayList^ CustomerOrders 
   {
      ArrayList^ get()
      {
         return this->custOrders;
      }
   }
};
// End Customer class
// The basic customer Order class.
ref class Order: public System::Object
{
private:
   String^ ordID;
public:
   Order( String^ orderid )
   {
      ordID = "";
      this->ordID = orderid;
   }
   property String^ OrderID 
   {
      String^ get()
      {
         return this->ordID;
      }
      void set( String^ value )
      {
         this->ordID = value;
      }
   }
};
// End Order class
void FillMyTreeView()
{
   // Add customers to the ArrayList of Customer objects.
   for ( int x = 0; x < 1000; x++ )
   {
      customerArray->Add( gcnew Customer( "Customer " + x ) );
   }
   
   // Add orders to each Customer object in the ArrayList.
   IEnumerator^ myEnum = customerArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Customer^ customer1 = safe_cast<Customer^>(myEnum->Current);
      for ( int y = 0; y < 15; y++ )
      {
         customer1->CustomerOrders->Add( gcnew Order( "Order " + y ) );
      }
   }
   // Display a wait cursor while the TreeNodes are being created.
   ::Cursor::Current = gcnew System::Windows::Forms::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.
   myEnum = customerArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Customer^ customer2 = safe_cast<Customer^>(myEnum->Current);
      treeView1->Nodes->Add( gcnew 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 = safe_cast<Order^>(myEnum->Current);
         treeView1->Nodes[ customerArray->IndexOf( customer2 ) ]->Nodes->Add( gcnew TreeNode( customer2->CustomerName + "." + order1->OrderID ) );
      }
   }
   
   // Reset the cursor to the default for all controls.
   ::Cursor::Current = Cursors::Default;
   
   // Begin repainting the TreeView.
   treeView1->EndUpdate();
}
// 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();
}
Public Class Customer
   Inherits [Object]
   Private custName As String = ""
   Friend custOrders As New ArrayList()
   Public Sub New(ByVal customername As String)
      Me.custName = customername
   End Sub
   Public Property CustomerName() As String
      Get
         Return Me.custName
      End Get
      Set(ByVal Value As String)
         Me.custName = Value
      End Set
   End Property
   Public ReadOnly Property CustomerOrders() As ArrayList
      Get
         Return Me.custOrders
      End Get
   End Property
End Class
Public Class Order
   Inherits [Object]
   Private ordID As String
   Public Sub New(ByVal orderid As String)
      Me.ordID = orderid
   End Sub
   Public Property OrderID() As String
      Get
         Return Me.ordID
      End Get
      Set(ByVal Value As String)
         Me.ordID = Value
      End Set
   End Property
End Class
' 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
注解
光标是一张小图片,其在屏幕上的位置由指针设备(如鼠标、笔或轨迹球)控制。 当用户移动指向设备时,操作系统会相应地移动光标。
使用不同的光标形状来通知用户鼠标将执行哪些操作。 例如,在编辑或选择文本时, Cursors.IBeam 通常会显示光标。 等待游标通常用于通知用户进程当前正在运行。 用户可能等待的过程示例包括打开文件、保存文件、填充控件(如 DataGrid) ListBox 或 TreeView 大量数据。
派生自 Control 类的所有控件都具有 Cursor 属性。 若要更改鼠标指针在控件边界内显示的光标,请将 分配给CursorCursor控件的 属性。 或者,可以通过将 分配给 CursorCurrent 属性来在应用程序级别显示游标。 例如,如果应用程序的目的是编辑文本文件,则可以将 属性设置为 CurrentCursors.WaitCursor ,以便在加载或保存文件时在应用程序上显示等待光标,以防止处理任何鼠标事件。 该过程完成后,将 Current 属性设置为 , Cursors.Default 以便应用程序在每个控件类型上显示相应的光标。
注意
如果在将属性重置Current回Cursors.Default游标之前调用 Application.DoEvents ,应用程序将恢复侦听鼠标事件,并恢复显示应用程序中每个控件的相应Cursor值。
游标对象可以从多个源创建,例如现有 Cursor、标准 Cursor 文件、资源或数据流的句柄。
注意
类 Cursor 不支持动画光标 (.ani 文件) 或具有非黑白颜色的游标。
如果用作光标的图像太小,则可以使用 DrawStretched 方法强制图像填充光标的边界。 可以通过调用 Hide 方法暂时隐藏游标,并通过调用 Show 方法还原游标。
从 .NET Framework 4.5.2 开始,Cursor当 app.config 文件包含以下条目时,将根据系统 DPI 设置调整大小:
<appSettings>  
  <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />  
</appSettings>  
构造函数
| Cursor(IntPtr) | 从指定的 Windows 句柄初始化 Cursor 类的新实例。 | 
| Cursor(Stream) | 从指定的数据流初始化 Cursor 类的新实例。 | 
| Cursor(String) | 从指定的文件初始化 Cursor 类的新实例。 | 
| Cursor(Type, String) | 从具有指定资源类型的指定资源初始化 Cursor 类的新实例。 | 
属性
| Clip | 获取或设置表示光标的剪辑矩形的边界。 | 
| Current | 获取或设置代表鼠标光标的光标对象。 | 
| Handle | 获取光标句柄。 | 
| HotSpot | 获取光标作用点。 | 
| Position | 获取或设置光标位置。 | 
| Size | 获取光标对象的大小。 | 
| Tag | 获取或设置包含有关 Cursor 的数据的对象。 | 
方法
| CopyHandle() | 复制该 Cursor 的句柄。 | 
| Dispose() | 释放由 Cursor 使用的所有资源。 | 
| Draw(Graphics, Rectangle) | 在指定边界内、指定的表面上绘制光标。 | 
| DrawStretched(Graphics, Rectangle) | 在指定边界内、指定的表面上以拉伸格式绘制光标。 | 
| Equals(Object) | 返回一个值,该值指示此光标是否等于指定的 Cursor。 | 
| Finalize() | 在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。 | 
| GetHashCode() | 检索当前 Cursor 的哈希代码。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| Hide() | 隐藏光标。 | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Show() | 显示光标。 | 
| ToString() | 检索表示此 Cursor 的可读字符串。 | 
运算符
| Equality(Cursor, Cursor) | 返回一个值,该值指示 Cursor 类的两个实例是否相等。 | 
| Inequality(Cursor, Cursor) | 返回一个值,该值指示 Cursor 类的两个实例是否不相等。 | 
显式接口实现
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) | 序列化对象。 |