Visual C++ Detect a Right Click on a Button

纵饮孤独 提交于 2019-12-25 03:34:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!