An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
You can use UIAutomation to identify the main Team window
Test with version 1.3.00.28779 and Automation ID = "teams-app-bar" (check the ID with MS Inspect tool if not found)
// Add reference to : c:\Windows\system32\UIAutomationCore.dll
// Add : using UIAutomationClient
// Add : using System.Runtime.InteropServices;
Global
IUIAutomation pUIAutomation = new CUIAutomation();
Main code
IntPtr hWndMain = IntPtr.Zero;
int nPID = 0;
int nThreadID = 0;
Process[] procs = Process.GetProcessesByName("teams");
if (procs.Count() > 0)
{
foreach (Process proc in procs)
{
if ((proc.MainWindowHandle != IntPtr.Zero))
{
hWndMain = proc.MainWindowHandle;
nPID = proc.Id;
nThreadID = Process.GetProcessById(nPID).Threads[0].Id;
break;
}
}
}
if (nThreadID != 0)
{
EnumCallBack Callback = new EnumCallBack(ListWindows);
EnumThreadWindows(nThreadID, Callback, IntPtr.Zero);
}
Callback
public bool ListWindows(IntPtr hWnd, ref IntPtr lParam)
{
IUIAutomationElement teamAppbar = null;
if (IsWindowVisible(hWnd))
{
IUIAutomationElement windowElement = pUIAutomation.ElementFromHandle(hWnd);
if (windowElement != null)
{
IUIAutomationElementArray elementArray = null;
IUIAutomationCondition condition = null;
condition = pUIAutomation.CreateTrueCondition();
elementArray = windowElement.FindAll(TreeScope.TreeScope_Descendants, condition);
if (elementArray != null)
{
int nNbItems = elementArray.Length;
for (int nItem = 0; nItem < nNbItems; nItem++)
{
IUIAutomationElement element = elementArray.GetElement(nItem);
string sName = element.CurrentName;
string sAutomationId = element.CurrentAutomationId;
//Console.WriteLine("Name : {0} - AutomationId : {1}", sName, sAutomationId);
if (sAutomationId == "teams-app-bar")
{
teamAppbar = element;
break;
}
}
}
}
StringBuilder sbText = new StringBuilder(260);
GetWindowText(hWnd, sbText, sbText.Capacity);
string sWindow = null;
if (teamAppbar != null)
sWindow = "Main window";
else
sWindow = "Secondary window";
Debug.WriteLine(string.Format("{0} - Window : {1}", sWindow, sbText.ToString()));
}
return true;
}
Declarations :
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool EnumThreadWindows(int dwThreadId, EnumCallBack lpfn, IntPtr lParam);
public delegate bool EnumCallBack(IntPtr handle, ref IntPtr lParam);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool IsWindowVisible(IntPtr hWnd);