MouseEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
public ref class MouseEventArgs : EventArgs
[System.Runtime.InteropServices.ComVisible(true)]
public class MouseEventArgs : EventArgs
[<System.Runtime.InteropServices.ComVisible(true)>]
type MouseEventArgs = class
inherit EventArgs
Public Class MouseEventArgs
Inherits EventArgs
- 继承
- 派生
- 属性
示例
下面的代码示例处理 MouseDown 控件上的 TextBox 事件,以便单击鼠标右键选择控件中的所有文本。 本示例要求你有一个包含命名textBox1控件的窗体TextBox。
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.ContextMenuStrip = New ContextMenuStrip()
End Sub
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
If (e.Button = MouseButtons.Right) Then
TextBox1.Select(0, TextBox1.Text.Length)
End If
End Sub
下面的代码示例使用 Location 该属性跟踪鼠标左键的单击,并绘制一系列直线段,以响应用户输入。 如果隐藏窗体,然后重新显示该窗体,则本示例不会重新绘制行;为简单起见,已省略此代码。
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
下面的代码示例使用 X 和 Y 属性在窗口中显示鼠标指针 ToolTip 的当前位置。
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当用户在指针位于控件上时按下鼠标按钮时发生该事件。 MouseUp当用户释放鼠标按钮时指针保留在控件上时发生该事件。 当用户 MouseMove 将鼠标指针移到控件上时发生该事件。 一个 MouseEventArgs 指定按下鼠标按钮、按下和释放鼠标按钮的次数、鼠标坐标以及鼠标滚轮移动量。
如果用户在释放鼠标按钮之前将焦点切换到另一 MouseDown 个应用程序,则可能会收到没有相应 MouseUp事件的事件。
这三个事件适用于Control和AxHostNotifyIcon类。
有关事件模型的信息,请参阅 处理和引发事件。
构造函数
| 名称 | 说明 |
|---|---|
| MouseEventArgs(MouseButtons, Int32, Int32, Int32, Int32) |
初始化 MouseEventArgs 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| Button |
获取按下了哪个鼠标按钮。 |
| Clicks |
获取按下和释放鼠标按钮的次数。 |
| Delta |
获取鼠标滚轮旋转的有符号计数,乘以WHEEL_DELTA常量。 缩进是鼠标滚轮的一个角。 |
| Location |
获取在生成鼠标事件期间鼠标的位置。 |
| X |
获取在生成鼠标事件期间鼠标的 x 坐标。 |
| Y |
获取在生成鼠标事件期间鼠标的 y 坐标。 |
方法
| 名称 | 说明 |
|---|---|
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |