winform click点击事件 左键 OR 右键
1.Click事件——EventArgs强转MouseEventArgs
button1.Click += new System.EventHandler(button1_Click); private void button1_Click(object sender, EventArgs e) { //EventArgs继承自MouseEventArgs,所以可以强转 //把EventArgs强制转换成MouseEventArgs就可以判断鼠标左右键了 MouseEventArgs Mouse_e = (MouseEventArgs)e; //判断点击鼠标左键或右键 if (Mouse_e.Button == MouseButtons.Left) { MessageBox.Show("这是鼠标左键"); //这里写鼠标左键点击事件 } else if (Mouse_e.Button == MouseButtons.Right) { MessageBox.Show("这是鼠标右键"); //这里写鼠标左键点击事件 } }
2.通过MouseDown委托
button2.MouseDown += new MouseEventHandler(button2_MouseDown); private void button2_MouseDown(object sender, MouseEventArgs e) { //判断点击鼠标左键或右键 if (Mouse_e.Button == MouseButtons.Left) { MessageBox.Show("这是鼠标左键"); //这里写鼠标左键点击事件 } else if (Mouse_e.Button == MouseButtons.Right) { MessageBox.Show("这是鼠标右键"); //这里写鼠标左键点击事件 } }