MouseEventArgs 类

MouseUpMouseDownMouseMove 事件提供数据。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public Class MouseEventArgs
    Inherits EventArgs
用法
Dim instance As MouseEventArgs
[ComVisibleAttribute(true)] 
public class MouseEventArgs : EventArgs
[ComVisibleAttribute(true)] 
public ref class MouseEventArgs : public EventArgs
/** @attribute ComVisibleAttribute(true) */ 
public class MouseEventArgs extends EventArgs
ComVisibleAttribute(true) 
public class MouseEventArgs extends EventArgs

备注

当指针在某个控件上,用户按下鼠标按钮时,将发生 MouseDown 事件。当指针保持在控件上,用户释放鼠标按钮时,发生 MouseUp 事件。当用户移动鼠标指针经过控件时,将发生 MouseMove 事件。MouseEventArgs 指定按下的鼠标按钮、鼠标按钮按下和释放的次数、鼠标的坐标以及鼠标轮的移动量。

如果用户在释放鼠标按钮之前将焦点切换到另一个应用程序,那么接收到 MouseDown 事件而没有对应的 MouseUp 是可能的。

ControlAxHostNotifyIcon 类存在这三个事件。

有关事件模型的信息,请参见 事件和委托

示例

下面的代码示例处理 TextBox 控件上的 MouseDown 事件,以便单击鼠标右键将选择该控件中的所有文本。此示例要求您有一个包含名为 textBox1TextBox 控件的窗体。

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
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);
    }
}

下面的代码示例使用 Location 属性跟踪鼠标左键单击,并绘制一系列直线段作为对用户输入的响应。如果隐藏窗体然后重新显示它,此示例不会重新绘制线段,该代码为简单起见而被省略了。

Dim FirstPoint As Point
Dim HaveFirstPoint As Boolean = False

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
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;
    }
}

下面的代码示例使用 XY 属性显示鼠标指针在 ToolTip 窗口中的当前位置。

Dim TrackTip As ToolTip

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

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
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);
}

继承层次结构

System.Object
   System.EventArgs
    System.Windows.Forms.MouseEventArgs
       System.Windows.Forms.DataGridViewCellMouseEventArgs
       System.Windows.Forms.HandledMouseEventArgs
       System.Windows.Forms.StatusBarPanelClickEventArgs
       System.Windows.Forms.TreeNodeMouseClickEventArgs

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

MouseEventArgs 成员
System.Windows.Forms 命名空间
OnMouseDown
Control.MouseDown 事件
OnMouseUp
Control.MouseUp 事件
OnMouseMove
Control.MouseMove 事件
MouseDown
MouseUp
MouseMove