Visual C++ .Net Tutorials: The Cursors


 

Introduction

A cursor is a small picture that represents the position of the mouse on a screen. To help in your applications development, Microsoft Visual Studio .Net ships with two sets of cursors: those readily available and those stored in the installed files.

Everyone of the cursors in MSVC .Net can be easily used simply by referring to its name once you know the cursor you want to use. Those cursors already available to you are:

MSVS .Net MSVS .Net MSVS .Net MSVS .Net

These cursors are stored in the Cursors namespace. To use one of the available cursors, call the Cursors namespace followed by the exact name of the above list.
Author Note In order to effectively use this lesson, you should have learned how to create a panel, a group box, a check box, and radio buttons.

  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with Cursors
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. Replace the contents of the empty file with the following:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	Panel *pnlRectangle;
    	GroupBox *GBox;
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Cursors Example";
    	this->Size = Drawing::Size(400, 200);
    
    	pnlRectangle = new Panel;
    	pnlRectangle->Location = Point(16, 16);
    	pnlRectangle->Size = Drawing::Size(150, 108);
    	pnlRectangle->BorderStyle = BorderStyle::Fixed3D;
    	pnlRectangle->Cursor = Cursors::SizeAll;
    	this->Controls->Add(pnlRectangle);
    
    	GBox = new GroupBox;
    	GBox->Location = Point(220, 8);
    	GBox->Text = S"Celebration";
    	GBox->Size = Drawing::Size(140, 120);
    	GBox->Cursor = Cursors::NoMoveHoriz;
    	Controls->Add(GBox);
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *FM = new SimpleForm;
    	Application::Run(FM);
    	return 0;
    }
  12. Test the application:
     
  13. Return to MSVC

Creating Cursors

Besides the cursors available in the studio, Microsoft Visual Studio .Net installs additional cursors in the
C:\Program Files\Microsoft Visual Studio .Net\Common7\Graphics\cursors accommodate more scenarios. The technique for using these cursors involves more than just knowing their name. Over all, you will sometimes use either an existing cursor or a custom one. For this reason and others, you can create your own cursors.

To create your own cursor, on the main menu of MSVC, you can click Project -> Add New Item... or Project -> Add Resource. A starting but empty cursor would be displayed. After designing a cursor, you must save it. It has an extension of .cur.

Essentially, a cursor uses only two colors, black or white. This is because a cursor is only used as an indicator of the presence or position of the mouse pointer on the screen. Based on this (limitation), you ought to be creative. The minimum you can give a cursor is a shape. This can be a square, a rectangle, a circle, an ellipse, a triangle, or any shape of your choice. You can make the cursor fully black by painting it with that color. If you decide to make the cursor completely white, make sure you draw the borders of the cursor. By playing with the frequency of pixels and varying the frequencies of black and white, you can create variances of gray.

Between the black and white colors, two gray degrees are provided to you. In reality these two colors are used to give a transparency to the cursor so the background can be seen when the mouse passes over a section of the document.

After creating a cursor, you must (in reality you should) define the area that serves as the actual pointer of the mouse. Such an area, actually a point, is called the hot spot of the cursor. If you don't define the hot spot, the resource editor will define or pick one for you, and you might not like it. Therefore, you should know where the hot spot of your cursor, or of any cursor your application is using, is.

Practical Learning: Creating a Cursor

  1. On the main menu of MSVC, click Project -> Add Resource…
  2. On the Add Resource dialog box, click Cursor and click New
  3. While the new cursor is still selected, on the Properties window, click Filename, type ThePoint and press Enter. Notice that an extension was added to the file name.
  4. If the Image Editor toolbar is not displaying, on the main menu, click View -> Toolbars -> Image Editor
    If the Colors window is not displaying, on the main menu, click Image -> Show Colors Window
    On the Image Editor toolbar, click the Line tool .
  5. In the Colors window, click the black button to select the black color.
  6. Draw a vertical line from the pixel on the 6th column and 2nd row from top
  7. 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
     
  8. Draw a horizontal line from the lower border of the dialog line to half-left
  9. Draw a diagonal line from the lower border of the vertical line to the left border of the horizontal line:
     
  10. Draw another diagonal line from the top corner of the current shape to the intersection of the horizontal and the left diagonal lines:
     
  11. On the Tools Palette, click Fill Tool
  12. On the Colors window, if you don't see a white button (as on the above pictures) drag the lower border of the Colors window.
    On the Colors window, click the last button of the second row and, on the Image Editor toolbar, click the Fill Tool
  13. On the drawing area, click the right triangle.
  14. On the Colors window, click the button with the black color
  15. On the drawing area, click in the left triangle
     
  16. To set the position of the cursor pointer, on the Image Editor toolbar, click the Set Hot Spot Tool button
     
  17. In the drawing area, click the most top black point of the cursor
     
  18. Save the cursor by click the Save All button
  19. To create another cursor, on the main menu, click Project -> Add Resource...
  20. On the Add Resource dialog box, double-click Cursor
  21. In the Properties window, click Filename, type AKeyboard and press Enter
  22. On the Image Editor toolbar, click the Text Tool button
  23. On the Text Tool window, click the Font... button
  24. In the Font combo box, as System is selected in the Font combo box, type win and select Wingdings. In the Font Style combo box, click Regular. In the Size combo box, click 24 and click OK
  25. Type 7
  26. This should produce the picture of a keyboard. As the keyboard is still selected, drag it to position it in the middle and center of the drawing area.
  27. Close the Text Tool window
  28. On the Colors window, click the last button of the second row and, in the Image Editor toolbar, click the Fill Tool button. Change all green colors inside the keyboard to white by clicking them:
     
  29. On the Image Editor toolbar, click the Set Hot Spot Tool button and, in the drawing area, click the end of the keyboard cord on top.

Using Custom Cursors

After creating your own cursor, you can use a constructor of the Cursor class, which is defined in the Forms namespace.

  1. To use the new custom cursors and a built-in cursor, change the source file as follows:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	// A constructor for the form object
    	SimpleForm();
    private:
    	// This panel will host some radio buttons
    	Panel *pnlPizzaSize;
    	// This group box will host some check boxes
    	GroupBox *GBox;
    	// Some radio buttons
    	RadioButton *rdoSmall;
    	RadioButton *rdoMedium;
    	RadioButton *rdoLarge;
    	// Some check boxes
    	CheckBox *ExtraCheese;
    	CheckBox *Sausage;
    	// A rectangular object
    	Panel *pnlRectangle;
    };
    
    SimpleForm::SimpleForm()
    {
    	// The caption of the form
    	this->Text = S"Cursors Example";
    	// The dimensions of the form
    	this->Size = Drawing::Size(360, 265);
    
    	// Create a panel
    	pnlPizzaSize = new Panel;
    	pnlPizzaSize->Location = Point(16, 16);
    	pnlPizzaSize->Size = Drawing::Size(150, 108);
    	pnlPizzaSize->BorderStyle = BorderStyle::Fixed3D;
    	// Set a built-in cursor for the panel 
    	pnlPizzaSize->Cursor = Cursors::Hand;
    
    	// Create some radio buttons
    	// and add each to the panel that will host them
    	rdoSmall = new RadioButton;
    	rdoSmall->Location = Point(16, 16);
    	rdoSmall->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	rdoSmall->Text = S"&Small";
    	pnlPizzaSize->Controls->Add(rdoSmall);
    
    	rdoMedium = new RadioButton;
    	rdoMedium->Location = Point(16, 40);
    	rdoMedium->Text = S"&Medium";
    	rdoMedium->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	pnlPizzaSize->Controls->Add(rdoMedium);
    
    	rdoLarge = new RadioButton;
    	rdoLarge->Location = Point(16, 64);
    	rdoLarge->Text = S"&Large";
    	rdoLarge->Checked = true;
    	rdoLarge->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	pnlPizzaSize->Controls->Add(rdoLarge);
    
    	// Add the panel itself to the form
    	this->Controls->Add(pnlPizzaSize);
    
    	GBox = new GroupBox;
    	GBox->Location = Point(200, 12);
    	GBox->Text = S"Pizza Toppings";
    	GBox->Size = Drawing::Size(140, 110);
    	// Set a custom cursor for the group box
    	GBox->Cursor = new System::Windows::Forms::Cursor("ThePoint.cur");
    
    	// Create some check boxes
    	// and add each to the group box that will host them
    	ExtraCheese = new CheckBox;
    	ExtraCheese->Location = Point(16, 30);
    	ExtraCheese->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	ExtraCheese->Text = S"&Extra Cheese";
    	GBox->Controls->Add(ExtraCheese);
    	Sausage = new CheckBox;
    	Sausage->Location = Point(16, 54);
    	Sausage->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	Sausage->Text = S"&Sausage";
    	GBox->Controls->Add(Sausage);
    
    	// Add the group box to the form
    	this->Controls->Add(GBox);
    	
    	// Create a rectangular object from a panel
    	pnlRectangle = new Panel;
    	pnlRectangle->Location = Point(16, 140);
    	pnlRectangle->Size = Drawing::Size(325, 80);
    	pnlRectangle->BorderStyle = BorderStyle::Fixed3D;
    	pnlRectangle->BackColor = Color::LightBlue;
    	// Set a custom cursor for the panel
    	pnlRectangle->Cursor = new System::Windows::Forms::Cursor("AKeyboard.cur");
    	this->Controls->Add(pnlRectangle);
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *FM = new SimpleForm;
    	Application::Run(FM);
    	return 0;
    }
  2. Test the application
 

Home Copyright © 2004-2010 FunctionX, Inc.