1. 程式人生 > >C# winform 判斷click事件點選的是左鍵還是右鍵

C# winform 判斷click事件點選的是左鍵還是右鍵

button.Click += new System.EventHandler(btn_Click);
private void btn_Click(object sender, EventArgs e)
{
//EventArgs繼承自MouseEventArgs,所以可以強轉
MouseEventArgs Mouse_e = (MouseEventArgs)e;


//點滑鼠右鍵,return
if (Mouse_e.Button == MouseButtons.Right)
{
MessageBox.Show("右鍵!");
}

}

只有把EventArgs強制轉換成MouseEventArgs就可以有相當於MouseDown事件的功能了 


button2.MouseDown += new MouseEventHandler(btn2_MouseDown);
private void btn2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("右鍵");
}
}