MouseEventArgs 類別

定義

提供 MouseUpMouseDownMouseMove 事件的資料。

public ref class MouseEventArgs : EventArgs
[System.Runtime.InteropServices.ComVisible(true)]
public class MouseEventArgs : EventArgs
public class MouseEventArgs : EventArgs
[<System.Runtime.InteropServices.ComVisible(true)>]
type MouseEventArgs = class
    inherit EventArgs
type MouseEventArgs = class
    inherit EventArgs
Public Class MouseEventArgs
Inherits EventArgs
繼承
MouseEventArgs
衍生
屬性

範例

下列程式碼範例會 MouseDown 處理 控制項上的 TextBox 事件,讓按一下滑鼠右鍵會選取控制項中的所有文字。 此範例需要您有一個表單,其中包含 TextBox 名為 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 使用 屬性來追蹤滑鼠左鍵的點選,並繪製一系列的直線線段,以回應使用者輸入。 如果您隱藏表單,然後重新顯示該表單,則此範例不會重新繪製行;為了簡單起見,已省略此程式碼。

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 的目前位置。

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 的事件。

AxHostNotifyIcon 類別存在 Control 這三個事件。

如需事件模型的相關資訊,請參閱 處理和引發事件

建構函式

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)

適用於

另請參閱