VISUAL C ++检测右键点击一个按钮 [英] Visual C++ Detect a Right Click on a Button
本文介绍了VISUAL C ++检测右键点击一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个程序,我想在按钮上右击做一个完全不同的金额code的。我有code显示的例子一个消息框,但最终它将是一个方法调用每个。我会告诉你在我需要它的上下文。任何及所有关于如何检测鼠标右键将有助于帮助。以下是我有code的片段:
私人:系统:虚空的button1_Click(系统:对象^发件人,系统:: EventArgs的^ E){
字符串^ BUTTONNAME = safe_cast<按钮^>(发件人) - GT;名称;
safe_cast<按钮^>(发件人) - GT;启用= FALSE;
如果(/ *插入code这里来检测右击* /)
消息框显示::(右键);
其他
消息框显示::(左击);
消息框显示::(BUTTONNAME);
}
};
解决方案
您可以使用MouseDown事件并检查它是否是正确的。
无效button1_MouseDown(对象^ / * *发件人/,系统:视窗:形式:MouseEventArgs ^ E)
{
//更新鼠标信息鼠标路径
点mouseDownLocation =点(E-GT&; X,E> Y);
字符串^ eventString = nullptr;
开关(E-GT&按钮)
{
案例:: MouseButtons ::左:
eventString =L;
打破; 案例:: MouseButtons ::右:
eventString =R;
打破; 案例:: MouseButtons ::中东:
eventString =M;
打破; 案例:: MouseButtons :: XButton1:
eventString =X1;
打破; 案例:: MouseButtons :: XButton2:
eventString =X2;
打破; 案例:: MouseButtons ::无:
默认:
打破;
}
//处理在这里...
}
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);
}
};
解决方案
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...
}
这篇关于VISUAL C ++检测右键点击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文