Visual C++ .Net Tutorials: The Mouse


Introduction

The mouse is an object that allows the user to work on any available area on the screen, including areas where the user cannot type. To use the mouse, the user clicks one of its button or simply moves the mouse on the screen or on displaying controls. To accomplish its intended purpose, depending on how it is used, the mouse carries a few pieces of information that allows the programmer to find out what is going on or to take specific action

  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 Using the Mouse
  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();
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Using the Mouse";
    	this->Size = Drawing::Size(640, 480);
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *SF = new SimpleForm();
    	Application::Run(SF);
    
    	return 0;
    }
  12. Test the application.

The Mouse Down Event

When the user presses one of the buttons on the mouse, the OnMouseDown event is fired. This event carries information such as the button that was pressed (the left, the middle, or the right button), the coordinates of the point where the mouse was clicked (the coordinates are known as X and Y), etc. Therefore, when implementing this event, you can find out what button was pressed and where it was pressed.

  1. To implement the OnMouseDown event, when the user presses a mouse's button, we will display a label where the button was pressed.
    Therefore, change the 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:
    	SimpleForm();
    private:
    	Label *lblMousePosition;
    	void FormMouseDown(Object *Sender, MouseEventArgs *Args);
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Using the Mouse";
    	this->Size = Drawing::Size(640, 480);
    	this->add_MouseDown(new MouseEventHandler(this, FormMouseDown));
    
    	lblMousePosition = new Label;
    	this->Controls->Add(lblMousePosition);
    }
    
    void SimpleForm::FormMouseDown(Object *Sender, MouseEventArgs *Args)
    {
    	lblMousePosition->Location = Point(Args->X, Args->Y);
    	lblMousePosition->Text = S"Mouse Position";
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *SF = new SimpleForm();
    	Application::Run(SF);
    
    	return 0;
    }
  2. Test the application

Related Examples:

Drawing a Line
Drawing a Rectangle

 

Home Copyright © 2002 FunctionX, Inc.