conditional-statementscoding-styleautoitbitflags

Conditional statement against _WinAPI_EnumDisplayDevices() -returned bitflag


_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?


Solution

  • 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:

    Dim $x = 0, $y = 0, $_enum, $_PhysMon[15] = [1,2,8,32,3,9,33,10,34,40,11,35,36,42,51], $_DefMon[8] = [2,3,10,34,11,35,42,43]
    Do
        $_enum = _WinAPI_EnumDisplayDevices("", $x)
        $_physCheck = _ArraySearch($_PhysMon, $_enum[3])
        $_defCheck = _ArraySearch($_DefMon, $_enum[3])
        $x+=1
        msgbox(0,"","Phys Check:  " & $_physCheck & @LF & "Def Check:   " & $_defCheck)
        If $_physCheck <> -1 AND %_defCheck <> -1 Then
            msgbox(0,"","Monitor " & $x & " IS THE PHYSICAL DEFAULT MONITOR")
        ElseIf $_physCheck <> -1 Then
            msgbox(0,"","Monitor " & $x & " IS A PHYSICAL MONITOR")
        Else
            msgbox(0,"","Monitor " & $x & " IS A VIRTUAL MONITOR")
        EndIf
    Until NOT $_enum[3]
    

    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.