Visual c++检测右键单击按钮

Visual C++ Detect a Right Click on a Button

本文关键字:单击 按钮 右键 检测 c++ Visual      更新时间:2023-10-16

我有一个程序,我想在一个按钮上右键单击做一个完全不同的代码量。我让代码在示例中显示一个消息框,但最终它将只是每个方法调用。我会告诉你我需要它的背景。关于如何检测右键的任何和所有帮助都会有所帮助。下面是我的代码片段:

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

你可以使用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...
      }