Cursor Cursor Cursor Cursor Class

定義

表示用來繪製滑鼠指標的影像。Represents the image used to paint the mouse pointer.

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
type Cursor = class
    interface IDisposable
    interface ISerializable
Public NotInheritable Class Cursor
Implements IDisposable, ISerializable
繼承
CursorCursorCursorCursor
屬性
實作

範例

下列程式碼範例會顯示一個表單,示範如何使用自訂游標。The following code example displays a form that demonstrates using a custom cursor. 自訂Cursor內嵌在應用程式的資源檔。The custom Cursor is embedded in the application's resource file. 這個範例需要名為游標檔案中包含的資料指標MyCursor.curThe example requires a cursor contained in a cursor file named MyCursor.cur. 若要編譯此範例中使用命令列,包括下列旗標: /res:MyCursor.Cur, CustomCursor.MyCursor.CurTo 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");  
           
        }
    }
}
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"
         
        ' 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 'New       
   End Class 'Form1
End Namespace 'CustomCursor

下列程式碼範例會顯示客戶資訊TreeView控制項。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. 在此範例中,15 筆訂單會顯示 1,000 個客戶。In this example, 1,000 customers are displayed with 15 orders each. 重新繪製的TreeView使用隱藏BeginUpdateEndUpdate方法,並等候Cursor會顯示時TreeView建立和繪製TreeNode物件。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. 此範例中,您需要名為的游標檔MyWait.cur應用程式目錄中。This example requires that you have a cursor file named MyWait.cur in the application directory. 它也需要Customer物件,可保存集合Order物件,以及您已建立的執行個體TreeView控制FormIt 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.
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 'End Customer class


Public Class Order
   Inherits [Object]
   Private ordID As String

   Public Sub New(ByVal orderid As String)
      Me.ordID = orderid
   End Sub 'New

   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 ' End Order 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 'FillMyTreeView

備註

資料指標是在螢幕上的位置會受到指標裝置,例如滑鼠、 畫筆或軌跡球的小型圖片。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. 例如,當編輯,或選取文字,Cursors.IBeam通常會顯示資料指標。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. 您可能必須等候使用者的處理序的範例會開啟檔案,儲存檔案,或填滿控制項這類DataGridListBoxTreeView大量資料。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.

所有控制項衍生自Control類別具有Cursor屬性。All controls that derive from the Control class have a Cursor property. 若要變更控制項的界限內時,滑鼠指標所顯示的游標,指派CursorCursor控制項的屬性。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. 或者,將指派應用程式層級顯示資料指標CursorCurrent屬性。Alternatively, you can display cursors at the application level by assigning a Cursor to the Current property. 例如,如果您的應用程式的目的是要編輯的文字檔案,您可能會設定Current屬性設Cursors.WaitCursor檔案載入或儲存以防止任何滑鼠事件處理時,透過應用程式顯示等待游標。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. 此程序完成時,請將Current屬性設Cursors.Default來顯示適當的資料指標,透過每個控制項類型的應用程式。When the process is complete, set the Current property to Cursors.Default for the application to display the appropriate cursor over each control type.

注意

如果您呼叫Application.DoEvents重設之前Current屬性將會回到Cursors.Default游標,應用程式會繼續接聽滑鼠事件,且將會恢復顯示適當Cursor應用程式中每個控制項。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,一種標準Cursor檔案、 資源或資料流。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.

注意

Cursor類別不支援動畫效果的資料指標 (.ani 檔案) 或使用黑白色彩的資料指標。The Cursor class does not support animated cursors (.ani files) or cursors with colors other than black and white.

如果您使用資料指標的影像太小,您可以使用DrawStretched方法,以強制影像來填滿資料指標的範圍。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. 您可以藉由呼叫暫時隱藏游標Hide方法,並將它還原藉由呼叫Show方法。You can temporarily hide the cursor by calling the Hide method, and restore it by calling the Show method.

從.NET Framework 4.5.2 開始Cursor會根據系統 DPI 設定時的 app.config 檔案包含下列項目調整大小:Starting with the .NET Framework 4.5.2, the Cursor will be resized based on the system DPI setting when the app.config file contains the following entry:

<appSettings>  
  <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />  
</appSettings>  

建構函式

Cursor(IntPtr) Cursor(IntPtr) Cursor(IntPtr) Cursor(IntPtr)

從指定的視窗控制代碼,初始化 Cursor 類別的新執行個體。Initializes a new instance of the Cursor class from the specified Windows handle.

Cursor(Stream) Cursor(Stream) Cursor(Stream) Cursor(Stream)

從指定的資料流,初始化 Cursor 類別的新執行個體。Initializes a new instance of the Cursor class from the specified data stream.

Cursor(String) Cursor(String) Cursor(String) Cursor(String)

從指定的檔案,初始化 Cursor 類別的新執行個體。Initializes a new instance of the Cursor class from the specified file.

Cursor(Type, String) Cursor(Type, String) Cursor(Type, String) Cursor(Type, String)

從指定的資源,以指定的資源類型,初始化 Cursor 類別的新執行個體。Initializes a new instance of the Cursor class from the specified resource with the specified resource type.

屬性

Clip Clip Clip Clip

取得或設定表示游標裁剪方框的範圍。Gets or sets the bounds that represent the clipping rectangle for the cursor.

Current Current Current Current

取得或設定表示滑鼠游標的游標物件。Gets or sets a cursor object that represents the mouse cursor.

Handle Handle Handle Handle

取得游標的控制代碼。Gets the handle of the cursor.

HotSpot HotSpot HotSpot HotSpot

取得游標作用點。Gets the cursor hot spot.

Position Position Position Position

取得或設定游標的位置。Gets or sets the cursor's position.

Size Size Size Size

取得游標物件的大小。Gets the size of the cursor object.

Tag Tag Tag Tag

取得或設定物件,它包含 Cursor 相關資料。Gets or sets the object that contains data about the Cursor.

方法

CopyHandle() CopyHandle() CopyHandle() CopyHandle()

複製這個 Cursor 的控制代碼。Copies the handle of this Cursor.

Dispose() Dispose() Dispose() Dispose()

釋放 Cursor 所使用的所有資源。Releases all resources used by the Cursor.

Draw(Graphics, Rectangle) Draw(Graphics, Rectangle) Draw(Graphics, Rectangle) Draw(Graphics, Rectangle)

在指定的表層和指定的範圍內繪製游標。Draws the cursor on the specified surface, within the specified bounds.

DrawStretched(Graphics, Rectangle) DrawStretched(Graphics, Rectangle) DrawStretched(Graphics, Rectangle) DrawStretched(Graphics, Rectangle)

在指定的表層和指定的範圍內,使用延伸格式繪製游標。Draws the cursor in a stretched format on the specified surface, within the specified bounds.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,指出這個游標是否等於指定的 CursorReturns a value indicating whether this cursor is equal to the specified Cursor.

Finalize() Finalize() Finalize() Finalize()

在記憶體回收開始前,允許物件嘗試釋放資源,並執行其他清除作業。Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

擷取目前 Cursor 的雜湊碼。Retrieves the hash code for the current Cursor.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
Hide() Hide() Hide() Hide()

隱藏游標。Hides the cursor.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Show() Show() Show() Show()

顯示游標。Displays the cursor.

ToString() ToString() ToString() ToString()

擷取表示這個 Cursor 的人類看得懂的字串。Retrieves a human readable string representing this Cursor.

運算子

Equality(Cursor, Cursor) Equality(Cursor, Cursor) Equality(Cursor, Cursor) Equality(Cursor, Cursor)

傳回值,指出 Cursor 類別的兩個執行個體是否相等。Returns a value indicating whether two instances of the Cursor class are equal.

Inequality(Cursor, Cursor) Inequality(Cursor, Cursor) Inequality(Cursor, Cursor) Inequality(Cursor, Cursor)

傳回值,指出 Cursor 類別的兩個執行個體是否不相等。Returns a value indicating whether two instances of the Cursor class are not equal.

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

序列化物件。Serializes the object.

適用於

另請參閱