将参数与OR(|)运算符组合在一起 [英] Combining Arguments with the OR(|) Operator

查看:143
本文介绍了将参数与OR(|)运算符组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Win32 API(以及其他API')中,当将参数传递给某些函数时,我已经看到并使用了 OR(|)将多个参数合并为一个。



示例:

 CreateWindow(...,
WS_VISIBLE | WS_CHILD | WS_DISABLED
,...);





在上面的函数调用中,它将 WS_VISIBLE,WS_CHILD,WS_DISABLED 组合成一个值。调用这样的函数很好。



问题:

如何 CreateWindow()函数分别捕获那些标志? (我想在我自己的项目中使用此方法

解决方案

这是通过位掩码。查看 WS_VISIBLE WS_CHILD 等的值: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600%28v= vs.85%29.aspx [ ^ ]。



如您所见,每个都是2的顺序。这是,每个人在这个词中只占一位。你总是可以在一个声明中检查一下。考虑:



 BOOL isVisible =(myWindowStyle& WS_VISIBLE)>  0 ; 
BOOL isChild =(myWindowStyle& WS_CHILD)> 0 ;
// ...





比特掩盖技术可能比这复杂得多,但又简单而且非常有效。



参见: http://www.cprogramming.com/tutorial/bitwise_operators.html [ ^ ]。



- SA

In the Win32 API(as well as in other API''s), When passing arguments to some functions, I have seen and used the OR (|) to combine multiple parameters as one.

An Example:

CreateWindow(...,
	     WS_VISIBLE | WS_CHILD | WS_DISABLED
             ,...);



In the above function call it combines WS_VISIBLE, WS_CHILD, WS_DISABLED into one value. Calling a function like that is fine.

Problem:
How does the CreateWindow() function catch those flags separately ? (I would like to use this method in my own projects)

解决方案

This is done with the bit masks. Look at the values of WS_VISIBLE, WS_CHILD, etc.: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600%28v=vs.85%29.aspx[^].

As you can see, each of them is the order of 2. That is, each of them takes exactly one bit in the word. And you can always check a bit in one statement. Consider:

BOOL isVisible = (myWindowStyle & WS_VISIBLE) > 0;
BOOL isChild = (myWindowStyle & WS_CHILD) > 0;
// ...



Bit-masking techniques could be much more complex than that, yet simple enough and very effective.

See also: http://www.cprogramming.com/tutorial/bitwise_operators.html[^].

—SA


这篇关于将参数与OR(|)运算符组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆