How to detect a click into the taskbar window button thumbnail?

Leonardo 91 Reputation points

How i could get the title or HWND of the window in which her taskbar button thumbnail is being clicked?

Using CBTProc i listen to HCB_ACTIVATE and i call UIAutomation reading ElementFromPoint
the problem is... sometimes it works to get the title and sometimes it get another thing than the title
when this happens title are blank and classname is SysListView32.

I think this happens when the call to HCB_ACTIVATE is received and the thumbnail already disappeared, then ElementFromPoint
get another thing than the window thumbnail.

I would like to not use a global hook in CBT, there's any other way to achieve this task?

std::wstring AccWindowFromPoint()  
	BSTR name = NULL;  
	if (SUCCEEDED(CoInitialize(NULL)))  
		CComPtr<IUIAutomation> automation;  
		if (SUCCEEDED(automation.CoCreateInstance(CLSID_CUIAutomation8))) // or CLSID_CUIAutomation  
			POINT pos{};  
			CComPtr<IUIAutomationElement> pElement;  
			if (SUCCEEDED(automation->ElementFromPoint(pos, &pElement)))  
				UIA_HWND uia_hwnd = nullptr;  
				BSTR classname = nullptr;  
				RECT rect;  
					"\nname: ", name,  
					"\nclassname: ", classname, "\n");  
				Print("[AccWindowFromPoint] failed to get ElementFromPoint\n");  
			Print("[AccWindowFromPoint] failed to CoCreateInstance\n");  
		Print("[AccWindowFromPoint] failed to CoInitialize\n");  
	if (name == NULL)  
		return L"";  
	std::wstring wstrName(name);  
	return wstrName;  
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,410 questions
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,514 questions
{count} votes

0 additional answers

Sort by: Most helpful