コード例 #1
0
        static void Main(string[] args)
        {
            try
            {
                var process = Process.Start(@"C:\Windows\system32\control.exe", @"/name Microsoft.DefaultPrograms /page pageDefaultProgram\pageAdvancedSettings?pszAppName=google%20chrome");
                var exited  = process.WaitForExit(3000);
                if (!exited)
                {
                    process.Kill();
                    throw new Exception("Control panel process didn't exit");
                }

                var uiAutomation = new CUIAutomation();

                var window = FindAndWait(uiAutomation.GetRootElement(), TreeScope.TreeScope_Children, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Set Program Associations"), TimeSpan.FromSeconds(3));
                try
                {
                    //Sadly, the window can't be hidden or moved to another desktop since doing so results in UI Automation not seeing it.

                    var listView = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Program Association List"), TimeSpan.FromSeconds(3));
                    var save     = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Save"), TimeSpan.FromSeconds(3));

                    var listItems = new[]
                    {
                        ".htm",
                        ".html",
                        "HTTP",
                        "HTTPS",
                    }.Select(name => FindAndWait(
                                 listView,
                                 TreeScope.TreeScope_Descendants,
                                 uiAutomation.CreateAndCondition(
                                     uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, name),
                                     uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId)
                                     ),
                                 TimeSpan.FromSeconds(3)
                                 )).ToArray();

                    foreach (var listItem in listItems)
                    {
                        var toggle = ((IUIAutomationTogglePattern)listItem.GetCurrentPattern(UIA_PatternIds.UIA_TogglePatternId));
                        while (toggle.CurrentToggleState != ToggleState.ToggleState_On) //TODO: Watch out for infinite looping here!
                        {
                            toggle.Toggle();
                        }
                    }

                    ((IUIAutomationInvokePattern)save.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke();
                }
                finally
                {
                    ((IUIAutomationWindowPattern)window.GetCurrentPattern(UIA_PatternIds.UIA_WindowPatternId)).Close();
                }
            }
            catch //Ensures all nested finally blocks execute
            {
                throw;
            }
        }
コード例 #2
0
        /// <summary>
        /// Get DesktopElement based on Process Id.
        /// </summary>
        /// <param name="pid"></param>
        /// <returns>return null if fail to get an element by process Id</returns>
        public static DesktopElement ElementFromProcessId(int pid)
        {
            IUIAutomationElement   root      = null;
            DesktopElement         element   = null;
            IUIAutomationCondition condition = null;

            try
            {
                // check whether process exist first.
                // if not, it will throw an ArgumentException
                using (var proc = Process.GetProcessById(pid))
                {
                    root      = UIAutomation.GetRootElement();
                    condition = UIAutomation.CreatePropertyCondition(PropertyType.UIA_ProcessIdPropertyId, pid);
                    var uia = root.FindFirst(TreeScope.TreeScope_Descendants, condition);
                    element = ElementFromUIAElement(uia);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                // report and let it return null
                ex.ReportException();
            }
#pragma warning restore CA1031 // Do not catch general exception types
            finally
            {
                if (root != null)
                {
                    Marshal.ReleaseComObject(root);
                }
                if (condition != null)
                {
                    Marshal.ReleaseComObject(condition);
                }
            }
            return(element);
        }
コード例 #3
0
        /// <summary>
        /// Get DesktopElement based on Process Id.
        /// </summary>
        /// <param name="pid"></param>
        /// <returns>return null if fail to get an element by process Id</returns>
        public static DesktopElement ElementFromProcessId(int pid)
        {
            IUIAutomationElement   root      = null;
            DesktopElement         element   = null;
            IUIAutomationCondition condition = null;

            try
            {
                // check whether process exist first.
                // if not, it will throw an ArgumentException
                using (var proc = Process.GetProcessById(pid))
                {
                    root      = UIAutomation.GetRootElement();
                    condition = UIAutomation.CreatePropertyCondition(PropertyType.UIA_ProcessIdPropertyId, pid);
                    var uia = root.FindFirst(TreeScope.TreeScope_Descendants, condition);
                    element = ElementFromUIAElement(uia);
                }
            }
            catch (Exception ex)
            {
                //silent and let it return null
                Debug.WriteLine(ex);
            }
            finally
            {
                if (root != null)
                {
                    Marshal.ReleaseComObject(root);
                }
                if (condition != null)
                {
                    Marshal.ReleaseComObject(condition);
                }
            }
            return(element);
        }
コード例 #4
0
        static public MSGF_Data ParseMineSweeperGameField()
        {
            MSGF_Data result = new MSGF_Data();

            result.Xcells = 0;

            appElement = _automation.ElementFromHandle(MineSweeperHWND);

            var array_rows = appElement.FindAll(TreeScope.TreeScope_Children, _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_CustomControlTypeId));

            if (array_rows.Length > 1)
            {
                result.RowsCount = array_rows.Length - 1;

                /*var elem_texts = array_rows.GetElement(0).FindAll(TreeScope.TreeScope_Children, _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_TextControlTypeId));
                 * if (elem_texts.Length == 2)
                 * {
                 *  result.TimeSecs = Convert.ToInt32(elem_texts.GetElement(0).GetCurrentPropertyValue(UIA_PropertyIds.UIA_ValueValuePropertyId));
                 *  result.RemainingMines = Convert.ToInt32(elem_texts.GetElement(1).GetCurrentPropertyValue(UIA_PropertyIds.UIA_ValueValuePropertyId));
                 * }*/

                // Ищем ячейки
                for (int j = 1; j < array_rows.Length; j++)
                {
                    var elem_row    = array_rows.GetElement(j);
                    var array_cells = elem_row.FindAll(TreeScope.TreeScope_Children, _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ButtonControlTypeId));

                    if (j == 1)
                    {
                        result.ColsCount = array_cells.Length;
                        result.cells     = new MSGF_Cell[result.ColsCount, result.RowsCount];
                    }

                    for (int i = 0; i < array_cells.Length; i++)
                    {
                        {
                            string nm = array_cells.GetElement(i).CurrentName;
                            result.cells[i, j - 1].elem = array_cells.GetElement(i); // Runtime Error Out of range


                            if (nm.Contains("помечена"))
                            {
                                result.cells[i, j - 1].state = -2; continue;
                            }
                            if (nm.Contains("неоткрыта"))
                            {
                                result.cells[i, j - 1].state = -1; result.Xcells++; continue;
                            }
                            if (nm.Contains("мин нет"))
                            {
                                result.cells[i, j - 1].state = 0; continue;
                            }
                            if (nm.Contains("ячейки: 1"))
                            {
                                result.cells[i, j - 1].state = 1; continue;
                            }
                            if (nm.Contains("ячейки: 2"))
                            {
                                result.cells[i, j - 1].state = 2; continue;
                            }
                            if (nm.Contains("ячейки: 3"))
                            {
                                result.cells[i, j - 1].state = 3; continue;
                            }
                            if (nm.Contains("ячейки: 4"))
                            {
                                result.cells[i, j - 1].state = 4; continue;
                            }
                            if (nm.Contains("ячейки: 5"))
                            {
                                result.cells[i, j - 1].state = 5; continue;
                            }
                            if (nm.Contains("ячейки: 6"))
                            {
                                result.cells[i, j - 1].state = 6; continue;
                            }
                            if (nm.Contains("ячейки: 7"))
                            {
                                result.cells[i, j - 1].state = 7; continue;
                            }
                            if (nm.Contains("ячейки: 8"))
                            {
                                result.cells[i, j - 1].state = 8; continue;
                            }
                        }
                    }
                }
            }

            return(result);
        }