Home

Cursors

 

A Cursor

 

Introduction

A cursor is a small picture that represents the position of the mouse on a Windows screen. Because Microsoft Windows is a graphic-oriented operating system, when it installs, it creates a set of standard or regularly used icons. These can be seen by opening the Control Panel window and double-clicking the Mouse icon. This opens the Mouse Properties dialog box where you can click the Pointers tab to see a list of standard cursors installed by Windows:

   
Mouse Properties

Microsoft Visual Studio 2005 provides a rich collection of cursors you can easily use in your application. You can apply them to any control as you wish.

To support cursors, the .NET Framework provides two classes. One of the classes used for cursors is called Cursors. This sealed class mostly contains a list of available cursors as properties. Another technique consists of using a cursor not listed in the Properties window. A cursor is based on the Cursor class. Both the Cursors and the Cursor classes are defined in the System.Windows.Forms namespace that is part of the System.Windows.Forms.dll library.

Practical Learning Practical Learning: Introducing Cursors

  1. From the Common Controls section of the Toolbox, click the ListBox control ListBox and click the upper-left section of the form
  2. From the Containers section of the Toolbox, click the Panel control Panel and click the upper-right section of the form
  3. Once again, from the Common Controls section of the Toolbox, click the TreeView control TreeView and click the lower-left section of the form
  4. From the Common Controls section of the Toolbox, click the RichTextBox control RichTextBox and click the lower-right section of the form
     
    Cursor

Creating a Cursor

A cursor is primarily represented in Microsoft Windows as a (resource) file. This means that, to get a cursor, you can design one. Fortunately, Microsoft Windows and the .NET Framework provides many cursors ready to be used. Otherwise, you can create and design your own.

To create a cursor, you can use use Microsoft Visual Studio. To start, on the main menu, you can click Project -> Add New Item... In the Templates list, you can click Cursor File, give it a name, and click Add.

A cursor is a Windows file that has the extension .cur.

Practical Learning Practical Learning: Creating a Cursor

  1. On the main menu, you can click Project -> Add New Item...
  2. In the Templates list, click Cursor File
  3. Change the Filename to push and click Add
  4. On the main menu, click File -> Save push.cur As...
  5. Locate and display the bin sub-folder of your project
  6. Double-click Debug to select that folder
  7. Click Save
  8. On the Image Editor toolbar, click the Line tool Line. In the Colors window, make sure the black color is selected
  9. Draw a vertical line from the pixel on the 6th column and 2nd row from top
  10. Draw a diagonal line at 45˚ from the top border of the new line to the lower-right until the line is at 5 pixels from the right border of the drawing area
     
    Cursor Design
  11. Draw a horizontal line from the lower border of the dialog line to half-left
  12. Draw a diagonal line from the lower border of the vertical line to the left border of the horizontal line:
     
    Cursor Design 2
  13. Draw another diagonal line from the top corner of the current shape to the intersection of horizontal and left diagonal line:
     
    Cursor Design 3
  14. On the Image Editor toolbar, click Fill Fill
  15. In the Colors window, click the button with a pink monitor Monitor
  16. In the drawing area, click the right triangle.
  17. In the Colors window, click the white color
  18. On the drawing area, click in the left triangle
     
    Cursor Design 4
  19. Save the file

Characteristics of a Cursor

 

Accessing a Cursor

There are two main ways you can use a cursor in your application. The easiest cursors are listed in the Cursor field of the Properties window for the control whose cursor you want to change. The available cursors are:

Cursor 1 Cursor 2 Cursor 3 Cursor 4

You can select one of these cursors in the Properties window and assign it to a control. These cursors are defined in a class called Cursors. This simple class mostly contains only a list of available cursors as properties. All these cursors are represented as Shared properties. Therefore, to use one of these cursors, call the name of the class, Cursors, followed by the class access operator ".", followed by the name of the cursor as it appears in the above list.

The Cursor class provides four constructors. One of them allows you to specify the path where the cursor is located. This constructor has the following syntax:

Public Sub New(fileName As String)

The argument passed to this constructor is the name or the location of the cursor as a file. After calling this constructor to initialize a Cursor variable, the cursor is ready. You can then use it as you see fit. For example, you can assign it to the Cursor property of a control.

When the cursor of a control has been changed, the control fires a CursorChanged event. This event is of type EventArgs.

Practical Learning Practical Learning: Using Cursors

  1. Display the form. Click the control on the upper-left section of the form
  2. In the Properties window, click the arrow of the combo box of the Cursor field and select PanNorth
  3. Right-click the form and click View Code
  4. To programmatically assign cursors, change the Load event as follows:
     
    Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            Dim icoMain As Icon = New Icon("diamond.ico")
            Icon = icoMain
    
            Dim curPush As Cursor = New Cursor("push.cur")
            Panel1.Cursor = Cursors.NoMove2D
            TreeView1.Cursor = curPush
            RichTextBox1.Cursor = Cursors.PanSE
    End Sub
  5. Execute the application to test it
     
    Cursor Cursor on a control
    Control showing a cursor Cursor displaying
  6. Close the form and return to your programming environment

The Hot Spot of a Cursor

Besides showing the current position of the mouse, depending on the application, at one time, the user may need to click. Because a cursor is primarily confined to a rectangular shape, a cursor must specify which one of its sections would lead the clicking. The area that directs or holds the clicking on a cursor is referred to as its hot spot. The person who designs a cursor must also specify its hot spot. This means that the hot spots have already been specified for all the built-in cursors of Microsoft Windows and the cursor that ship with the .NET Framework.

If you are visually creating a cursor, to specify its hot spot, on the Image Editor toolbar, you can click the Set Hot Spot Set Hot Spot Tool. Then, on the cursor, click the point that will act as the hot spot.

On an existing cursor, to know the coordinates of the hot spot, access the value of its HotSpot property. The Cursor.HotSpot property is of type Point.

Practical Learning Practical Learning: Creating a Hot Spot for a Cursor

  1. Display the cursor you were designing
  2. To set the position of the cursor pointer, on the Image Editor toolbar, click the Set Hot Spot Tool Set Hot Spot Tool
  3. Click the tip of the cursor at the intersection of vertical and the the diagonal lines in the top-left section
     
    Set Hot Spot Tool
  4. Save and close the cursor window
  5. Right-click the form and click View Code
  6. In the Class Name combo box, select (Form1 Events)
  7. In the Method Name combo box, select MouseMove
  8. Implement the event as follows:
     
    Private Sub Form1_MouseMove(ByVal sender As Object, _
                              ByVal e As System.Windows.Forms.MouseEventArgs) _
                                Handles Me.MouseMove
            Text = "Hot Spot: F(" & Cursor.HotSpot.X.ToString() & _
                                  ", " & Cursor.HotSpot.Y & ")"
    End Sub
  9. In the Class Name combo box, select ListBox1
  10. In the Method Name combo box, select MouseMove
  11. Implement the event as follows:
     
    Private Sub ListBox1_MouseMove(ByVal sender As Object, _
                               ByVal e As System.Windows.Forms.MouseEventArgs) _
                                   Handles ListBox1.MouseMove
            Text = "Hot Spot: L(" & ListBox1.Cursor.HotSpot.X.ToString() & _
                           ", " & ListBox1.Cursor.HotSpot.Y & ")"
    End Sub
  12. In the Class Name combo box, select Panel1
  13. In the Method Name combo box, select MouseMove
  14. Implement the event as follows:
     
    Private Sub Panel1_MouseMove(ByVal sender As Object, _
                              ByVal e As System.Windows.Forms.MouseEventArgs) _
                                 Handles Panel1.MouseMove
            Text = "Hot Spot: P(" & Panel1.Cursor.HotSpot.X.ToString() & _
                           ", " & Panel1.Cursor.HotSpot.Y & ")"
    End Sub
  15. In the Class Name combo box, select TreeView1
  16. In the Method Name combo box, select MouseMove
  17. Implement the event as follows:
     
    Private Sub TreeView1_MouseMove(ByVal sender As Object, _
                               ByVal e As System.Windows.Forms.MouseEventArgs) _
                                    Handles TreeView1.MouseMove
            Text = "Hot Spot: T(" & TreeView1.Cursor.HotSpot.X.ToString() & _
                       ", " & TreeView1.Cursor.HotSpot.Y & ")"
    End Sub
  18. In the Class Name combo box, select RichTextBox1
  19. In the Method Name combo box, select MouseMove
  20. Implement the event as follows:
     
    Private Sub RichTextBox1_MouseMove(ByVal sender As Object, _
                               ByVal e As System.Windows.Forms.MouseEventArgs) _
                                       Handles RichTextBox1.MouseMove
            Text = "Hot Spot: R(" & RichTextBox1.Cursor.HotSpot.X.ToString() & _
                      ", " & RichTextBox1.Cursor.HotSpot.Y & ")"
    End Sub
  21. Execute the application to see the results:
     
    Hot Spot
  22. Close the form and return to your programming environment

The Size of a Cursor

A cursor is a picture of size 32 x 32 pixels. To know the size of an existing cursor, you can get the value of the Size property of the Cursor object. The Cursor.Size property is of type Size.

To specify the rectangle in which the cursor much be confined, you can create a Rectangle object and assign it to the Clip property of a cursor. To know the rectangle in which a cursor is confined, get the value of its Clip property. The Cursor.Clip property is of type Rectanble.

The Position of a Cursor

While the user is moving the mouse on a control, the cursor moves also, which, by its purpose, is designed to show the current position of the mouse. In some application, at one particular time, you may want to know the coordinates of the mouse position. To provide you with this information, the Cursor class is equipped with a property named Position. The Position property is of type Point, which gives you to left (X) and the top (Y) coordinates of the mouse. Here is an example that displays the position of the mouse on the title bar of a form:

Private Sub Form1_MouseMove(ByVal sender As Object, _
                                ByVal e As System.Windows.Forms.MouseEventArgs) _
                                Handles Me.MouseMove
        Text = CStr(Cursor.Position.X) & _
                   ", " & CStr(Cursor.Position.Y)
End Sub

Showing or Hiding a Cursor

If at any time you want to hide a cursor, you can call the Cursor.Hide() method. Its syntax is:

Public Shared Sub Hide

To display the cursor again, you can call the Cursor.Show() method. Its syntax is:

Public Shared Sub Hide Show  

 

 

Home Copyright © 2008-2016, FunctionX, Inc.