public static ScreenRect GetWindowTaskbarButtonRect(string windowName, IntPtr taskbarShellTry /* HWND shall try target*/) { IntPtr hWndMSTaskListWClass = ScanChildrenForWindow(taskbarShellTry, className: "MSTaskListWClass"); if (hWndMSTaskListWClass == IntPtr.Zero) return ScreenRect.Zero; IUIAutomation pUIAutomation = new CUIAutomation(); IUIAutomationElement windowElement = pUIAutomation.ElementFromHandle(hWndMSTaskListWClass); if (windowElement != null) { IUIAutomationCondition condition = pUIAutomation.CreateTrueCondition(); IUIAutomationElementArray elementArray = windowElement.FindAll(TreeScope.TreeScope_Descendants | TreeScope.TreeScope_Children, condition); if (elementArray != null) { //find matching elements by process List elementsPerProc = new(); string currentExe = System.AppDomain.CurrentDomain.FriendlyName; int nNbItems = elementArray.Length; for (int nItem = 0; nItem <= nNbItems - 1; nItem++) { IUIAutomationElement element = elementArray.GetElement(nItem); string sName = element.CurrentName; if (sName.Contains("running window")) { if (element.CurrentAutomationId.Contains(currentExe)) { elementsPerProc.Add(element); } } } //try find matching element by window title if (elementsPerProc.Count >= 1) { tagRECT rect = elementsPerProc[0].CurrentBoundingRectangle; ScreenRect result = new(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); foreach (IUIAutomationElement el in elementsPerProc) { if (el.CurrentName.Contains(windowName)) { IntPtr hwnd = el.CurrentNativeWindowHandle; tagRECT rect2 = el.CurrentBoundingRectangle; result = new ScreenRect(rect2.left, rect2.top, rect2.right - rect2.left, rect2.bottom - rect2.top); break; } } return result; } } } return ScreenRect.Zero; }