public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DeviceMode mode, IntPtr hwnd, uint dwflags, IntPtr lParam);
/// <summary> /// /// </summary> private static void UnloadDriver() { unmapSharedBuffers(); 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 (NativeMethods.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; try { SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, CDS_UPDATEREGISTRY, IntPtr.Zero); SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, 0, IntPtr.Zero); } catch { } }
/// <summary> /// /// </summary> private static void LoadDriver() { 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; if ((_bitmapBpp = deviceMode.dmBitsPerPel) != 32) throw new Exception("Unsupported bitmap format"); 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; String drivers = ""; while (deviceFound = NativeMethods.EnumDisplayDevices(null, deviceIndex, ref device, 0)) { // Trace.WriteLine("DEBUG: Found display driver: " + device.DeviceString + " " + device.DeviceName); if (device.DeviceString == driverName) break; drivers = drivers + " " + device.DeviceString; deviceIndex++; } if (!deviceFound) { // MessageBox.Show("The appropriate mirror driver is not loaded. Only found " + drivers, "FATAL"); throw new Exception("Mirror driver not loaded"); } driverInstanceName = device.DeviceName; const string driverRegistryPath = "SYSTEM\\CurrentControlSet\\Hardware Profiles\\Current\\System\\CurrentControlSet\\Services"; _registryKey = Registry.LocalMachine.OpenSubKey(driverRegistryPath, true); if (_registryKey != null) { _registryKey = _registryKey.CreateSubKey(driverMiniportName); if (_registryKey != null) _registryKey = _registryKey.CreateSubKey(driverDeviceNumber); else throw new Exception("Couldn't open registry key"); } else throw new Exception("Couldn't open registry key"); // _registryKey.SetValue("Cap.DfbBackingMode", 0); // _registryKey.SetValue("Order.BltCopyBits.Enabled", 1); _registryKey.SetValue("Cap.DfbBackingMode", 3); _registryKey.SetValue("Screen.ForcedBpp", 32); _registryKey.SetValue("Pointer.Enabled", 1); _registryKey.SetValue("Attach.ToDesktop", 1); try { SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, CDS_UPDATEREGISTRY, IntPtr.Zero); SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, 0, IntPtr.Zero); } catch { } try { if (!mapSharedBuffers()) throw new InvalidOperationException("mapSharedBuffers failed"); } catch { throw new InvalidOperationException("mapSharedBuffers failed."); } }
/// <summary> /// /// </summary> /// <param name="lpszDeviceName"></param> /// <param name="mode"></param> /// <param name="hwnd"></param> /// <param name="dwflags"></param> /// <param name="lParam"></param> private static void SafeChangeDisplaySettingsEx(string lpszDeviceName, ref DeviceMode mode, IntPtr hwnd, uint dwflags, IntPtr lParam) { int result = NativeMethods.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 } }
/// <summary> /// /// </summary> private static void LoadDriver() { 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; } if ((_bitmapBpp = deviceMode.dmBitsPerPel) != 32) { throw new Exception("Unsupported bitmap format"); } 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; String drivers = ""; while (deviceFound = NativeMethods.EnumDisplayDevices(null, deviceIndex, ref device, 0)) { // Trace.WriteLine("DEBUG: Found display driver: " + device.DeviceString + " " + device.DeviceName); if (device.DeviceString == driverName) { break; } drivers = drivers + " " + device.DeviceString; deviceIndex++; } if (!deviceFound) { // MessageBox.Show("The appropriate mirror driver is not loaded. Only found " + drivers, "FATAL"); throw new Exception("Mirror driver not loaded"); } driverInstanceName = device.DeviceName; const string driverRegistryPath = "SYSTEM\\CurrentControlSet\\Hardware Profiles\\Current\\System\\CurrentControlSet\\Services"; _registryKey = Registry.LocalMachine.OpenSubKey(driverRegistryPath, true); if (_registryKey != null) { _registryKey = _registryKey.CreateSubKey(driverMiniportName); if (_registryKey != null) { _registryKey = _registryKey.CreateSubKey(driverDeviceNumber); } else { throw new Exception("Couldn't open registry key"); } } else { throw new Exception("Couldn't open registry key"); } // _registryKey.SetValue("Cap.DfbBackingMode", 0); // _registryKey.SetValue("Order.BltCopyBits.Enabled", 1); _registryKey.SetValue("Cap.DfbBackingMode", 3); _registryKey.SetValue("Screen.ForcedBpp", 32); _registryKey.SetValue("Pointer.Enabled", 1); _registryKey.SetValue("Attach.ToDesktop", 1); try { SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, CDS_UPDATEREGISTRY, IntPtr.Zero); SafeChangeDisplaySettingsEx(device.DeviceName, ref deviceMode, IntPtr.Zero, 0, IntPtr.Zero); } catch { } try { if (!mapSharedBuffers()) { throw new InvalidOperationException("mapSharedBuffers failed"); } } catch { throw new InvalidOperationException("mapSharedBuffers failed."); } }