针对 _WinAPI_EnumDisplayDevices() 的条件语句 - 返回位标志
_WinAPI_EnumDisplayDevices()
报告 3 个额外的虚拟我不需要的监视器。因此,我创建了一个 If 语句,如果出现特定标志(例如 1、2、3、35 或 33),它只返回这些监视器。然而,它让我困扰我的条件语句有多长:
$_enum = _WinAPI_EnumDisplayDevices("", $x)
If $_enum[3] = 1 OR $_enum[3] = 2 OR $_enum[3] = 3 OR $_enum[3] = 33 OR $_enum[3] = 35 Then
如何用更少的代码获得相同的结果?
_WinAPI_EnumDisplayDevices()
reports 3 additional virtual monitors I don't need. So I've created an If statement where if specific flags come about (such as 1, 2, 3, 35, or 33), it only returns those monitors. However, it bugs me how long my conditional statement is:
$_enum = _WinAPI_EnumDisplayDevices("", $x)
If $_enum[3] = 1 OR $_enum[3] = 2 OR $_enum[3] = 3 OR $_enum[3] = 33 OR $_enum[3] = 35 Then
How to get same results with less code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该注意这些标志的含义。查看 _WinAPI_EnumDisplayDevices 下的示例。
本质上,当您检查标志 1、2、3、33 或 35 时。您实际上只是检查标志 1、2 和 32。其中“3 = 2 + 1”和“35 = 32 + 2 + 1”和“33 = 32 + 1”。使用 BitAnd 函数,您可以更轻松地检查这些。
你的条件将变成:
That is a a bit less,但如果其他人正在阅读该应用程序,他仍然不会理解它。您可以通过注释或将条件移至新函数来解决此问题。这是一个带有函数的示例:
我选择了 isValidMonitor 名称,因为我不太确定您的代码的用途。也许更好的名称是“isPrimaryDesktop()”,但是我会删除对标志 32 的检查。不过,您可以看到您的代码立即更具可读性。
You should take note of what the flags mean. Check out the example under _WinAPI_EnumDisplayDevices.
In essence when you are checking for flags 1, 2, 3, 33 or 35. You're only really checking for flags 1, 2 and 32. Where "3 = 2 + 1" and "35 = 32 + 2 + 1" and "33 = 32 + 1". With the BitAnd function you can more easily check for these.
Your conditional will become:
That's a bit shorter, but if someone else is reading the application he still won't make much sense of it. You can solve this with a comment or by moving the conditional to a new function. Here's an example with a function:
I chose the isValidMonitor name because I'm not really sure what your code is meant to do. Maybe a better name is "isPrimaryDesktop()" but then I'd remove the check for flag 32. You can see, though, that your code is instantly more readable.
发现
_ArraySearch()
允许搜索数组中的值,并根据找到或未找到的内容返回不同的标志。因此,我可以创建一个包含所有值的数组,然后针对该数组对$_enum[3]
执行_ArraySearch()
。我最终得到:
我已经设置了所有可能的标志组合,成功解析了哪些监视器是真实的(物理)与非真实的(虚拟),甚至还定义了机器的默认显示。
Discovered
_ArraySearch()
allows to search a value in an array, and returns different flags based on what it finds or doesn't. So I can create an array with all values, then perform an_ArraySearch()
for$_enum[3]
against the array.I ended up with:
I've set all possible combinations of flags, successfully parsed what monitors are real (physical) vs not real (virtual) and even defined the machine's default display as well.