Cursor Třída

Definice

Představuje obrázek použitý k malování ukazatele myši.

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
Dědičnost
Cursor
Atributy
Implementuje

Příklady

Následující příklad kódu zobrazí formulář, který ukazuje použití vlastního kurzoru. Vlastní Cursor je vložený do souboru prostředků aplikace. Příklad vyžaduje kurzor obsažený v souboru kurzoru s názvem MyCursor.cur. Pokud chcete tento příklad zkompilovat pomocí příkazového řádku, uveďte následující příznak: /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

Následující příklad kódu zobrazuje informace o zákazníci v ovládacím TreeView prvku. Uzly kořenového stromu zobrazují jména zákazníků a podřízené uzly stromu zobrazují čísla objednávek přiřazená každému zákazníkovi. V tomto příkladu se zobrazuje 1 000 zákazníků s 15 objednávkami. Překreslení TreeView je potlačeno pomocí metod BeginUpdate a EndUpdate metod a čekání Cursor se zobrazí při TreeView vytváření a malování TreeNode objektů. Tento příklad vyžaduje, abyste měli v adresáři aplikace soubor MyWait.cur kurzoru. Vyžaduje také Customer objekt, který může obsahovat kolekci Order objektů a vytvořili jste instanci TreeView ovládacího prvku na 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


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

Poznámky

Kurzor je malý obrázek, jehož umístění na obrazovce je řízeno bodovacím zařízením, jako je myš, pero nebo trackball. Když uživatel přesune bodovací zařízení, operační systém přesune kurzor odpovídajícím způsobem.

Různé obrazce kurzoru slouží k informování uživatele o tom, jakou operaci bude mít myš. Například při úpravách nebo výběru textu Cursors.IBeam se obvykle zobrazí kurzor. Kurzor čekání se běžně používá k informování uživatele, že proces je aktuálně spuštěný. Příklady procesů, na které může uživatel čekat, jsou otevření souboru, uložení souboru nebo vyplnění ovládacího prvku, jako DataGridListBox TreeView je nebo s velkým množstvím dat.

Všechny ovládací prvky odvozené z Control třídy mají Cursor vlastnost. Chcete-li změnit kurzor zobrazený ukazatelem myši, když je v mezích ovládacího prvku, přiřaďte Cursor vlastnost Cursor ovládacího prvku. Případně můžete zobrazit kurzory na úrovni aplikace přiřazením Cursor vlastnosti Current . Pokud je například účelem aplikace upravit textový soubor, můžete vlastnost nastavit Current tak, aby Cursors.WaitCursor zobrazovala kurzor čekání na aplikaci, zatímco se soubor načte nebo uloží, aby se zabránilo zpracování událostí myši. Po dokončení procesu nastavte Current vlastnost na Cursors.Default aplikaci tak, aby zobrazovala odpovídající kurzor na každý typ ovládacího prvku.

Poznámka

Pokud zavoláte Application.DoEvents před resetováním Current vlastnosti zpět na Cursors.Default kurzor, aplikace obnoví naslouchání událostem myši a obnoví zobrazení odpovídající Cursor pro každý ovládací prvek v aplikaci.

Objekty kurzoru lze vytvořit z několika zdrojů, jako je například popisovač existujícího Cursorsouboru, standardního Cursor souboru, prostředku nebo datového streamu.

Poznámka

Třída Cursor nepodporuje animované kurzory (soubory.ani) ani kurzory s jinými barvami než černobíle.

Pokud je obrázek, který používáte jako kurzor, příliš malý, můžete pomocí DrawStretched metody vynutit, aby obrázek vyplnil hranice kurzoru. Kurzor můžete dočasně skrýt zavoláním Hide metody a jeho obnovením voláním Show metody.

Počínaje .NET Framework 4.5.2 Cursor se velikost změní na základě nastavení DPI systému, pokud soubor app.config obsahuje následující položku:

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

Konstruktory

Cursor(IntPtr)

Inicializuje novou instanci Cursor třídy ze zadaného Windows popisovač.

Cursor(Stream)

Inicializuje novou instanci Cursor třídy ze zadaného datového proudu.

Cursor(String)

Inicializuje novou instanci Cursor třídy ze zadaného souboru.

Cursor(Type, String)

Inicializuje novou instanci třídy ze zadaného Cursor prostředku se zadaným typem prostředku.

Vlastnosti

Clip

Získá nebo nastaví hranice, které představují obdélník výřezu kurzoru.

Current

Získá nebo nastaví kurzor objekt, který představuje kurzor myši.

Handle

Získá úchyt kurzoru.

HotSpot

Získá kurzor horké místo.

Position

Získá nebo nastaví pozici kurzoru.

Size

Získá velikost objektu kurzoru.

Tag

Získá nebo nastaví objekt, který obsahuje data o Cursor.

Metody

CopyHandle()

Zkopíruje popisovač tohoto Cursorsouboru .

Dispose()

Uvolní všechny prostředky používané nástrojem Cursor.

Draw(Graphics, Rectangle)

Nakreslí kurzor na zadanou plochu v rámci zadaných hranic.

DrawStretched(Graphics, Rectangle)

Nakreslí kurzor v roztaženém formátu na zadanou plochu v rámci zadaných hranic.

Equals(Object)

Vrátí hodnotu označující, zda je tento kurzor roven zadanému Cursor.

Finalize()

Umožňuje objektu pokusit se uvolnit prostředky a provést další operace čištění před uvolněním uvolňování paměti.

GetHashCode()

Načte kód hash pro aktuální Cursor.

GetType()

Type Získá aktuální instanci.

(Zděděno od Object)
Hide()

Skryje kurzor.

MemberwiseClone()

Vytvoří použádnou kopii aktuálního souboru Object.

(Zděděno od Object)
Show()

Zobrazí kurzor.

ToString()

Načte čitelný řetězec představující tento Cursorřetězec .

Operátory

Equality(Cursor, Cursor)

Vrátí hodnotu označující, zda jsou dvě instance Cursor třídy stejné.

Inequality(Cursor, Cursor)

Vrátí hodnotu označující, zda se dvě instance Cursor třídy nerovnají.

Explicitní implementace rozhraní

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Serializuje objekt.

Platí pro

Viz také