共用方式為


Cursor 類別

定義

表示用來繪製滑鼠指標的影像。

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 ISerializable
Public 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 會被使用 BeginUpdate and EndUpdate 方法抑制,並顯示等待 Cursor 畫面,等待物件 TreeView 的建立與上色 TreeNode 。 這個範例需要你在應用程式目錄中有一個游標檔案。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 標。 等待游標通常用來通知使用者程序正在執行。 你可能讓使用者等待的流程範例包括開啟檔案、儲存檔案,或填入控制項(如 DataGridListBoxTreeView 是大量資料的輸入)。

所有源自該 Control 類別的控制項都有屬性 Cursor 。 若要更改滑鼠指標在控制項範圍內顯示的游標,請將 a Cursor 指派給 Cursor 控制項的屬性。 或者,你也可以在應用程式層級將游標顯示,方法是將 a Cursor 指派給 Current 屬性。 例如,如果您的應用程式目的是編輯文字檔,您可以設定 Current 屬性在 Cursors.WaitCursor 檔案載入或儲存時顯示等待游標,以防止任何滑鼠事件被處理。 程序完成後,將屬性設定 Current 為 , Cursors.Default 讓應用程式在每個控制類型上顯示相應游標。

備註

如果你在將屬性重置CurrentCursors.Default游標前呼叫Application.DoEvents,應用程式會恢復監聽滑鼠事件,並重新顯示應用程式中每個控制項的適當Cursor值。

游標物件可以從多個來源建立,例如現有 Cursor的 handle、標準 Cursor 檔案、資源或資料串流。

備註

Cursor 類別不支援動畫游標(.ani檔案)或非黑白色彩的游標。

如果你用作游標的圖片太小,你可以用這個 DrawStretched 方法強制圖像填滿游標的範圍。 你可以暫時透過呼叫 Hide 方法隱藏游標,然後透過呼叫 Show 方法來還原游標。

從 .NET Framework 4.5.2 開始, Cursor 當 app.config 檔案包含以下條目時,會根據系統 DPI 設定進行調整大小:

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

建構函式

名稱 Description
Cursor(IntPtr)

從指定的 Windows 句柄初始化該 Cursor 類別的新實例。

Cursor(Stream)

從指定的資料流中初始化該類別的新實例 Cursor

Cursor(String)

從指定檔案初始化該類別的新實例 Cursor

Cursor(Type, String)

初始化該類別的新實例 Cursor ,來自指定資源的資源類型。

屬性

名稱 Description
Clip

取得或設定代表游標裁切矩形的界限。

Current

取得或設定一個代表滑鼠游標的游標物件。

Handle

抓住游標的把手。

HotSpot

會觸發游標熱點。

Position

取得或設定游標的位置。

Size

會取得游標物件的大小。

Tag

取得或設定包含關於 的資料 Cursor的物件。

方法

名稱 Description
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

操作員

名稱 Description
Equality(Cursor, Cursor)

回傳一個值,表示該類別的兩個實例 Cursor 是否相等。

Inequality(Cursor, Cursor)

回傳一個值,表示該類別的兩個實例 Cursor 是否不相等。

明確介面實作

名稱 Description
ISerializable.GetObjectData(SerializationInfo, StreamingContext)

序列化物件。

適用於

另請參閱