You can use IUIAutomation
Test in C# WinForms on Windows 10 ( add a button for the Click) :
// Add reference to : c:\Windows\system32\UIAutomationCore.dll
// Add : using UIAutomationClient;
public partial class Form1 : Form
{
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr hWndTray = FindWindow("Shell_TrayWnd", null);
IntPtr hWndTrayNotify = FindWindowEx(hWndTray, IntPtr.Zero, "TrayNotifyWnd", null);
IntPtr hWndSysPager = FindWindowEx(hWndTrayNotify, IntPtr.Zero, "SysPager", null);
IntPtr hWndToolbar = FindWindowEx(hWndSysPager, IntPtr.Zero, "ToolbarWindow32", null);
IntPtr hWndRebar = FindWindowEx(hWndTray, IntPtr.Zero, "ReBarWindow32", null);
IntPtr hWndMSTaskSwWClass = FindWindowEx(hWndRebar, IntPtr.Zero, "MSTaskSwWClass", null);
IntPtr hWndMSTaskListWClass = FindWindowEx(hWndMSTaskSwWClass, IntPtr.Zero, "MSTaskListWClass", null);
IUIAutomation pUIAutomation = new CUIAutomation();
// Taskbar
IUIAutomationElement windowElement = pUIAutomation.ElementFromHandle(hWndMSTaskListWClass);
if (windowElement != null)
{
IUIAutomationElementArray elementArray = null;
IUIAutomationCondition condition = pUIAutomation.CreateTrueCondition();
elementArray = windowElement.FindAll(TreeScope.TreeScope_Descendants | TreeScope.TreeScope_Children, condition);
if (elementArray != null)
{
Console.WriteLine("Taskbar");
int nNbItems = elementArray.Length;
for (int nItem = 0; nItem <= nNbItems - 1; nItem++)
{
IUIAutomationElement element = elementArray.GetElement(nItem);
string sName = element.CurrentName;
string sAutomationId = element.CurrentAutomationId;
tagRECT rect = element.CurrentBoundingRectangle;
Console.WriteLine("\tName : {0} - AutomationId : {1} - Rect({2}, {3}, {4}, {5})", sName, sAutomationId, rect.left, rect.top, rect.right, rect.bottom);
}
}
}
// Tray icons
IUIAutomationElement windowElementTray = pUIAutomation.ElementFromHandle(hWndToolbar);
if(windowElementTray != null)
{
IUIAutomationElementArray elementArray = null;
IUIAutomationCondition condition = pUIAutomation.CreateTrueCondition();
elementArray = windowElementTray.FindAll(TreeScope.TreeScope_Descendants | TreeScope.TreeScope_Children, condition);
if (elementArray != null)
{
Console.WriteLine("Tray Icons");
int nNbItems = elementArray.Length;
for (int nItem = 0; nItem <= nNbItems - 1; nItem++)
{
IUIAutomationElement element = elementArray.GetElement(nItem);
string sName = element.CurrentName;
string sAutomationId = element.CurrentAutomationId;
tagRECT rect = element.CurrentBoundingRectangle;
Console.WriteLine("\tName : {0} - AutomationId : {1} - Rect({2}, {3}, {4}, {5})", sName, sAutomationId, rect.left, rect.top, rect.right, rect.bottom);
}
}
}
}
}