滑鼠指標在控制項上按下按鍵時觸發。
public event MouseEventHandler MouseDown;
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
MouseEventArgs 關鍵屬性:
| 屬性 | 說明 |
|---|---|
| Button | 按下的滑鼠按鈕 (Left/Right/Middle) |
| Clicks | 點擊次數 (單擊/雙擊) |
| Delta | 滾輪轉動制動器計數 |
| Location | 事件發生時滑鼠位置 (Point) |
| X, Y | 座標分量 |
範例:右鍵顯示捷徑選單
private void Form1_MouseDown(object sender, MouseEventArgs e) {
OnMouseDown = true;
if (e.Button == MouseButtons.Right) {
this.contextMenuStrip1.Show(this, new Point(e.X, e.Y));
}
}
滑鼠指標在控制項上釋放按鍵時觸發。
public event MouseEventHandler MouseUp;
private void Form1_MouseUp(object sender, MouseEventArgs e) {
OnMouseDown = false;
}
配合 MouseMove 可實現拖曳邏輯 (按下設標記、移動更新位置、釋放清標記)。