MouseButtons Enumeration
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
Namespace: System.Windows.FormsAssembly: System.Windows.Forms (in system.windows.forms.dll)
| Member name | Description | |
|---|---|---|
![]() | 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. With Windows 2000, Microsoft is introducing support for the Microsoft IntelliMouse Explorer, which is a mouse with five buttons. The two new mouse buttons (XBUTTON1 and XBUTTON2) provide backward/forward navigation. | |
| XButton2 | The second XButton was pressed. With Windows 2000, Microsoft is introducing support for the Microsoft IntelliMouse Explorer, which is a mouse with five buttons. The two new mouse buttons (XBUTTON1 and XBUTTON2) provide backward/forward navigation. |
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 Sub richTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles richTextBox1.MouseDown ' Determine which mouse button is clicked. If e.Button = MouseButtons.Left Then ' Obtain the character at which the mouse cursor was clicked. Dim tempChar As Char = richTextBox1.GetCharFromPosition(New Point(e.X, e.Y)) ' Determine whether the character is an empty space. If tempChar <> " " Then ' Display the character in a message box. MessageBox.Show(("The character at the specified position is " + tempChar + ".")) End If End If End Sub
private void richTextBox1_MouseDown(Object sender, System.Windows.
Forms.MouseEventArgs e)
{
// Determine which mouse button is clicked.
if (e.get_Button().Equals(get_MouseButtons().Left)) {
// Obtain the character at which the mouse cursor was clicked at.
char tempChar = richTextBox1.GetCharFromPosition(new Point(e.
get_X(), e.get_Y()));
// Determine whether the character is an empty space.
if (!(((System.Char)tempChar).ToString().Equals(" "))) {
// Display the character in a message box.
MessageBox.Show("The character at the specified position is "
+ tempChar + ".");
}
}
} //richTextBox1_MouseDown
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.
Imports System Imports System.Drawing Imports System.Windows.Forms Namespace MouseEvent ' Summary description for Form1. Public NotInheritable Class Form1 Inherits System.Windows.Forms.Form Friend WithEvents panel1 As System.Windows.Forms.Panel Private label1 As System.Windows.Forms.Label Private label2 As System.Windows.Forms.Label Private label3 As System.Windows.Forms.Label Private label4 As System.Windows.Forms.Label Private label5 As System.Windows.Forms.Label Private label6 As System.Windows.Forms.Label Private label7 As System.Windows.Forms.Label Private label8 As System.Windows.Forms.Label Private label9 As System.Windows.Forms.Label Friend WithEvents clearButton As System.Windows.Forms.Button Private mousePath As System.Drawing.Drawing2D.GraphicsPath Private groupBox1 As System.Windows.Forms.GroupBox Private fontSize As Integer = 20 <System.STAThread()> _ Public Shared Sub Main() System.Windows.Forms.Application.Run(New Form1()) End Sub 'Main Public Sub New() mousePath = New System.Drawing.Drawing2D.GraphicsPath() Me.panel1 = New System.Windows.Forms.Panel() Me.label1 = New System.Windows.Forms.Label() Me.clearButton = New System.Windows.Forms.Button() Me.label2 = New System.Windows.Forms.Label() Me.label3 = New System.Windows.Forms.Label() Me.label4 = New System.Windows.Forms.Label() Me.label5 = New System.Windows.Forms.Label() Me.label6 = New System.Windows.Forms.Label() Me.label7 = New System.Windows.Forms.Label() Me.label8 = New System.Windows.Forms.Label() Me.label9 = New System.Windows.Forms.Label() Me.groupBox1 = New System.Windows.Forms.GroupBox() ' Mouse Events Label Me.label1.Location = New System.Drawing.Point(24, 504) Me.label1.Size = New System.Drawing.Size(392, 23) ' DoubleClickSize Label Me.label2.AutoSize = True Me.label2.Location = New System.Drawing.Point(24, 48) Me.label2.Size = New System.Drawing.Size(35, 13) ' DoubleClickTime Label Me.label3.AutoSize = True Me.label3.Location = New System.Drawing.Point(24, 72) Me.label3.Size = New System.Drawing.Size(35, 13) ' MousePresent Label Me.label4.AutoSize = True Me.label4.Location = New System.Drawing.Point(24, 96) Me.label4.Size = New System.Drawing.Size(35, 13) ' MouseButtons Label Me.label5.AutoSize = True Me.label5.Location = New System.Drawing.Point(24, 120) Me.label5.Size = New System.Drawing.Size(35, 13) ' MouseButtonsSwapped Label Me.label6.AutoSize = True Me.label6.Location = New System.Drawing.Point(320, 48) Me.label6.Size = New System.Drawing.Size(35, 13) ' MouseWheelPresent Label Me.label7.AutoSize = True Me.label7.Location = New System.Drawing.Point(320, 72) Me.label7.Size = New System.Drawing.Size(35, 13) ' MouseWheelScrollLines Label Me.label8.AutoSize = True Me.label8.Location = New System.Drawing.Point(320, 96) Me.label8.Size = New System.Drawing.Size(35, 13) ' NativeMouseWheelSupport Label Me.label9.AutoSize = True Me.label9.Location = New System.Drawing.Point(320, 120) Me.label9.Size = New System.Drawing.Size(35, 13) ' Mouse Panel Me.panel1.Anchor = System.Windows.Forms.AnchorStyles.Top Or _ System.Windows.Forms.AnchorStyles.Left Or _ System.Windows.Forms.AnchorStyles.Right Me.panel1.BackColor = System.Drawing.SystemColors.ControlDark Me.panel1.Location = New System.Drawing.Point(16, 160) Me.panel1.Size = New System.Drawing.Size(664, 320) ' Clear Button Me.clearButton.Anchor = System.Windows.Forms.AnchorStyles.Top Or _ System.Windows.Forms.AnchorStyles.Right Me.clearButton.Location = New System.Drawing.Point(592, 504) Me.clearButton.TabIndex = 1 Me.clearButton.Text = "Clear" ' GroupBox Me.groupBox1.Anchor = System.Windows.Forms.AnchorStyles.Top Or _ System.Windows.Forms.AnchorStyles.Left Or _ System.Windows.Forms.AnchorStyles.Right Me.groupBox1.Location = New System.Drawing.Point(16, 24) Me.groupBox1.Size = New System.Drawing.Size(664, 128) Me.groupBox1.Text = "System.Windows.Forms.SystemInformation" ' Set up how the form should be displayed and add the controls to the form. Me.ClientSize = New System.Drawing.Size(696, 534) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.label9, _ Me.label8, Me.label7, Me.label6, Me.label5, Me.label4, _ Me.label3, Me.label2, Me.clearButton, Me.panel1, Me.label1, Me.groupBox1}) Me.Text = "Mouse Event Example" ' Display information about the system mouse. label2.Text = "SystemInformation.DoubleClickSize: " + SystemInformation.DoubleClickSize.ToString() label3.Text = "SystemInformation.DoubleClickTime: " + SystemInformation.DoubleClickTime.ToString() label4.Text = "SystemInformation.MousePresent: " + SystemInformation.MousePresent.ToString() label5.Text = "SystemInformation.MouseButtons: " + SystemInformation.MouseButtons.ToString() label6.Text = "SystemInformation.MouseButtonsSwapped: " + SystemInformation.MouseButtonsSwapped.ToString() label7.Text = "SystemInformation.MouseWheelPresent: " + SystemInformation.MouseWheelPresent.ToString() label8.Text = "SystemInformation.MouseWheelScrollLines: " + SystemInformation.MouseWheelScrollLines.ToString() label9.Text = "SystemInformation.NativeMouseWheelSupport: " + SystemInformation.NativeMouseWheelSupport.ToString() End Sub 'New Private Sub panel1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles panel1.MouseDown ' Update the mouse path with the mouse information Dim mouseDownLocation As New Point(e.X, e.Y) Dim eventString As String = Nothing Select Case e.Button Case MouseButtons.Left eventString = "L" Case MouseButtons.Right eventString = "R" Case MouseButtons.Middle eventString = "M" Case MouseButtons.XButton1 eventString = "X1" Case MouseButtons.XButton2 eventString = "X2" Case MouseButtons.None: eventString = Nothing End Select If Not (eventString Is Nothing) Then mousePath.AddString(eventString, FontFamily.GenericSerif, CInt(FontStyle.Bold), fontSize, mouseDownLocation, StringFormat.GenericDefault) Else mousePath.AddLine(mouseDownLocation, mouseDownLocation) End If panel1.Focus() panel1.Invalidate() End Sub Private Sub panel1_MouseEnter(sender As Object, e As System.EventArgs) Handles panel1.MouseEnter ' Update the mouse event label to indicate the MouseEnter event occurred. label1.Text = sender.GetType().ToString() + ": MouseEnter" End Sub Private Sub panel1_MouseHover(sender As Object, e As System.EventArgs) Handles panel1.MouseHover ' Update the mouse event label to indicate the MouseHover event occurred. label1.Text = sender.GetType().ToString() + ": MouseHover" End Sub Private Sub panel1_MouseLeave(sender As Object, e As System.EventArgs) Handles panel1.MouseLeave ' Update the mouse event label to indicate the MouseLeave event occurred. label1.Text = sender.GetType().ToString() + ": MouseLeave" End Sub Private Sub panel1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles panel1.MouseMove ' Update the mouse path that is drawn onto the Panel. Dim mouseX As Integer = e.X Dim mouseY As Integer = e.Y mousePath.AddLine(mouseX, mouseY, mouseX, mouseY) End Sub Private Sub panel1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles panel1.MouseWheel ' Update the drawing based upon the mouse wheel scrolling. Dim numberOfTextLinesToMove As Integer = e.Delta * SystemInformation.MouseWheelScrollLines / 120 ' WHEEL_DATA Dim numberOfPixelsToMove As Integer = numberOfTextLinesToMove * fontSize If numberOfPixelsToMove <> 0 Then Dim translateMatrix As New System.Drawing.Drawing2D.Matrix() translateMatrix.Translate(0, numberOfPixelsToMove) mousePath.Transform(translateMatrix) End If panel1.Invalidate() End Sub Private Sub panel1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles panel1.MouseUp Dim mouseUpLocation = New System.Drawing.Point(e.X, e.Y) ' Show the number of clicks in the path graphic. Dim numberOfClicks As Integer = e.Clicks mousePath.AddString(" " + numberOfClicks.ToString(), _ FontFamily.GenericSerif, CInt(FontStyle.Bold), _ fontSize, mouseUpLocation, StringFormat.GenericDefault) panel1.Invalidate() End Sub Private Sub panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles panel1.Paint ' Perform the painting of the Panel. e.Graphics.DrawPath(System.Drawing.Pens.DarkRed, mousePath) End Sub Private Sub clearButton_Click(sender As Object, e As System.EventArgs) Handles clearButton.Click ' Clear the Panel display. mousePath.Dispose() mousePath = New System.Drawing.Drawing2D.GraphicsPath() panel1.Invalidate() End Sub End Class 'Form1 End Namespace
package MouseEvent;
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Button clearButton;
private System.Drawing.Drawing2D.GraphicsPath mousePath;
private System.Windows.Forms.GroupBox groupBox1;
private int fontSize = 20;
/** @attribute STAThread()
*/
public static void main(String[] args)
{
Application.Run(new Form1());
} //main
public Form1()
{
mousePath = new System.Drawing.Drawing2D.GraphicsPath();
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.clearButton = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
// Mouse Events Label
this.label1.set_Location(new System.Drawing.Point(24, 504));
this.label1.set_Size(new System.Drawing.Size(392, 23));
// DoubleClickSize Label
this.label2.set_AutoSize(true);
this.label2.set_Location(new System.Drawing.Point(24, 48));
this.label2.set_Size(new System.Drawing.Size(35, 13));
// DoubleClickTime Label
this.label3.set_AutoSize(true);
this.label3.set_Location(new System.Drawing.Point(24, 72));
this.label3.set_Size(new System.Drawing.Size(35, 13));
// MousePresent Label
this.label4.set_AutoSize(true);
this.label4.set_Location(new System.Drawing.Point(24, 96));
this.label4.set_Size(new System.Drawing.Size(35, 13));
// MouseButtons Label
this.label5.set_AutoSize(true);
this.label5.set_Location(new System.Drawing.Point(24, 120));
this.label5.set_Size(new System.Drawing.Size(35, 13));
// MouseButtonsSwapped Label
this.label6.set_AutoSize(true);
this.label6.set_Location(new System.Drawing.Point(320, 48));
this.label6.set_Size(new System.Drawing.Size(35, 13));
// MouseWheelPresent Label
this.label7.set_AutoSize(true);
this.label7.set_Location(new System.Drawing.Point(320, 72));
this.label7.set_Size(new System.Drawing.Size(35, 13));
// MouseWheelScrollLines Label
this.label8.set_AutoSize(true);
this.label8.set_Location(new System.Drawing.Point(320, 96));
this.label8.set_Size(new System.Drawing.Size(35, 13));
// NativeMouseWheelSupport Label
this.label9.set_AutoSize(true);
this.label9.set_Location(new System.Drawing.Point(320, 120));
this.label9.set_Size(new System.Drawing.Size(35, 13));
// Mouse Panel
this.panel1.set_Anchor(System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Left
| System.Windows.Forms.AnchorStyles.Right);
this.panel1.set_BackColor(System.Drawing.SystemColors.get_ControlDark());
this.panel1.set_Location(new System.Drawing.Point(16, 160));
this.panel1.set_Size(new System.Drawing.Size(664, 320));
this.panel1.add_MouseUp(
new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp));
this.panel1.add_Paint(new System.Windows.Forms.PaintEventHandler(
this.panel1_Paint));
this.panel1.add_MouseEnter(new System.EventHandler(
this.panel1_MouseEnter));
this.panel1.add_MouseHover(new System.EventHandler(
this.panel1_MouseHover));
this.panel1.add_MouseMove(
new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove));
this.panel1.add_MouseLeave(
new System.EventHandler(this.panel1_MouseLeave));
this.panel1.add_MouseDown(
new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown));
this.panel1.add_MouseWheel(
new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel));
// Clear Button
this.clearButton.set_Anchor(System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Right);
this.clearButton.set_Location(new System.Drawing.Point(592, 504));
this.clearButton.set_TabIndex(1);
this.clearButton.set_Text("Clear");
this.clearButton.add_Click(new System.EventHandler(
this.clearButton_Click));
// GroupBox
this.groupBox1.set_Anchor(System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Left
| System.Windows.Forms.AnchorStyles.Right);
this.groupBox1.set_Location(new System.Drawing.Point(16, 24));
this.groupBox1.set_Size(new System.Drawing.Size(664, 128));
this.groupBox1.set_Text("System.Windows.Forms.SystemInformation");
// Set up how the form should be displayed and add the controls
// to the form.
this.set_ClientSize(new System.Drawing.Size(696, 534));
this.get_Controls().AddRange(new System.Windows.Forms.Control[] {
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.set_Text("Mouse Event Example");
// Displays information about the system mouse.
label2.set_Text("SystemInformation.DoubleClickSize: "
+ SystemInformation.get_DoubleClickSize().ToString());
label3.set_Text("SystemInformation.DoubleClickTime: "
+ ((Int32)SystemInformation.get_DoubleClickTime()).ToString());
label4.set_Text("SystemInformation.MousePresent: "
+ ((System.Boolean)SystemInformation.get_MousePresent()).ToString());
label5.set_Text("SystemInformation.MouseButtons: "
+ ((Int32)SystemInformation.get_MouseButtons()).ToString());
label6.set_Text("SystemInformation.MouseButtonsSwapped: "
+ ((System.Boolean)SystemInformation.get_MouseButtonsSwapped()).
ToString());
label7.set_Text("SystemInformation.MouseWheelPresent: "
+ ((System.Boolean)SystemInformation.get_MouseWheelPresent()).
ToString());
label8.set_Text("SystemInformation.MouseWheelScrollLines: "
+ ((Int32)SystemInformation.get_MouseWheelScrollLines()).
ToString());
label9.set_Text("SystemInformation.NativeMouseWheelSupport: "
+ ((System.Boolean)SystemInformation.
get_NativeMouseWheelSupport()).ToString());
} //Form1
private void panel1_MouseDown(Object sender,
System.Windows.Forms.MouseEventArgs e)
{
// Update the mouse path with the mouse information
Point mouseDownLocation = new Point(e.get_X(), e.get_Y());
String eventString = null;
switch (e.get_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 != null) {
mousePath.AddString(eventString, FontFamily.get_GenericSerif(),
(int)FontStyle.Bold, fontSize, mouseDownLocation,
StringFormat.get_GenericDefault());
}
else {
mousePath.AddLine(mouseDownLocation, mouseDownLocation);
}
panel1.Focus();
panel1.Invalidate();
} //panel1_MouseDown
private void panel1_MouseEnter(Object sender, System.EventArgs e)
{
// Update the mouse event label to indicate the MouseEnter event occurred.
label1.set_Text(sender.GetType().ToString() + ": MouseEnter");
} //panel1_MouseEnter
private void panel1_MouseHover(Object sender, System.EventArgs e)
{
// Update the mouse event label to indicate the MouseHover event occurred.
label1.set_Text(sender.GetType().ToString() + ": MouseHover");
} //panel1_MouseHover
private void panel1_MouseLeave(Object sender, System.EventArgs e)
{
// Update the mouse event label to indicate the MouseLeave event occurred.
label1.set_Text(sender.GetType().ToString() + ": MouseLeave");
} //panel1_MouseLeave
private void panel1_MouseMove(Object sender,
System.Windows.Forms.MouseEventArgs e)
{
// Update the mouse path that is drawn onto the Panel.
int mouseX = e.get_X();
int mouseY = e.get_Y();
mousePath.AddLine(mouseX, mouseY, mouseX, mouseY);
} //panel1_MouseMove
private void panel1_MouseWheel(Object sender,
System.Windows.Forms.MouseEventArgs e)
{
// Update the drawing based upon the mouse wheel scrolling.
int numberOfTextLinesToMove =
e.get_Delta() * SystemInformation.get_MouseWheelScrollLines() / 120;
int numberOfPixelsToMove = numberOfTextLinesToMove * fontSize;
if (numberOfPixelsToMove != 0) {
System.Drawing.Drawing2D.Matrix translateMatrix =
new System.Drawing.Drawing2D.Matrix();
translateMatrix.Translate(0, numberOfPixelsToMove);
mousePath.Transform(translateMatrix);
}
panel1.Invalidate();
} //panel1_MouseWheel
private void panel1_MouseUp(Object sender,
System.Windows.Forms.MouseEventArgs e)
{
Point mouseUpLocation = new System.Drawing.Point(e.get_X(), e.get_Y());
// Show the number of clicks in the path graphic.
int numberOfClicks = e.get_Clicks();
mousePath.AddString(" " + ((Int32)numberOfClicks).ToString(),
FontFamily.get_GenericSerif(), (int)(FontStyle.Bold), fontSize,
mouseUpLocation, StringFormat.get_GenericDefault());
panel1.Invalidate();
} //panel1_MouseUp
private void panel1_Paint(Object sender,
System.Windows.Forms.PaintEventArgs e)
{
// Perform the painting of the Panel.
e.get_Graphics().DrawPath(System.Drawing.Pens.get_DarkRed(), mousePath);
} //panel1_Paint
private void clearButton_Click(Object sender, System.EventArgs e)
{
// Clear the Panel display.
mousePath.Dispose();
mousePath = new System.Drawing.Drawing2D.GraphicsPath();
panel1.Invalidate();
} //clearButton_Click
} //Form1
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
