AWnd[] _FindAll(_WndList k, AWnd wParent) { using (k) { using var ab = new Util.ArrayBuilder_ <AWnd>(); _FindInList(wParent, k, w => ab.Add(w)); //CONSIDER: ab could be part of _WndList. Now the delegate creates garbage. return(ab.ToArray()); } }
/// <summary> /// Returns index of matching element or -1. /// Returns -1 if using getAll. /// </summary> /// <param name="wParent">Parent window. Can be default(AWnd) if inList is true and no DirectChild flag and not using winforms name.</param> /// <param name="a">List of AWnd. Does not dispose it.</param> /// <param name="getAll">If not null, calls it for all matching and returns -1.</param> int _FindInList(AWnd wParent, _WndList a, Action <AWnd> 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 AWnd 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 (_nameIs == _NameIs.id) { if (w.ControlId != _id) { 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.label: // s = w.NameLabel; // break; case _NameIs.accName: s = w.NameAcc; break; case _NameIs.wfName: if (_wfControls == null) { try { _wfControls = new AWinFormsControlNames(wParent.Is0 ? w : wParent); } catch (AuWndException) { //invalid parent window return(-1); } catch (AuException e) { //probably process of higher UAC integrity level AWarning.Write($"Failed to get .NET control names. {e.Message}"); return(-1); } } s = _wfControls.GetControlName(w); break; default: Debug.Assert(_nameIs == _NameIs.name); 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); }
/// <summary> /// Finds the specified control in a list of controls. /// Returns 0-based index, or -1 if not found. /// The <see cref="Result"/> property will be the control. /// </summary> /// <param name="a">List of controls, for example returned by <see cref="GetWnd.Children"/>.</param> /// <param name="wParent">Direct or indirect parent window. Used only for flag DirectChild.</param> public int FindInList(IEnumerable <AWnd> a, AWnd wParent = default) { using var k = new _WndList(a); return(_FindInList(wParent, k)); }
/// <summary> /// Finds the specified child control, like <see cref="AWnd.Child"/>. /// Returns true if found. /// The <see cref="Result"/> property will be the control. /// </summary> /// <param name="wParent">Direct or indirect parent window. Can be top-level window or control.</param> /// <exception cref="AuWndException">Invalid wParent.</exception> public bool Find(AWnd wParent) { using var k = new _WndList(_AllChildren(wParent)); return(_FindInList(wParent, k) >= 0); }