如何检查在c ++ / cli中的鼠标单击事件期间单击了哪个鼠标按钮?

时间:2011-11-18 17:50:41

标签: c# winforms c++-cli mouseclick-event code-translation

它应该在c#中这样工作,但是c ++ / cli中的等价物是什么?

private void CustomControl_MouseClick(object sender, MouseEventArgs e) 
{        
   if (e.Button == MouseButtons.Right) 
   { 
     ... do something
   } 
} 

MouseButtons.Right,MouseButtons :: Right和MouseButtons->对所有人来说似乎都没有编译。它总是说

error C2039: 'Right' : is not a member of 'System::Enum'

这是我的c ++ / cli代码:

System::Void WindowTest::pictureBoxTest_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
    {
        if (e->Button == MouseButtons::Left)
        {
        // do something
            }
}

1 个答案:

答案 0 :(得分:1)

来自here您似乎错过::之前的MouseButtons::Right

  void panel1_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;
     }
     if ( eventString != nullptr )
     {
        mousePath->AddString( eventString, FontFamily::GenericSerif, (int)FontStyle::Bold, (float)fontSize, mouseDownLocation, StringFormat::GenericDefault );
     }
     else
     {
        mousePath->AddLine( mouseDownLocation, mouseDownLocation );
     }

     panel1->Focus();
     panel1->Invalidate();
  }