Home

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 static 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 Cursor(String filename);

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. Double-click an unoccupied area of the form to access its Load event
  4. To programmatically assign cursors, implement the event as follows:
     
    private void Form1_Load(object sender, EventArgs e)
    {
        System.Drawing.Icon icoMain = new System.Drawing.Icon("diamond.ico");
        Icon = icoMain;
    
        System.Windows.Forms.Cursor curPush = 
    		new System.Windows.Forms.Cursor("push.cur");
        panel1.Cursor = Cursors.NoMove2D;
        treeView1.Cursor = curPush;
        richTextBox1.Cursor = Cursors.PanSE;
    }
  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. On the form, click an unoccupied area of the form
  6. In the Properties window, click the Events button and double-click the box right to MouseMove
  7. Implement the event as follows:
     
    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
                Text = "Hot Spot: F(" +
                           Cursor.HotSpot.X.ToString() +
                           ", " + Cursor.HotSpot.Y +
                           ")";
    }
  8. Return to the form
  9. Click the control on the top-left area of the form
  10. In the Events section of the Properties window, double-click the box right to MouseMove
  11. Implement the event as follows:
     
    private void listBox1_MouseMove(object sender, MouseEventArgs e)
    {
                Text = "Hot Spot: L(" +
                           listBox1.Cursor.HotSpot.X.ToString() +
                           ", " + listBox1.Cursor.HotSpot.Y +
                           ")";
    }
  12. Return to the form
  13. Click the control on the top-right area of the form
  14. In the Events section of the Properties window, double-click the box right to MouseMove
  15. Implement the event as follows:
     
    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
                Text = "Hot Spot: P(" +
                           panel1.Cursor.HotSpot.X.ToString() +
                           ", " + panel1.Cursor.HotSpot.Y +
                           ")";
    }
  16. Return to the form
  17. Click the control on the lower-left area of the form
  18. In the Events section of the Properties window, double-click the box right to MouseMove
  19. Implement the event as follows:
     
    private void treeView1_MouseMove(object sender, MouseEventArgs e)
    {
                Text = "Hot Spot: T(" +
                       treeView1.Cursor.HotSpot.X.ToString() +
                       ", " + treeView1.Cursor.HotSpot.Y +
                       ")";
    }
  20. Return to the form
  21. Click the control on the lower-right area of the form
  22. In the Events section of the Properties window, double-click the box right to MouseMove
  23. Implement the event as follows:
     
    private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
    {
                Text = "Hot Spot: R(" +
                       richTextBox1.Cursor.HotSpot.X.ToString() +
                       ", " + richTextBox1.Cursor.HotSpot.Y +
                       ")";
    }
  24. Execute the application to see the results:
     
  25. 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 void Exercise_MouseMove(object sender, MouseEventArgs e)
{
            Text = Cursor.Position.X.ToString() +
                   ", " +
                   Cursor.Position.Y.ToString();
}

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 static void Hide();

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

public static void Show();
 

Previous Copyright © 2007 FunctionX, Inc. Home