當滑鼠指標位於控制項上且按下滑鼠按鍵時觸發。
語法: public event MouseEventHandler MouseDown
MouseEventHandler:處理 MouseDown、MouseUp、MouseMove 事件的委派。
委派語法:public delegate void MouseEventHandler(Object sender, MouseEventArgs e)
sender:事件來源。e:包含事件資料的 MouseEventArgs。| 屬性 | 說明 |
|---|---|
Button | 取得按下的滑鼠按鈕 |
Clicks | 取得按下並釋放的次數 |
Delta | 取得滑鼠滾輪轉動的制動器計數 |
Location | 取得事件發生時滑鼠位置 |
X, Y | 取得事件發生時的 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;
}
單擊:MouseDown → MouseUp → MouseClick
雙擊:MouseDown → MouseUp → MouseClick → MouseDown → MouseUp → MouseDoubleClick
MouseDown 判斷條件後呼叫 DoDragDrop。MouseUp 適合結束拖曳、釋放資源、重設狀態旗標。e.Button 可區分左/右/中鍵,實現不同互動邏輯。