Example #1
0
        public bool  XDD_GetActiveAttachedMonitor(
            ref DISPLAY_DEVICE ddMonitor                // 输出ddMonitor信息
            )
        {
            // 枚举Adapter下Monitor用变量
            int            dwMonitorIndex = 0;
            DISPLAY_DEVICE ddMonEmpty     = new DISPLAY_DEVICE();
            DISPLAY_DEVICE ddMonTmp       = new DISPLAY_DEVICE();

            // 枚举Adapter
            int            dwAdapterIndex = 0;
            DISPLAY_DEVICE ddAdapter      = new DISPLAY_DEVICE();

            ddAdapter.cb = System.Runtime.InteropServices.Marshal.SizeOf(typeof(DISPLAY_DEVICE));

            while (WindowsAPI.EnumDisplayDevices(null, dwAdapterIndex, ref ddAdapter, 0) != false)
            {
                // 枚举该Adapter下的Monitor
                dwMonitorIndex = 0;
                ddMonTmp       = ddMonEmpty;
                ddMonTmp.cb    = System.Runtime.InteropServices.Marshal.SizeOf(typeof(DISPLAY_DEVICE));

                while (WindowsAPI.EnumDisplayDevices(ddAdapter.DeviceName, dwMonitorIndex, ref ddMonTmp, 0) != false)
                {
                    // 判断状态是否正确
                    if ((ddMonTmp.StateFlags & CommonConst.DISPLAY_DEVICE_ACTIVE) == CommonConst.DISPLAY_DEVICE_ACTIVE &&
                        (ddMonTmp.StateFlags & CommonConst.DISPLAY_DEVICE_ATTACHED) == CommonConst.DISPLAY_DEVICE_ATTACHED
                        )
                    {
                        ddMonitor = ddMonTmp;
                        return(true);
                    }

                    // 下一个Monitor
                    dwMonitorIndex += 1;
                    ddMonTmp        = ddMonEmpty;
                    ddMonTmp.cb     = System.Runtime.InteropServices.Marshal.SizeOf(typeof(DISPLAY_DEVICE));
                }

                // 下一个Adapter
                dwAdapterIndex += 1;
                ddAdapter       = ddMonEmpty;
                ddAdapter.cb    = System.Runtime.InteropServices.Marshal.SizeOf(typeof(DISPLAY_DEVICE));
            }

            // 未枚举到满足条件的Monitor
            return(false);
        }