Cursors Class
Provides a collection of Cursor objects for use by a Windows Forms application.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Some of the Cursor objects in this class can take on a different appearance than those described. The user can change the cursor appearance by adjusting the mouse pointer settings in their operating system. The panning and no move cursors are static and cannot be changed by the operating system.
The panning and no move cursors are used during mouse wheel operations. Depending on the direction the window can be scrolled, the cursor changes to the appropriate no move cursor when the mouse wheel is clicked. The cursor then changes to the appropriate panning cursor as the mouse is moved.
The following example demonstrates changing the mouse cursor using the Control.Cursor property, the Cursor class, and the Cursors class. The example creates a form that contains a ComboBox control, a Panel control, and a ListView control. The ComboBox contains all cursors provided by the Cursors class. When the user selects a mouse cursor in the ComboBox, the Control.Cursor property is set to the selected cursor, which updates the cursor for the Panel. The ListView is updated every time the Control.CursorChanged event occurs.
Imports System Imports System.Drawing Imports System.Windows.Forms Namespace MCursor ' Summary description for Form1. Public NotInheritable Class Form1 Inherits System.Windows.Forms.Form Friend WithEvents cursorSelectionComboBox As System.Windows.Forms.ComboBox Friend WithEvents testPanel As System.Windows.Forms.Panel Private label1 As System.Windows.Forms.Label Private label2 As System.Windows.Forms.Label Private cursorEventViewer As System.Windows.Forms.ListView Private label3 As System.Windows.Forms.Label <System.STAThread()> _ Public Shared Sub Main() System.Windows.Forms.Application.Run(New Form1) End Sub 'Main Public Sub New() Me.cursorSelectionComboBox = New System.Windows.Forms.ComboBox Me.testPanel = New System.Windows.Forms.Panel Me.label1 = New System.Windows.Forms.Label Me.label2 = New System.Windows.Forms.Label Me.cursorEventViewer = New System.Windows.Forms.ListView Me.label3 = New System.Windows.Forms.Label ' Select Cursor Label Me.label2.Location = New System.Drawing.Point(24, 16) Me.label2.Size = New System.Drawing.Size(80, 16) Me.label2.Text = "Select cursor:" ' ' Cursor Testing Panel Label Me.label1.Location = New System.Drawing.Point(24, 80) Me.label1.Size = New System.Drawing.Size(144, 23) Me.label1.Text = "Cursor testing panel:" ' Cursor Changed Events Label Me.label3.Location = New System.Drawing.Point(184, 16) Me.label3.Size = New System.Drawing.Size(128, 16) Me.label3.Text = "Cursor changed events:" ' Cursor Selection ComboBox Me.cursorSelectionComboBox.Location = New System.Drawing.Point(24, 40) Me.cursorSelectionComboBox.Size = New System.Drawing.Size(152, 21) Me.cursorSelectionComboBox.TabIndex = 0 ' Cursor Test Panel Me.testPanel.BackColor = System.Drawing.SystemColors.ControlDark Me.testPanel.Location = New System.Drawing.Point(24, 104) Me.testPanel.Size = New System.Drawing.Size(152, 160) ' Cursor Event ListView Me.cursorEventViewer.Location = New System.Drawing.Point(184, 40) Me.cursorEventViewer.Size = New System.Drawing.Size(256, 224) Me.cursorEventViewer.TabIndex = 4 Me.cursorEventViewer.View = System.Windows.Forms.View.List ' Set up how the form should be displayed and add the controls to the form. Me.ClientSize = New System.Drawing.Size(456, 286) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.label3, _ Me.cursorEventViewer, Me.label2, Me.label1, _ Me.testPanel, Me.cursorSelectionComboBox}) Me.Text = "Cursors Example" ' Add all the cursor types to the combobox. Dim cursor As Cursor For Each cursor In CursorList() cursorSelectionComboBox.Items.Add(cursor) Next cursor End Sub 'New Private Function CursorList() As Cursor() ' Make an array of all the types of cursors in Windows Forms. return New Cursor() {Cursors.AppStarting, Cursors.Arrow, Cursors.Cross, _ Cursors.Default, Cursors.Hand, Cursors.Help, _ Cursors.HSplit, Cursors.IBeam, Cursors.No, _ Cursors.NoMove2D, Cursors.NoMoveHoriz, Cursors.NoMoveVert, _ Cursors.PanEast, Cursors.PanNE, Cursors.PanNorth, _ Cursors.PanNW, Cursors.PanSE, Cursors.PanSouth, _ Cursors.PanSW, Cursors.PanWest, Cursors.SizeAll, _ Cursors.SizeNESW, Cursors.SizeNS, Cursors.SizeNWSE, _ Cursors.SizeWE, Cursors.UpArrow, Cursors.VSplit, Cursors.WaitCursor} End Function Private Sub cursorSelectionComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cursorSelectionComboBox.SelectedIndexChanged ' Set the cursor in the test panel to be the selected cursor style. testPanel.Cursor = CType(cursorSelectionComboBox.SelectedItem, Cursor) End Sub Private Sub testPanel_CursorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles testPanel.CursorChanged ' Build up a string containing the type of object sending the event, and the event. Dim cursorEvent As String = String.Format("[{0}]: {1}", sender.GetType().ToString(), "Cursor changed") ' Records this event in the list view. Me.cursorEventViewer.Items.Add(cursorEvent) End Sub End Class 'Form1 End Namespace 'MCursor
The following example draws the specified cursor on the form in its normal size, and in stretched mode, twice its size. This example assumes that you have a Form and a Cursor object to pass into the method when it is called.
Private Sub DrawCursorsOnForm(cursor As Cursor) ' If the form's cursor is not the Hand cursor and the ' Current cursor is the Default, Draw the specified ' cursor on the form in normal size and twice normal size. If (Not Me.Cursor.Equals(Cursors.Hand)) And _ Cursor.Current.Equals(Cursors.Default) Then ' Draw the cursor stretched. Dim graphics As Graphics = Me.CreateGraphics() Dim rectangle As New Rectangle(New Point(10, 10), _ New Size(cursor.Size.Width * 2, cursor.Size.Height * 2)) cursor.DrawStretched(graphics, rectangle) ' Draw the cursor in normal size. rectangle.Location = New Point(rectangle.Width + _ rectangle.Location.X, rectangle.Height + rectangle.Location.Y) rectangle.Size = cursor.Size cursor.Draw(graphics, rectangle) ' Dispose of the cursor. cursor.Dispose() End If End Sub
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.