Home

GDI+ Resources: Cursors

 

Cursors

 

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 .NET provides a rich collections of cursors you can easily use in your application. You can apply them to any control as you wish. To do this, access the properties of the control and open the Cursor field. If those cursors are not enough, which is not unusual, you have various options. You can use one of the many cursors installed by Visual Studio in Drive:\Program Files\Microsoft Visual Studio\Common\Graphics\Cursors.

 

Practical Learning Practical Learning: Introducing Cursors

  1. Start a new Windows Application named Resources2
  2. Change the Text property of the form to GDI+ Resources
  3. On the Toolbox, click the ListBox control ListBox and click the upper-left section of the form
  4. Again, on the Toolbox, click the Panel control Panel and click the upper-right section of the form
  5. Once again, on the Toolbox, click the TreeView control TreeView and click the lower-left section of the form
  6. On the Toolbox, click the RichTextBox control RichTextBox and click the upper-right section of the form
     

Creating Cursors

To create and design your own cursor, you can use use Visual Studio. To do this, on the main menu of Visual Studio, you can click Project -> Add New Itemů Then, in the Add New Item dialog box, you can select Cursor File, give it a name and click Open.

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

 

Practical Learning Practical Learning: Creating a Cursor

  1. To create a new cursor, on the main menu of Visual Studio .NET, click File -> New -> File
  2. In the New File dialog box, click Cursor File and click Open
  3. In the Image Editor toolbar, click the Erase Tool Erase and wipe the drawing area to get rid of the I-beam that was left on the cursor
  4. On the Image Editor toolbar, click the Line tool Line. In the Colors window, make sure the black color is selected
  5. Draw a vertical line from the pixel on the 6th column and 2nd row from top
  6. 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 1
  7. Draw a horizontal line from the lower border of the dialog line to half-left
  8. Draw a diagonal line from the lower border of the vertical line to the left border of the horizontal line:
     
    Cursor Design 2
  9. Draw another diagonal line from the top corner of the current shape to the intersection of horizontal and left diagonal line:
     
    Cursor Design 3
  10. On the Image Editor toolbar, click Fill Fill
  11. In the Colors window, click the button with a pink monitor Monitor 2
  12. In the drawing area, click the right triangle.
  13. In the Colors window, click the white color
  14. On the drawing area, click in the left triangle
     
    Cursor Design 4
  15. To set the position of the cursor pointer, on the Image Editor toolbar, click the Set Hot Spot Tool Set Hot Spot
  16. 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
  17. To save the icon, on the Standard toolbar, click the Save All button Save All
  18. Locate the .\Resources2\bin\Debug folder of the current project and display it in the Save In combo box
  19. Change the name of the file to Push
 

Using Cursors

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 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 a period, followed by the name of the cursor as it appears in the above list.

The second way consists of using a cursor not listed in the Properties window. A cursor is based on the Cursor class. It 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 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.

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();

 

 

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, System.EventArgs e)
    {
    	Cursor curPush = new Cursor("Push.cur");
    	Cursor curPalette = new Cursor("C:\\Program Files\\Microsoft Visual Studio .NET 2003\\Common7\\Graphics\\cursors\\PALETTE.CUR");
    
    	this.panel1.Cursor = Cursors.NoMove2D;
    	this.treeView1.Cursor = curPush;
    	this.richTextBox1.Cursor = curPalette;
    }
  5. Execute the application to test it
     
  6. Close the form and return to your programming environment
 

Home Copyright © 2004-2010 FunctionX, Inc.