Cursor 类

代表用于绘制鼠标指针的图像。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<SerializableAttribute> _
Public NotInheritable Class Cursor
    Implements IDisposable, ISerializable
用法
Dim instance As Cursor
[SerializableAttribute] 
public sealed class Cursor : IDisposable, ISerializable
[SerializableAttribute] 
public ref class Cursor sealed : IDisposable, ISerializable
/** @attribute SerializableAttribute() */ 
public final class Cursor implements IDisposable, ISerializable
SerializableAttribute 
public final class Cursor implements IDisposable, ISerializable

备注

光标是一个小图片,它在屏幕上的位置由指针设备(如鼠标、钢笔或轨迹球)控制。在用户移动指针设备时,操作系统相应地移动光标。

不同的光标形状用于通知用户鼠标将有什么样的操作。例如,当编辑或选择文本时,通常显示 Cursors.IBeam 光标。等待光标通常用于通知用户当前有一个进程正在运行。可能让用户等待的进程的示例有打开文件、保存文件或者用大量数据填充 DataGridListBoxTreeView 控件。

Control 类派生的所有控件均有 Cursor 属性。若要更改由鼠标指针(当其在控件的边界内时)显示的光标,请将一个 Cursor 分配给控件的 Cursor 属性。或者可以在应用程序级别显示光标,方法是向 Current 属性分配一个 Cursor。例如,如果应用程序的目的是编辑文本文件,则可以将 Current 属性设置为 Cursors.WaitCursor,以在加载或保存文件时在应用程序上显示等待光标,从而防止处理任何鼠标事件。当该进程完成时,请将 Current 属性设置为 Cursors.Default,以便应用程序在每种控件类型上显示适当的光标。

提示

如果在将 Current 属性重置回 Cursors.Default 光标之前调用 Application.DoEvents,应用程序将重新开始侦听鼠标事件并恢复显示应用程序中每个控件的相应 Cursor

可以从多个源(如现有 Cursor 的句柄、标准 Cursor 文件、资源或数据流)创建光标对象。

提示

Cursor 类不支持动画光标(.ani 文件),也不支持彩色(而非黑白色光标)。

如果用作光标的图像太小,则可以使用 DrawStretched 方法来强制该图像填充到光标的边界。可以通过调用 Hide 方法来暂时隐藏光标,然后通过调用 Show 方法来还原光标。

示例

下面的代码示例显示一个窗体,该窗体演示自定义光标的用法。自定义 Cursor 嵌入在应用程序的资源文件中。该示例要求在名为 MyCursor.cur 的光标文件中包含有一个光标。要使用命令行编译该示例,请包括以下标志:/res:MyCursor.Cur, CustomCursor.MyCursor.Cur

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
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");  
           
        }
    }
}
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace CustomCursor
{
   public ref class Form1: public System::Windows::Forms::Form
   {
   public:
      Form1()
      {
         this->ClientSize = 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 case sensitive.
         this->Cursor = gcnew System::Windows::Forms::Cursor( GetType(),"MyCursor.Cur" );
      }

   };

}


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

import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;

public class Form1 extends System.Windows.Forms.Form
{
    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main

    public Form1()
    {
        this.set_ClientSize(new System.Drawing.Size(292, 266));
        this.set_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.set_Cursor(new Cursor(GetType(), "MyCursor.Cur"));
    } //Form1 
} //Form1

下面的代码示例在 TreeView 控件中显示客户信息。根树节点显示客户名称,子树节点显示分配给每个客户的订单编号。在本示例中,显示 1,000 个客户,每个客户有 15 个订单。使用 BeginUpdate 方法和 EndUpdate 方法可取消重新绘制 TreeView,在 TreeView 创建和绘制 TreeNode 对象时,显示等待 Cursor。此示例要求应用程序目录中包含有名为 MyWait.cur 的光标文件。它还要求有一个可容纳 Order 对象集合的 Customer 对象,并要求已在 Form 上创建了 TreeView 控件的实例。

' 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
// 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();
}
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.
   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();
}
// 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"
            + ((Int32)x).ToString()));
    }
    // Add orders to each Customer object in the ArrayList.
    for (int iCtr = 0; iCtr < customerArray.get_Count(); iCtr++) {
        Customer customer1 = (Customer)customerArray.get_Item(iCtr);
        for (int y = 0; y < 15; y++) {
            customer1.get_CustomerOrders().Add(new Order("Order"
                + ((Int32)y).ToString()));
        }
    }
    // Display a wait cursor while the TreeNodes are being created.
    get_Cursor().set_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.get_Nodes().Clear();
    // Add a root TreeNode for each Customer object in the ArrayList.
    for (int iCtr1 = 0; iCtr1 < customerArray.get_Count(); iCtr1++) {
        Customer customer2 = (Customer)customerArray.get_Item(iCtr1);
        treeView1.get_Nodes().Add(new TreeNode(customer2.get_CustomerName()));
        // Add a child treenode for each Order object in the current
        // Customer object.
        for (int iCtr2 = 0; iCtr2 < customer2.get_CustomerOrders().
            get_Count(); iCtr2++) {
            Order order1 = (Order)customer2.get_CustomerOrders().
                get_Item(iCtr2);
            treeView1.get_Nodes().
                get_Item(customerArray.IndexOf(customer2)).get_Nodes().
                Add(new TreeNode(customer2.get_CustomerName() + "."
                + order1.get_OrderID()));
        }
    }
    // Reset the cursor to the default for all controls.
    get_Cursor().set_Current(Cursors.get_Default());
    // Begin repainting the TreeView.
    treeView1.EndUpdate();
} //FillMyTreeView

继承层次结构

System.Object
  System.Windows.Forms.Cursor

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Cursor 成员
System.Windows.Forms 命名空间
Cursors