Cursor クラス
マウス ポインタの描画に使用されるイメージを表します。
この型のすべてのメンバの一覧については、Cursor メンバ を参照してください。
System.Object
System.Windows.Forms.Cursor
<Serializable>
NotInheritable Public Class Cursor Implements IDisposable, ISerializable
[C#]
[Serializable]
public sealed class Cursor : IDisposable, ISerializable
[C++]
[Serializable]
public __gc __sealed class Cursor : public IDisposable, ISerializable
[JScript]
public
Serializable
class Cursor implements IDisposable, ISerializable
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
カーソルは、画面に表示される小さなピクチャです。画面上のカーソルの位置は、マウス、ペン、トラックボールなどのポインティング デバイスによって制御されます。ユーザーがポインティング デバイスを動かすと、その動きに応じ、オペレーティング システムによりカーソルが移動されます。
カーソルの形を変えることで、マウスの操作目的をユーザーに示します。たとえば、テキストを編集または選択するときは、通常、 Cursors.IBeam カーソルが表示されます。プロセスが現在実行中であることを示すには、通常、待機カーソルが使用されます。ユーザーを待機させる可能性のあるプロセスの例には、ファイルのオープンや保存、またはコントロール (DataGrid 、 ListBox 、または TreeView) への大量のデータの格納があります。
Control クラスから派生するすべてのコントロールには、 Cursor プロパティがあります。マウス ポインタによって表示されるカーソルをコントロールの範囲内で変更するには、 Cursor をコントロールの Cursor プロパティに割り当てます。また、 Cursor を Current プロパティに割り当て、アプリケーション レベルでカーソルを表示することもできます。たとえば、アプリケーションでテキストファイルを編集する場合は、 Current プロパティを Cursors.WaitCursor に設定し、ファイルの読み込みまたは保存中にマウス イベントが処理されないように、アプリケーション上に待機カーソルを表示します。このプロセスが完了すると、アプリケーションで各コントロールの種類に応じた適切なカーソルを表示するために、 Current プロパティを Cursors.Default に設定します。
メモ Current プロパティを Cursors.Default カーソルにリセットする前に Application.DoEvents を呼び出すと、アプリケーションがマウス イベントの待機を再開し、アプリケーションの各コントロールの適切な Cursor の表示を再開します。
カーソル オブジェクトは、既存の Cursor のハンドル、標準 Cursor ファイル、リソース、データ ストリームなど、複数のソースから作成できます。
メモ Cursor クラスでは、アニメーション カーソル (.ani ファイル)、またはカラーのカーソルはサポートされていません。
カーソルとして使用するイメージが小さすぎる場合は、 DrawStretched メソッドを使用して、イメージを強制的にカーソルの境界まで拡大できます。 Hide メソッドを呼び出してカーソルを一時的に非表示にしたり、 Show メソッドを呼び出して元に戻したりできます。
使用例
[Visual Basic, C#, C++] System.Windows.Forms.Cursor.#ctor コンストラクタを使用して、カスタム カーソルの使用方法を示すフォームを表示する例を次に示します。カスタム 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
[C#]
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");
}
}
}
[C++]
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
namespace CustomCursor {
public __gc class Form1 : public System::Windows::Forms::Form {
public:
Form1() {
this->ClientSize = System::Drawing::Size(292, 266);
this->Text = S"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 = new System::Windows::Forms::Cursor(GetType(), S"MyCursor.Cur");
}
};
}
[STAThread]
int main() {
Application::Run(new CustomCursor::Form1());
}
[Visual Basic, C#, C++] 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
[C#]
// 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();
}
[C++]
void FillMyTreeView() {
// Add customers to the ArrayList of Customer objects.
for (int x=0; x<1000; x++) {
customerArray->Add(new Customer(String::Concat(S"Customer ", __box(x))));
}
// Add orders to each Customer object in the ArrayList.
IEnumerator* myEnum = customerArray->GetEnumerator();
while (myEnum->MoveNext()) {
Customer* customer1 = __try_cast<Customer*>(myEnum->Current);
for (int y=0; y<15; y++) {
customer1->CustomerOrders->Add(new Order(String::Concat(S"Order ", __box(y))));
}
}
// Display a wait cursor while the TreeNodes are being created.
Cursor::Current = new System::Windows::Forms::Cursor(S"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 = __try_cast<Customer*>(myEnum->Current);
treeView1->Nodes->Add(new 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 = __try_cast<Order*>(myEnum->Current);
treeView1->Nodes->Item[customerArray->IndexOf(customer2)]->Nodes->Add(
new TreeNode(String::Concat(customer2->CustomerName, S".", order1->OrderID)));
}
}
// Reset the cursor to the default for all controls.
Cursor::Current = Cursors::Default;
// Begin repainting the TreeView.
treeView1->EndUpdate();
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Windows.Forms
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)