MouseButtons Enumeration

 

Specifies constants that define which mouse button was pressed.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[FlagsAttribute]
[ComVisibleAttribute(true)]
public enum class MouseButtons

Member nameDescription
Left

The left mouse button was pressed.

Middle

The middle mouse button was pressed.

None

No mouse button was pressed.

Right

The right mouse button was pressed.

XButton1

The first XButton was pressed.

XButton2

The second XButton was pressed.

This enumeration is used by many classes, including AxHost, Control, DataGrid, Form, RadioButton, Splitter, StatusBar, and UpDownBase.

The following example demonstrates how to use the GetCharFromPosition method to obtain a character from the contents of a RichTextBox given its control coordinates. The example code uses coordinates located in the MouseEventArgs object passed as a parameter to the event handler to determine the location in the control to obtain the character. The character is then displayed in a MessageBox if it is not a space character. This example assumes that a RichTextBox control named richTextBox1 has been created and that the example code is connected to the MouseDown event of the RichTextBox.

private:
   void richTextBox1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
   {
      // Determine which mouse button is clicked.
      if ( e->Button == ::MouseButtons::Left )
      {
         // Obtain the character at which the mouse cursor was clicked at.
         Char tempChar = richTextBox1->GetCharFromPosition( Point(e->X,e->Y) );

         // Determine whether the character is an empty space.
         if (  !tempChar.ToString()->Equals( " " ) )

         // Display the character in a message box.
         MessageBox::Show( String::Format( "The character at the specified position is {0}.", tempChar ) );
      }
   }

The following example demonstrates using different mouse events to draw the path of the mouse on a Panel. A line segment is added to the GraphicsPath for each MouseMove and MouseDown events that occur. To update the graphics, the Invalidate method is called for the Panel on each MouseDown and MouseUp event. In addition, the graphic path is scrolled up or down when the MouseWheel event occurs. Additional mouse events, like MouseHover, are identified on screen as well. Also displayed on the screen is additional information about the mouse from the SystemInformation class.

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace MouseEvent
{
   public ref class Form1: public System::Windows::Forms::Form
   {
   private:
      System::Windows::Forms::Panel^ panel1;
      System::Windows::Forms::Label ^ label1;
      System::Windows::Forms::Label ^ label2;
      System::Windows::Forms::Label ^ label3;
      System::Windows::Forms::Label ^ label4;
      System::Windows::Forms::Label ^ label5;
      System::Windows::Forms::Label ^ label6;
      System::Windows::Forms::Label ^ label7;
      System::Windows::Forms::Label ^ label8;
      System::Windows::Forms::Label ^ label9;
      System::Windows::Forms::Button^ clearButton;
      System::Drawing::Drawing2D::GraphicsPath^ mousePath;
      System::Windows::Forms::GroupBox^ groupBox1;
      int fontSize;

   public:
      Form1()
      {
         fontSize = 20;
         mousePath = gcnew System::Drawing::Drawing2D::GraphicsPath;
         this->panel1 = gcnew System::Windows::Forms::Panel;
         this->label1 = gcnew System::Windows::Forms::Label;
         this->clearButton = gcnew System::Windows::Forms::Button;
         this->label2 = gcnew System::Windows::Forms::Label;
         this->label3 = gcnew System::Windows::Forms::Label;
         this->label4 = gcnew System::Windows::Forms::Label;
         this->label5 = gcnew System::Windows::Forms::Label;
         this->label6 = gcnew System::Windows::Forms::Label;
         this->label7 = gcnew System::Windows::Forms::Label;
         this->label8 = gcnew System::Windows::Forms::Label;
         this->label9 = gcnew System::Windows::Forms::Label;
         this->groupBox1 = gcnew System::Windows::Forms::GroupBox;

         // Mouse Events Label
         this->label1->Location = System::Drawing::Point( 24, 504 );
         this->label1->Size = System::Drawing::Size( 392, 23 );

         // DoubleClickSize Label
         this->label2->AutoSize = true;
         this->label2->Location = System::Drawing::Point( 24, 48 );
         this->label2->Size = System::Drawing::Size( 35, 13 );

         // DoubleClickTime Label
         this->label3->AutoSize = true;
         this->label3->Location = System::Drawing::Point( 24, 72 );
         this->label3->Size = System::Drawing::Size( 35, 13 );

         // MousePresent Label
         this->label4->AutoSize = true;
         this->label4->Location = System::Drawing::Point( 24, 96 );
         this->label4->Size = System::Drawing::Size( 35, 13 );

         // MouseButtons Label
         this->label5->AutoSize = true;
         this->label5->Location = System::Drawing::Point( 24, 120 );
         this->label5->Size = System::Drawing::Size( 35, 13 );

         // MouseButtonsSwapped Label
         this->label6->AutoSize = true;
         this->label6->Location = System::Drawing::Point( 320, 48 );
         this->label6->Size = System::Drawing::Size( 35, 13 );

         // MouseWheelPresent Label
         this->label7->AutoSize = true;
         this->label7->Location = System::Drawing::Point( 320, 72 );
         this->label7->Size = System::Drawing::Size( 35, 13 );

         // MouseWheelScrollLines Label
         this->label8->AutoSize = true;
         this->label8->Location = System::Drawing::Point( 320, 96 );
         this->label8->Size = System::Drawing::Size( 35, 13 );

         // NativeMouseWheelSupport Label
         this->label9->AutoSize = true;
         this->label9->Location = System::Drawing::Point( 320, 120 );
         this->label9->Size = System::Drawing::Size( 35, 13 );

         // Mouse Panel
         this->panel1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left | System::Windows::Forms::AnchorStyles::Right);
         this->panel1->BackColor = System::Drawing::SystemColors::ControlDark;
         this->panel1->Location = System::Drawing::Point( 16, 160 );
         this->panel1->Size = System::Drawing::Size( 664, 320 );
         this->panel1->MouseUp += gcnew System::Windows::Forms::MouseEventHandler( this, &Form1::panel1_MouseUp );
         this->panel1->Paint += gcnew System::Windows::Forms::PaintEventHandler( this, &Form1::panel1_Paint );
         this->panel1->MouseEnter += gcnew System::EventHandler( this, &Form1::panel1_MouseEnter );
         this->panel1->MouseHover += gcnew System::EventHandler( this, &Form1::panel1_MouseHover );
         this->panel1->MouseMove += gcnew System::Windows::Forms::MouseEventHandler( this, &Form1::panel1_MouseMove );
         this->panel1->MouseLeave += gcnew System::EventHandler( this, &Form1::panel1_MouseLeave );
         this->panel1->MouseDown += gcnew System::Windows::Forms::MouseEventHandler( this, &Form1::panel1_MouseDown );
         this->panel1->MouseWheel += gcnew System::Windows::Forms::MouseEventHandler( this, &Form1::panel1_MouseWheel );

         // Clear Button
         this->clearButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right);
         this->clearButton->Location = System::Drawing::Point( 592, 504 );
         this->clearButton->TabIndex = 1;
         this->clearButton->Text = "Clear";
         this->clearButton->Click += gcnew System::EventHandler( this, &Form1::clearButton_Click );

         // GroupBox
         this->groupBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left | System::Windows::Forms::AnchorStyles::Right);
         this->groupBox1->Location = System::Drawing::Point( 16, 24 );
         this->groupBox1->Size = System::Drawing::Size( 664, 128 );
         this->groupBox1->Text = "System::Windows::Forms::SystemInformation";

         // Set up how the form should be displayed and add the controls to the form.
         this->ClientSize = System::Drawing::Size( 696, 534 );
         array<System::Windows::Forms::Control^>^temp0 = {this->label9,this->label8,this->label7,this->label6,this->label5,this->label4,this->label3,this->label2,this->clearButton,this->panel1,this->label1,this->groupBox1};
         this->Controls->AddRange( temp0 );
         this->Text = "Mouse Event Example";

         // Displays information about the system mouse.
         label2->Text = "SystemInformation::DoubleClickSize: {0}",SystemInformation::DoubleClickSize;
         label3->Text = "SystemInformation::DoubleClickTime: {0}",SystemInformation::DoubleClickTime;
         label4->Text = "SystemInformation::MousePresent: {0}",SystemInformation::MousePresent;
         label5->Text = "SystemInformation::MouseButtons: {0}",SystemInformation::MouseButtons;
         label6->Text = "SystemInformation::MouseButtonsSwapped: {0}",SystemInformation::MouseButtonsSwapped;
         label7->Text = "SystemInformation::MouseWheelPresent: {0}",SystemInformation::MouseWheelPresent;
         label8->Text = "SystemInformation::MouseWheelScrollLines: {0}",SystemInformation::MouseWheelScrollLines;
         label9->Text = "SystemInformation::NativeMouseWheelSupport: {0}",SystemInformation::NativeMouseWheelSupport;
      }

   private:
      void panel1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
      {
         // Update the mouse path with the mouse information
         Point mouseDownLocation = Point(e->X,e->Y);
         String^ eventString = nullptr;
         switch ( e->Button )
         {
            case ::MouseButtons::Left:
               eventString = "L";
               break;

            case ::MouseButtons::Right:
               eventString = "R";
               break;

            case ::MouseButtons::Middle:
               eventString = "M";
               break;

            case ::MouseButtons::XButton1:
               eventString = "X1";
               break;

            case ::MouseButtons::XButton2:
               eventString = "X2";
               break;

            case ::MouseButtons::None:
            default:
               break;
         }
         if ( eventString != nullptr )
         {
            mousePath->AddString( eventString, FontFamily::GenericSerif, (int)FontStyle::Bold, (float)fontSize, mouseDownLocation, StringFormat::GenericDefault );
         }
         else
         {
            mousePath->AddLine( mouseDownLocation, mouseDownLocation );
         }

         panel1->Focus();
         panel1->Invalidate();
      }

      void panel1_MouseEnter( Object^ sender, System::EventArgs^ /*e*/ )
      {

         // Update the mouse event label to indicate the MouseEnter event occurred.
         label1->Text = String::Concat( sender->GetType(), ": MouseEnter" );
      }

      void panel1_MouseHover( Object^ sender, System::EventArgs^ /*e*/ )
      {

         // Update the mouse event label to indicate the MouseHover event occurred.
         label1->Text = String::Concat( sender->GetType(), ": MouseHover" );
      }

      void panel1_MouseLeave( Object^ sender, System::EventArgs^ /*e*/ )
      {

         // Update the mouse event label to indicate the MouseLeave event occurred.
         label1->Text = String::Concat( sender->GetType(), ": MouseLeave" );
      }

      void panel1_MouseMove( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
      {
         // Update the mouse path that is drawn onto the Panel.
         int mouseX = e->X;
         int mouseY = e->Y;
         mousePath->AddLine( mouseX, mouseY, mouseX, mouseY );
      }

      void panel1_MouseWheel( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
      {
         // Update the drawing based upon the mouse wheel scrolling.
         int numberOfTextLinesToMove = e->Delta * SystemInformation::MouseWheelScrollLines / 120;
         int numberOfPixelsToMove = numberOfTextLinesToMove * fontSize;
         if ( numberOfPixelsToMove != 0 )
         {
            System::Drawing::Drawing2D::Matrix^ translateMatrix = gcnew System::Drawing::Drawing2D::Matrix;
            translateMatrix->Translate( 0, (float)numberOfPixelsToMove );
            mousePath->Transform(translateMatrix);
         }

         panel1->Invalidate();
      }

      void panel1_MouseUp( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
      {
         Point mouseUpLocation = System::Drawing::Point( e->X, e->Y );

         // Show the number of clicks in the path graphic.
         int numberOfClicks = e->Clicks;
         mousePath->AddString( String::Format( "   {0}", numberOfClicks ), FontFamily::GenericSerif, (int)FontStyle::Bold, (float)fontSize, mouseUpLocation, StringFormat::GenericDefault );
         panel1->Invalidate();
      }

      void panel1_Paint( Object^ /*sender*/, System::Windows::Forms::PaintEventArgs^ e )
      {
         // Perform the painting of the Panel.
         e->Graphics->DrawPath( System::Drawing::Pens::DarkRed, mousePath );
      }

      void clearButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
      {
         // Clear the Panel display.
         delete mousePath;
         mousePath = gcnew System::Drawing::Drawing2D::GraphicsPath;
         panel1->Invalidate();
      }
   };
}

[STAThread]
int main()
{
   Application::Run( gcnew MouseEvent::Form1 );
}

.NET Framework
Available since 1.1
Return to top
Show: