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 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 和 EndUpdate 方法隱藏,而且會在建立和繪製 TreeNode 物件時 TreeView 顯示等候 Cursor 。 此範例會要求您在應用程式目錄中有名為 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 屬性。 若要在控制項的界限內變更滑鼠指標所顯示的游標,請將 指派 Cursor 給 Cursor 控制項的 屬性。 或者,您可以將 指派 Cursor 給 Current 屬性,在應用層級顯示資料指標。 例如,如果應用程式的目的是要編輯文字檔,您可以將 屬性設定 Current 為 Cursors.WaitCursor ,在檔案載入或儲存時顯示應用程式上的等候游標,以防止處理任何滑鼠事件。 當程式完成時,請將 Current 屬性 Cursors.Default 設定為 ,讓應用程式在每個控制項類型上顯示適當的資料指標。
注意
如果您在重設屬性回到 Cursors.Default 游標之前呼叫 Application.DoEvents ,應用程式會繼續接聽滑鼠事件,並繼續顯示應用程式中每個控制項的適當 Cursor 。 Current
資料指標物件可以從數個來源建立,例如現有 Cursor 、標準 Cursor 檔案、資源或資料流程的控制碼。
注意
類別 Cursor 不支援動畫資料指標 (.ani 檔案) 或具有黑白色彩的資料指標。
如果您使用做為游標的影像太小,您可以使用 DrawStretched 方法來強制影像填滿游標的界限。 您可以呼叫 Hide 方法來暫時隱藏資料指標,並藉由呼叫 Show 方法來還原資料指標。
從 .NET Framework 4.5.2 開始,當app.config檔案包含下列專案時, Cursor 將會根據系統 DPI 設定來調整大小:
<appSettings>
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>
建構函式
Cursor(IntPtr) |
從指定的視窗控制代碼,初始化 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) |
序列化物件。 |