我正在尝试在visual c#中制作一个扫雷型游戏,我希望在我右键单击并左键单击按钮时会发生不同的事情,我该怎么做?
我试过这段代码,但它只注册了左键点击:
private void button1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MessageBox.Show("Left");
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("Right");
}
}
您将不得不使用MouseUp
或MouseDown
事件而不是Click
事件来捕获右键单击。
只需尝试使用button1_MouseDown
事件而不是button1_MouseClick
事件。它将解决您的问题。
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//do something
}
if (e.Button == MouseButtons.Right)
{
//do something
}
}
按钮只对MouseButtons.Left
作出反应,而不是对MouseButton.Right
反应,甚至对中间反应。
void Select(object sender, MouseEventArgs e)
{
/* var btn = sender as CardButton;*/
if (e.Button == MouseButtons.Left)
{
if (this.Selected == false)
{
this.Selected = true;
}
else
{
this.Selected = false;
}
}
if (e.Button == MouseButtons.Right)
{
if (this.Selected == false)
{
this.Selected = true;
}
else
{
this.Selected = false;
}
}
Draw();
}