MouseEventArgs MouseEventArgs MouseEventArgs MouseEventArgs Class

定义

MouseUpMouseDownMouseMove 事件提供数据。Provides data for the MouseUp, MouseDown, and MouseMove events.

public ref class MouseEventArgs : EventArgs
[System.Runtime.InteropServices.ComVisible(true)]
public class MouseEventArgs : EventArgs
type MouseEventArgs = class
    inherit EventArgs
Public Class MouseEventArgs
Inherits EventArgs
继承
MouseEventArgsMouseEventArgsMouseEventArgsMouseEventArgs
派生
属性

示例

下面的代码示例处理MouseDown上的事件TextBox控件,以便单击鼠标右键按钮选择所有文本控件中。The following code examples handles the MouseDown event on a TextBox control so that clicking the right mouse button selects all the text in the control. 此示例要求有一个包含窗体TextBox控件名为textBox1This example requires that you have a form that contains a TextBox control that is named textBox1.

private void Form1_Load(object sender, EventArgs e)
{
    // This line suppresses the default context menu for the TextBox control. 
    textBox1.ContextMenu = new ContextMenu();
    textBox1.MouseDown += new MouseEventHandler(textBox1_MouseDown);
}

void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        textBox1.Select(0, textBox1.Text.Length);
    }
}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.ContextMenu = New ContextMenu()
End Sub

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    If (e.Button = Windows.Forms.MouseButtons.Right) Then
        TextBox1.Select(0, TextBox1.Text.Length)
    End If
End Sub

下面的代码示例使用Location属性以跟踪单击鼠标左键并在响应用户输入中绘制一系列直线段。The following code example uses the Location property to track clicks of the left mouse button and to draw a series of straight line segments in response to user input. 该示例不会刷新行,如果隐藏窗体,然后重新显示它;为简单起见,省略了此代码。The example does not redraw the lines if you hide the form and then redisplay it; this code has been omitted for simplicity.

Point firstPoint;
Boolean haveFirstPoint;

public void EnableDrawing()
{
    this.MouseDown += new MouseEventHandler(Form1_MouseDownDrawing);
}

void Form1_MouseDownDrawing(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (haveFirstPoint)
    {
        Graphics g = this.CreateGraphics();
        g.DrawLine(Pens.Black, firstPoint, e.Location);
        haveFirstPoint = false;
    }
    else
    {
        firstPoint = e.Location;
        haveFirstPoint = true;
    }
}
Dim FirstPoint As Point
Dim HaveFirstPoint As Boolean = False

Private Sub Form1_MouseDownDrawing(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    If HaveFirstPoint Then
        Dim g As Graphics = Me.CreateGraphics()
        g.DrawLine(Pens.Black, FirstPoint, e.Location)
        HaveFirstPoint = False
    Else
        FirstPoint = e.Location
        HaveFirstPoint = True
    End If
End Sub

下面的代码示例使用XY属性,以显示在鼠标指针的当前位置ToolTip窗口。The following code example uses the X and Y properties to display the current position of the mouse pointer in a ToolTip window.

ToolTip trackTip;

private void TrackCoordinates()
{
    trackTip = new ToolTip();
    this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}

void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    String tipText = String.Format("({0}, {1})", e.X, e.Y);
    trackTip.Show(tipText, this, e.Location);
}
Dim TrackTip As ToolTip

Private Sub TrackCoordinates()
    TrackTip = New ToolTip()
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    Dim TipText As String = String.Format("({0}, {1})", e.X, e.Y)
    TrackTip.Show(TipText, Me, e.Location)
End Sub

注解

MouseDown事件发生在用户按下鼠标指针位于控件上时。The MouseDown event occurs when the user presses the mouse button while the pointer is over a control. MouseUp指针处于控件上方时,用户释放鼠标按钮时发生事件。The MouseUp event occurs when the user releases the mouse button while the pointer remains over the control. MouseMove用户将鼠标指针移到控件上时发生事件。The MouseMove event occurs when the user moves the mouse pointer over a control. 一个MouseEventArgs指定哪个鼠标按钮按下时,多少次按下并释放鼠标按钮的鼠标和鼠标滚轮移动的量的坐标。A MouseEventArgs specifies which mouse button is pressed, how many times the mouse button was pressed and released, the coordinates of the mouse, and the amount the mouse wheel moved.

可以接收MouseDown而没有相应的事件MouseUp,如果用户将焦点切换到另一个应用程序,然后松开鼠标按钮。It is possible to receive a MouseDown event without a corresponding MouseUp, if the user switches focus to another application before releasing the mouse button.

这三个事件存在ControlAxHost,和NotifyIcon类。These three events exist for the Control, AxHost, and NotifyIcon classes.

有关事件模型的信息,请参阅处理和引发事件For information about the event model, see Handling and Raising Events.

构造函数

MouseEventArgs(MouseButtons, Int32, Int32, Int32, Int32) MouseEventArgs(MouseButtons, Int32, Int32, Int32, Int32) MouseEventArgs(MouseButtons, Int32, Int32, Int32, Int32) MouseEventArgs(MouseButtons, Int32, Int32, Int32, Int32)

初始化 MouseEventArgs 类的新实例。Initializes a new instance of the MouseEventArgs class.

属性

Button Button Button Button

获取曾按下的是哪个鼠标按钮。Gets which mouse button was pressed.

Clicks Clicks Clicks Clicks

获取按下并释放鼠标按钮的次数。Gets the number of times the mouse button was pressed and released.

Delta Delta Delta Delta

获取鼠标轮已转动的制动器数的有符号计数乘以 WHEEL_DELTA 常数。Gets a signed count of the number of detents the mouse wheel has rotated, multiplied by the WHEEL_DELTA constant. 制动器是鼠标轮的一个凹口。A detent is one notch of the mouse wheel.

Location Location Location Location

获取鼠标在产生鼠标事件时的位置。Gets the location of the mouse during the generating mouse event.

X X X X

获取鼠标在产生鼠标事件时的 x 坐标。Gets the x-coordinate of the mouse during the generating mouse event.

Y Y Y Y

获取鼠标在产生鼠标事件时的 y 坐标。Gets the y-coordinate of the mouse during the generating mouse event.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅