鼠标按钮::在 c++/cli 中向左

发布于 2024-10-20 11:56:08 字数 187 浏览 2 评论 0原文

我想检查一下左侧按钮是否被按下。

我对 Msdna 感到愤怒:

if(e->Button == MouseButtons.Left) {...}
//or
if(e->Button == ::MouseButtons.Left) {...}

但它们都没有编译通过。

I would like to check the left button is the pressed one.

I red on Msdna:

if(e->Button == MouseButtons.Left) {...}
//or
if(e->Button == ::MouseButtons.Left) {...}

But no one of them compiles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-10-27 11:56:08

这是C++语言的一个烦恼,由C++/CLI继承。它将类型名称和类成员名称放在同一个符号表中。当您使用 C++/CLI 而不是 C# 或 VB.NET(将类型标识符分开的语言)编写 Winforms 代码时,这是您经常会遇到的问题。

MouseButtons 枚举类型和 Form 类的 MouseButtons 属性之间存在歧义,它们都在此处的范围内。 IntelliSense 不再帮助您获得正确的语法,这可能就是您最终得到的结果。而不是 :: 不再有机会获得像样的编译器错误消息。您可以通过完整地编写枚举类型名称来解决歧义:

     if (e->Button == System::Windows::Forms::MouseButtons::Left) {
         // etc...
     }

像这样的问题可能是 C++/CLI 从未变得非常流行的原因之一。话又说回来,C# 是一种集体行为。受到推崇的。

This is an annoyance of the C++ language, inherited by C++/CLI. It puts the names of types and the names of class members in the same symbol table. This is something you'll battle often when you write Winforms code in C++/CLI instead of C# or VB.NET, languages that keep type identifiers separate.

There's an ambiguity between the MouseButtons enum type and the Form class' MouseButtons property, they are both in scope here. IntelliSense stops helping you to get the syntax right which is probably how you ended up with . instead of :: Leaving no odds anymore to get a decent compiler error message. You resolve the ambiguity by writing the enum type name in full:

     if (e->Button == System::Windows::Forms::MouseButtons::Left) {
         // etc...
     }

Problems like these are probably one reason that C++/CLI never got very popular. Then again, C# is rather a class act. Recommended.

残龙傲雪 2024-10-27 11:56:08

您需要将事件挂接到您想要的控件上:

this->control->MouseDown += new System::Windows::Forms::MouseEventHandler(this, &Form1::control_MouseDown);

并像这样处理它:

void control_MouseDown(Object* sender, System::Windows::Forms::MouseEventArgs* e) {    
    if(e->Button == MouseButtons::Left) {
       //code here
    }
}

You need to hook the event on control you want:

this->control->MouseDown += new System::Windows::Forms::MouseEventHandler(this, &Form1::control_MouseDown);

and handle it like this:

void control_MouseDown(Object* sender, System::Windows::Forms::MouseEventArgs* e) {    
    if(e->Button == MouseButtons::Left) {
       //code here
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文