Ejemplo n.º 1
0
 /// <summary>
 /// Gets the programming name of a Windows Forms control.
 /// Returns null if it is not a Windows Forms control or if fails.
 /// </summary>
 /// <param name="c">The control. Can be top-level window too.</param>
 /// <remarks>This function is easy to use and does not throw excaptions. However, when you need names of multiple controls of a single window, better create a WinformsControlNames instance (once) and for each control call its GetControlNameOrText method, it will be faster.</remarks>
 public static string GetSingleControlName(wnd c)
 {
     if (!IsWinformsControl(c))
     {
         return(null);
     }
     try {
         using (var x = new WinformsControlNames(c)) return(x.GetControlName(c));
     }
     catch { }
     return(null);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns index of matching element or -1.
        /// Returns -1 if using getAll.
        /// </summary>
        /// <param name="wParent">Parent window. Can be default(wnd) if inList is true and no DirectChild flag and not using winforms name.</param>
        /// <param name="a">List of wnd. Does not dispose it.</param>
        /// <param name="getAll">If not null, calls it for all matching and returns -1.</param>
        int _FindInList(wnd wParent, WndList_ a, Action <wnd> getAll = null)
        {
            Result = default;
            if (a.Type == WndList_.ListType.None)
            {
                return(-1);
            }
            bool inList    = a.Type != WndList_.ListType.ArrayBuilder;
            int  skipCount = _skipCount;

            try {             //will need to dispose something
                for (int index = 0; a.Next(out wnd w); index++)
                {
                    if (w.Is0)
                    {
                        continue;
                    }

                    if (inList)                       //else the enum function did this
                    {
                        if (!_flags.Has(WCFlags.HiddenToo))
                        {
                            if (!w.IsVisibleIn_(wParent))
                            {
                                continue;
                            }
                        }

                        if (_flags.Has(WCFlags.DirectChild) && !wParent.Is0)
                        {
                            if (w.ParentGWL_ != wParent)
                            {
                                continue;
                            }
                        }
                    }

                    if (_id != null)
                    {
                        if (w.ControlId != _id.Value)
                        {
                            continue;
                        }
                    }

                    if (_className != null)
                    {
                        if (!_className.Match(w.ClassName))
                        {
                            continue;
                        }
                    }

                    if (_name != null)
                    {
                        string s;
                        switch (_nameIs)
                        {
                        case _NameIs.text:
                            s = w.ControlText;
                            break;

                        case _NameIs.elmName:
                            s = w.NameElm;
                            break;

                        case _NameIs.wfName:
                            if (_wfControls == null)
                            {
                                try {
                                    _wfControls = new WinformsControlNames(wParent.Is0 ? w : wParent);
                                }
                                catch (AuWndException) {                                 //invalid parent window
                                    return(-1);
                                }
                                catch (AuException e) {                                 //probably process of higher UAC integrity level
                                    print.warning($"Failed to get winforms control names. {e.Message}");
                                    return(-1);
                                }
                            }
                            s = _wfControls.GetControlName(w);
                            break;

                        //case _NameIs.label:
                        //	s = w.NameLabel;
                        //	break;
                        default:
                            s = w.Name;
                            break;
                        }

                        if (!_name.Match(s))
                        {
                            continue;
                        }
                    }

                    if (_also != null && !_also(w))
                    {
                        continue;
                    }

                    if (getAll != null)
                    {
                        getAll(w);
                        continue;
                    }

                    if (skipCount-- > 0)
                    {
                        continue;
                    }

                    Result = w;
                    return(index);
                }
            }
            finally {
                if (_wfControls != null)
                {
                    _wfControls.Dispose(); _wfControls = null;
                }
            }

            return(-1);
        }