开发者

Why use bitwise operators for checking mouse clicks?

开发者 https://www.devze.com 2023-01-06 13:40 出处:网络
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

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 & Mouse开发者_如何学CButtons.Right) == 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消