예제 #1
0
 private static void SafeChangeDisplaySettingsEx(string lpszDeviceName, ref DeviceMode mode, IntPtr hwnd, uint dwflags, IntPtr lParam)
 {
     int result = ChangeDisplaySettingsEx(lpszDeviceName, ref mode, hwnd, dwflags, lParam);
     switch (result)
     {
         case 0:
             return; //DISP_CHANGE_SUCCESSFUL
         case 1:
             throw new Exception("The computer must be restarted for the graphics mode to work."); //DISP_CHANGE_RESTART
         case -1:
             throw new Exception("The display driver failed the specified graphics mode."); // DISP_CHANGE_FAILED
         case -2:
             throw new Exception("The graphics mode is not supported."); // DISP_CHANGE_BADMODE
         case -3:
             throw new Exception("Unable to write settings to the registry."); // DISP_CHANGE_NOTUPDATED
         case -4:
             throw new Exception("An invalid set of flags was passed in."); // DISP_CHANGE_BADFLAGS
         case -5:
             throw new Exception("An invalid parameter was passed in. This can include an invalid flag or combination of flags.");
         // DISP_CHANGE_BADPARAM
         case -6:
             throw new Exception("The settings change was unsuccessful because the system is DualView capable."); // DISP_CHANGE_BADDUALVIEW
     }
 }
 private static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DeviceMode mode, IntPtr hwnd, uint dwflags, IntPtr lParam);
        public void Unload()
        {
            if (State == MirrorState.Running)
                Stop();
            if (State == MirrorState.Connected)
                Disconnect();

            if (State != MirrorState.Loaded)
                return;

            var deviceMode = new DeviceMode();
            deviceMode.dmSize = (short)Marshal.SizeOf(typeof(DeviceMode));
            deviceMode.dmDriverExtra = 0;
            deviceMode.dmFields = (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_POSITION);

            var device = new DisplayDevice();
            device.CallBack = Marshal.SizeOf(device);
            deviceMode.dmDeviceName = string.Empty;
            uint deviceIndex = 0;
            while (EnumDisplayDevices(null, deviceIndex, ref device, 0))
            {
                if (device.DeviceString.Equals(driverName))
                    break;

                deviceIndex++;
            }

            Debug.Assert(_registryKey != null);

            _registryKey.SetValue("Attach.ToDesktop", 0);
            _registryKey.Close();

            deviceMode.dmDeviceName = driverMiniportName;

            if (deviceMode.dmBitsPerPel == 24) deviceMode.dmBitsPerPel = 32;

            #region This was CommitDisplayChanges

            SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, CDS_UPDATEREGISTRY, IntPtr.Zero);
            SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, 0, IntPtr.Zero);

            #endregion

            State = MirrorState.Idle;
        }
        public bool Load()
        {
            if (State != MirrorState.Idle)
                throw new InvalidOperationException("You may call Load only if the state is Idle");

            var device = new DisplayDevice();
            var deviceMode = new DeviceMode { dmDriverExtra = 0 };

            device.CallBack = Marshal.SizeOf(device);
            deviceMode.dmSize = (short)Marshal.SizeOf(deviceMode);
            deviceMode.dmBitsPerPel = Screen.PrimaryScreen.BitsPerPixel;

            if (deviceMode.dmBitsPerPel == 24)
                deviceMode.dmBitsPerPel = 32;

            _bitmapBpp = deviceMode.dmBitsPerPel;

            deviceMode.dmDeviceName = string.Empty;
            deviceMode.dmFields = (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_POSITION);
            _bitmapHeight = deviceMode.dmPelsHeight = Screen.PrimaryScreen.Bounds.Height;
            _bitmapWidth = deviceMode.dmPelsWidth = Screen.PrimaryScreen.Bounds.Width;

            bool deviceFound;
            uint deviceIndex = 0;

            while (deviceFound = EnumDisplayDevices(null, deviceIndex, ref device, 0))
            {
                if (device.DeviceString == driverName)
                    break;
                deviceIndex++;
            }

            if (!deviceFound) return false;

            driverInstanceName = device.DeviceName;

            _registryKey = Registry.LocalMachine.OpenSubKey(driverRegistryPath, true);
            if (_registryKey != null)
                _registryKey = _registryKey.CreateSubKey(driverMiniportName);
            else
                throw new Exception("Couldn't open registry key");

            if (_registryKey != null)
                _registryKey = _registryKey.CreateSubKey(driverDeviceNumber);
            else
                throw new Exception("Couldn't open registry key");

            //			_registryKey.SetValue("Cap.DfbBackingMode", 0);
            _registryKey.SetValue("Screen.ForcedBpp", 24);
            _registryKey.SetValue("Attach.ToDesktop", 1);

            #region This was CommitDisplayChanges

            SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, CDS_UPDATEREGISTRY, IntPtr.Zero);
            SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, 0, IntPtr.Zero);

            #endregion

            State = MirrorState.Loaded;

            return true;
        }
        public bool DriverExists()
        {
            var device = new DisplayDevice();
            var deviceMode = new DeviceMode {dmDriverExtra = 0};

            device.CallBack = Marshal.SizeOf(device);
            deviceMode.dmSize = (short) Marshal.SizeOf(deviceMode);
            deviceMode.dmBitsPerPel = Screen.PrimaryScreen.BitsPerPixel;

            if (deviceMode.dmBitsPerPel == 24)
                deviceMode.dmBitsPerPel = 32;

            _bitmapBpp = deviceMode.dmBitsPerPel;

            deviceMode.dmDeviceName = string.Empty;
            deviceMode.dmFields = (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_POSITION);
            _bitmapHeight = deviceMode.dmPelsHeight = Screen.PrimaryScreen.Bounds.Height;
            _bitmapWidth = deviceMode.dmPelsWidth = Screen.PrimaryScreen.Bounds.Width;

            bool deviceFound;
            uint deviceIndex = 0;

            while (deviceFound = EnumDisplayDevices(null, deviceIndex, ref device, 0))
            {
                if (device.DeviceString == driverName)
                    break;
                deviceIndex++;
            }

            if (!deviceFound) return false;
            else return true;
        }
예제 #6
0
 private static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DeviceMode mode, IntPtr hwnd, uint dwflags, IntPtr lParam);
예제 #7
0
        public bool Load()
        {
            if (State != MirrorState.Idle)
            {
                throw new InvalidOperationException("You may call Load only if the state is Idle");
            }

            var device     = new DisplayDevice();
            var deviceMode = new DeviceMode {
                dmDriverExtra = 0
            };

            device.CallBack         = Marshal.SizeOf(device);
            deviceMode.dmSize       = (short)Marshal.SizeOf(deviceMode);
            deviceMode.dmBitsPerPel = Screen.PrimaryScreen.BitsPerPixel;

            if (deviceMode.dmBitsPerPel == 24)
            {
                deviceMode.dmBitsPerPel = 32;
            }

            _bitmapBpp = deviceMode.dmBitsPerPel;

            deviceMode.dmDeviceName = string.Empty;
            deviceMode.dmFields     = (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_POSITION);
            _bitmapHeight           = deviceMode.dmPelsHeight = Screen.PrimaryScreen.Bounds.Height;
            _bitmapWidth            = deviceMode.dmPelsWidth = Screen.PrimaryScreen.Bounds.Width;

            bool deviceFound;
            uint deviceIndex = 0;

            while (deviceFound = EnumDisplayDevices(null, deviceIndex, ref device, 0))
            {
                if (device.DeviceString == driverName)
                {
                    break;
                }
                deviceIndex++;
            }

            if (!deviceFound)
            {
                return(false);
            }

            driverInstanceName = device.DeviceName;

            _registryKey = Registry.LocalMachine.OpenSubKey(driverRegistryPath, true);
            if (_registryKey != null)
            {
                _registryKey = _registryKey.CreateSubKey(driverMiniportName);
            }
            else
            {
                throw new Exception("Couldn't open registry key");
            }

            if (_registryKey != null)
            {
                _registryKey = _registryKey.CreateSubKey(driverDeviceNumber);
            }
            else
            {
                throw new Exception("Couldn't open registry key");
            }

            //			_registryKey.SetValue("Cap.DfbBackingMode", 0);
            _registryKey.SetValue("Screen.ForcedBpp", 24);
            _registryKey.SetValue("Attach.ToDesktop", 1);

            #region This was CommitDisplayChanges

            SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, CDS_UPDATEREGISTRY, IntPtr.Zero);
            SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, 0, IntPtr.Zero);

            #endregion

            State = MirrorState.Loaded;

            return(true);
        }