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 |
#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; } |
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. |
#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; } |
Related Examples:
Drawing a Line
Drawing a Rectangle
|
||
Home | Copyright © 2002 FunctionX, Inc. | |
|