问题
I have a program where I want a right click on a button to do a completely different amount of code. I have the code display a messagebox for the example, but eventually it will just be a method call each. I'll show you the context in which I need it. Any and all help on how to detect a right click will help. Here's the snippet of code I have:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ buttonName = safe_cast<Button^>(sender)->Name;
safe_cast<Button^>(sender)->Enabled = false;
if(/*Insert Code Here To Detect a right click*/)
MessageBox::Show("Right Click");
else
MessageBox::Show("Left Click");
MessageBox::Show(buttonName);
}
};
回答1:
You can use the MouseDown event and check if it is right
void button1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
{
// Update the mouse path with the mouse information
Point mouseDownLocation = Point(e->X,e->Y);
String^ eventString = nullptr;
switch ( e->Button )
{
case ::MouseButtons::Left:
eventString = "L";
break;
case ::MouseButtons::Right:
eventString = "R";
break;
case ::MouseButtons::Middle:
eventString = "M";
break;
case ::MouseButtons::XButton1:
eventString = "X1";
break;
case ::MouseButtons::XButton2:
eventString = "X2";
break;
case ::MouseButtons::None:
default:
break;
}
//Process Here...
}
来源:https://stackoverflow.com/questions/16485514/visual-c-detect-a-right-click-on-a-button