0

我有一个程序,我希望右键单击一个按钮来执行完全不同数量的代码。我让代码显示示例的消息框,但最终它只是一个方法调用。我将向您展示我需要它的上下文。有关如何检测右键单击的任何和所有帮助都会有所帮助。这是我的代码片段:

    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);
         }
};
4

1 回答 1

2

您可以使用 MouseDown 事件并检查它是否正确

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...
      }
于 2013-05-10T15:26:29.713 回答