为什么使用按位运算符来检查鼠标点击?

发布于 2024-09-09 07:09:06 字数 205 浏览 1 评论 0原文

我通常编写以下内容来处理鼠标右键单击。

if (e.Button == MouseButtons.Right) { 但是

,我见过人们这样做。有人能告诉我为什么他们这样做吗?有什么好处?

if ((e.Button & MouseButtons.Right) == MouseButtons.Right) { }

I usually write the following to handle a right mouse click.

if (e.Button == MouseButtons.Right)
{
}

But, I have seen people do it this way. Can somebody tell me why they do it this way? What's the advantage?

if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
{
}

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

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

发布评论

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

评论(1

娇妻 2024-09-16 07:09:06

我不认为有任何理由在 MouseDown 事件处理程序中使用 (e.Button & MouseButtons.Right) == MouseButtons.Right 表达式,但它更有意义MouseMove 事件处理程序。当用户将光标移动到按下多个按钮的控件上时,此代码会检测是否按下了鼠标右键(也可以按下其他按钮),而 e.Button == MouseButtons.Right 表示仅按下右键被按下。

I don't see any reason to use (e.Button & MouseButtons.Right) == MouseButtons.Right expression in MouseDown event handler, but it makes more sense in MouseMove event handler. When user moves cursor over control with several buttons pressed, this code detects if right mouse button pressed (other buttons can also be pressed), while e.Button == MouseButtons.Right means that only the right button is pressed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文