Holds configurations of a display monitor
 internal static string ArrayToXml(DisplaySetting[] array)
 {
     var serializer = new XmlSerializer(typeof (DisplaySetting[]));
     var sb = new StringBuilder();
     using (var writer = XmlWriter.Create(sb))
     {
         serializer.Serialize(writer, array);
     }
     return sb.ToString();
 }
示例#2
0
 private static void ApplyExtendedSettings()
 {
     Utility.DoTimeout(
         () =>
     {
         System.Windows.Forms.Application.DoEvents();
         var displays = GetDisplays();
         if ((displays.Count > 1) ||
             (displays.Count == 1 && displays[0].Monitor.Top == 0 && displays[0].Monitor.Left == 0 &&
              displays[0].Monitor.Bottom != 0 && displays[0].Monitor.Right != 0 &&
              (displays[0].Monitor.Right / 16 == displays[0].Monitor.Bottom / 9 ||
               displays[0].Monitor.Right / 16 == displays[0].Monitor.Bottom / 10 ||
               displays[0].Monitor.Right / 4 == displays[0].Monitor.Bottom / 3)))
         {
             Thread.Sleep(5000);
             return(true);
         }
         return(false);
     },
         20000,
         500);
     Cleanup();
     System.Windows.Forms.Application.DoEvents();
     if (!string.IsNullOrWhiteSpace(Settings.Default.DisplaySettings))
     {
         Utility.ContinueException(
             () => ApplyDisplaySettings(DisplaySetting.XmlToArray(Settings.Default.DisplaySettings)));
         System.Windows.Forms.Application.DoEvents();
         Thread.Sleep(2000);
     }
     else
     {
         Process.Start(@"control.exe", @"desk.cpl,Settings,@Settings");
         Thread.Sleep(10000);
     }
 }
示例#3
0
        private static bool AutomateSurroundSettings(Application application)
        {
            // Waiting for form to become visible
            var setupDialog =
                Utility.DefaultOnException(
                    () => application.GetWindow(NVidiaLocalization.NVIDIA_Surround_Caption_NVIDIA_Set_Up_Surround));

            if (setupDialog == null)
            {
                return(true); // Control Panel somehow knows the settings
            }

            try
            {
                setupDialog.HideMinimize();
                setupDialog.WaitWhileBusy();
                setupDialog.ShowFocus();
                SplashForm.Instance.Focus();
                System.Windows.Forms.Application.DoEvents();

                var topologyDropdown =
                    Utility.DefaultOnException(() => setupDialog.Get <ComboBox>(SearchCriteria.ByAutomationId("3484")));
                var resolutionDropdown =
                    Utility.DefaultOnException(() => setupDialog.Get <ComboBox>(SearchCriteria.ByAutomationId("3486")));
                var refreshRateDropdown =
                    Utility.DefaultOnException(() => setupDialog.Get <ComboBox>(SearchCriteria.ByAutomationId("3487")));

                setupDialog.HideMinimize();
                setupDialog.WaitWhileBusy();

                // Waiting a little for element to load, if not yet
                var enableButton = setupDialog.GetChildWindowWithControlId <Button>(3493, 5000);

                SplashForm.Instance.Focus();
                System.Windows.Forms.Application.DoEvents();

                var bezel1TextBox = setupDialog.GetChildWindowWithControlId <TextBox>(3506);
                var bezel2TextBox = setupDialog.GetChildWindowWithControlId <TextBox>(3507);
                var bezel3TextBox = setupDialog.GetChildWindowWithControlId <TextBox>(3508);
                var bezel4TextBox = setupDialog.GetChildWindowWithControlId <TextBox>(3509);

                // Lets keep the current display setting before going any further
                Utility.ContinueException(
                    () => Settings.Default.DisplaySettings = DisplaySetting.ArrayToXml(GetDisplaySettings()));

                if (!string.IsNullOrWhiteSpace(Settings.Default.Arrangement))
                {
                    // List of all monitors
                    var displays =
                        setupDialog.GetChildWindowWithControlId <ListView>(3489)
                        .AutomationElement.FindAll(
                            TreeScope.Descendants,
                            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.CheckBox));

                    if (topologyDropdown != null && Settings.Default.Topology > -1)
                    {
                        var topologyDropdownItems = topologyDropdown.Items;
                        if (topologyDropdownItems.Count > Settings.Default.Topology)
                        {
                            topologyDropdownItems[Settings.Default.Topology].Select();
                            setupDialog.WaitWhileBusy();
                            Mouse.Instance.RestoreLocation();
                            SplashForm.Instance.Focus();
                            System.Windows.Forms.Application.DoEvents();
                        }
                    }

                    foreach (var monitor in Settings.Default.Arrangement.Split('|'))
                    {
                        foreach (AutomationElement display in displays)
                        {
                            var cb = new CheckBox(display, null);
                            if (monitor.ToLower().Trim().Contains(cb.Text.ToLower().Trim()))
                            {
                                Utility.DoTimeout(
                                    () =>
                                {
                                    Mouse.Instance.Location = cb.Bounds.Location + new Vector(22, 7);
                                    setupDialog.ExecuteAutomationAction(() => Mouse.Instance.Click());
                                    Mouse.Instance.RestoreLocation();
                                    SplashForm.Instance.Focus();
                                    System.Windows.Forms.Application.DoEvents();
                                    return(cb.Checked);
                                });
                                break;
                            }
                        }
                    }

                    if (bezel1TextBox != null && Settings.Default.Bezel1 > -1)
                    {
                        bezel1TextBox.BulkText = Settings.Default.Bezel1.ToString();
                        setupDialog.WaitWhileBusy();
                    }

                    if (bezel2TextBox != null && Settings.Default.Bezel2 > -1)
                    {
                        bezel2TextBox.BulkText = Settings.Default.Bezel2.ToString();
                        setupDialog.WaitWhileBusy();
                    }

                    if (bezel3TextBox != null && Settings.Default.Bezel3 > -1)
                    {
                        bezel3TextBox.BulkText = Settings.Default.Bezel3.ToString();
                        setupDialog.WaitWhileBusy();
                    }

                    if (bezel4TextBox != null && Settings.Default.Bezel4 > -1)
                    {
                        bezel4TextBox.BulkText = Settings.Default.Bezel4.ToString();
                        setupDialog.WaitWhileBusy();
                    }

                    if (resolutionDropdown != null && Settings.Default.Resolution > -1)
                    {
                        var resolutionDropdownItems = resolutionDropdown.Items;
                        if (resolutionDropdownItems.Count > Settings.Default.Resolution)
                        {
                            resolutionDropdownItems[Settings.Default.Resolution].Select();
                            setupDialog.WaitWhileBusy();
                            Mouse.Instance.RestoreLocation();
                            SplashForm.Instance.Focus();
                            System.Windows.Forms.Application.DoEvents();
                        }
                    }

                    if (refreshRateDropdown != null && Settings.Default.RefreshRate > -1)
                    {
                        var refreshRateDropdownItems = refreshRateDropdown.Items;
                        if (refreshRateDropdownItems.Count > Settings.Default.RefreshRate)
                        {
                            refreshRateDropdownItems[Settings.Default.RefreshRate].Select();
                            setupDialog.WaitWhileBusy();
                            Mouse.Instance.RestoreLocation();
                            SplashForm.Instance.Focus();
                            System.Windows.Forms.Application.DoEvents();
                        }
                    }

                    // Let's see if the provided settings are satisfying
                    if (Utility.DoTimeout(
                            () =>
                    {
                        setupDialog.WaitWhileBusy();
                        return(enableButton.Enabled);
                    }))
                    {
                        var result = Utility.DoTimeout(
                            () =>
                        {
                            setupDialog.ExecuteAutomationAction(
                                () => Utility.ContinueException(() => enableButton.Click()));
                            Mouse.Instance.RestoreLocation();
                            System.Windows.Forms.Application.DoEvents();
                            application.WaitWhileBusy();
                            return(!enableButton.Enabled
                                   ||
                                   enableButton.Text.ToLower()
                                   .Contains(NVidiaLocalization.NVIDIA_Surround_DisableButton_Disable.ToLower()));
                        });
                        Utility.ContinueException(() => setupDialog.Close());
                        if (result)
                        {
                            Settings.Default.Save();
                        }
                        return(result);
                    }
                }

                var arrangementPanel = setupDialog.GetChildWindowWithControlId <Button>(3490, 5000);
                if (arrangementPanel == null)
                {
                    throw new Exception(
                              Language.Surround_Arrangement_panel_is_not_accessible_from_our_side_can_t_save_your_settings_);
                }

                // Show the form and lets user to decide what to do
                setupDialog.ShowTopMost();
                Mouse.Instance.Location = setupDialog.Location
                                          + new Vector(setupDialog.Bounds.Width / 2, setupDialog.Bounds.Height / 2);
                Mouse.Instance.SavePosition();

                while (!setupDialog.IsClosed)
                {
                    try
                    {
                        if (topologyDropdown != null)
                        {
                            Settings.Default.Topology = Methods.SendMessage(topologyDropdown.GetHWnd(), 0x0147, 0, "");
                        }
                        if (resolutionDropdown != null)
                        {
                            Settings.Default.Resolution = Methods.SendMessage(
                                resolutionDropdown.GetHWnd(),
                                0x0147,
                                0,
                                "");
                        }
                        if (refreshRateDropdown != null)
                        {
                            Settings.Default.RefreshRate = Methods.SendMessage(
                                refreshRateDropdown.GetHWnd(),
                                0x0147,
                                0,
                                "");
                        }

                        Utility.ContinueException(
                            () =>
                        {
                            Settings.Default.Bezel1 = (!string.IsNullOrWhiteSpace(bezel1TextBox?.Text))
                                    ? bezel1TextBox.Text.TryParseIntOrDefault()
                                    : 0;
                            Settings.Default.Bezel2 = (!string.IsNullOrWhiteSpace(bezel2TextBox?.Text))
                                    ? bezel2TextBox.Text.TryParseIntOrDefault()
                                    : 0;
                            Settings.Default.Bezel3 = (!string.IsNullOrWhiteSpace(bezel3TextBox?.Text))
                                    ? bezel3TextBox.Text.TryParseIntOrDefault()
                                    : 0;
                            Settings.Default.Bezel4 = (!string.IsNullOrWhiteSpace(bezel4TextBox?.Text))
                                    ? bezel4TextBox.Text.TryParseIntOrDefault()
                                    : 0;
                        });

                        Utility.ContinueException(
                            () =>
                        {
                            if (!arrangementPanel.IsOffScreen)
                            {
                                Settings.Default.Arrangement = string.Join(
                                    "|",
                                    arrangementPanel.AutomationElement.FindAll(
                                        TreeScope.Children,
                                        Condition.TrueCondition)
                                    .Cast <AutomationElement>()
                                    .Select(el => new Panel(el, null))
                                    .OrderBy(panel => panel.Bounds.Right)
                                    .ThenBy(panel => panel.Bounds.Bottom)
                                    .Select(panel => panel.Text));
                            }
                        });

                        setupDialog.WaitWhileBusy();
                        if (
                            enableButton.Text.ToLower()
                            .Contains(NVidiaLocalization.NVIDIA_Surround_DisableButton_Disable.ToLower()))
                        {
                            Settings.Default.Save();
                            setupDialog.Close();
                            System.Windows.Forms.Application.DoEvents();
                            setupDialog.WaitWhileBusy();
                            return(true);
                        }

                        Thread.Sleep(100);
                        setupDialog.WaitWhileBusy();
                        System.Windows.Forms.Application.DoEvents();
                    }
                    catch (Exception)
                    {
                        break;
                    }
                }
                Utility.ContinueException(
                    () =>
                {
                    setupDialog.Close();
                    setupDialog.WaitWhileBusy();
                });
                Settings.Default.Reload();
                return(false);
            }
            catch (Exception)
            {
                setupDialog.Close();
                setupDialog.WaitWhileBusy();
                throw;
            }
        }
        private static bool ApplyDisplaySettings(DisplaySetting[] settings)
        {
            var allDisplays = GetDisplays();
            foreach (var setting in settings)
            {
                allDisplays.Remove(
                    allDisplays.FirstOrDefault(
                        ex => ex.DeviceName.ToLower().Trim() == setting.DisplayName.ToLower().Trim()));
                var flags = ChangeDisplaySettingsFlags.Updateregistry | ChangeDisplaySettingsFlags.Global
                            | ChangeDisplaySettingsFlags.Noreset;
                if (setting.DevMode.Position.X == 0 && setting.DevMode.Position.Y == 0)
                {
                    flags |= ChangeDisplaySettingsFlags.SetPrimary;
                }
                var devMode = setting.DevMode;
                if (Methods.ChangeDisplaySettingsEx(setting.DisplayName, ref devMode, IntPtr.Zero, flags, IntPtr.Zero)
                    != ChangeDisplaySettingsExResults.Successful)
                {
                    return false;
                }
            }

            // Disable missing monitors
            foreach (var display in allDisplays)
            {
                var emptyDev = DevMode.GetEmpty();
                Methods.ChangeDisplaySettingsEx(
                    display.DeviceName,
                    ref emptyDev,
                    IntPtr.Zero,
                    ChangeDisplaySettingsFlags.Updateregistry | ChangeDisplaySettingsFlags.Global
                    | ChangeDisplaySettingsFlags.Noreset,
                    IntPtr.Zero);
            }

            // Apply all settings
            Methods.ChangeDisplaySettingsEx(
                null,
                IntPtr.Zero,
                IntPtr.Zero,
                ChangeDisplaySettingsFlags.Reset,
                IntPtr.Zero);

            return true;
        }