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

Leonardo 91 Reputation points
2022-08-16T13:49:50.853+00:00

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{};  
			GetCursorPos(&pos);  
  
			CComPtr<IUIAutomationElement> pElement;  
			if (SUCCEEDED(automation->ElementFromPoint(pos, &pElement)))  
			{  
				pElement->get_CurrentName(&name);  
  
				UIA_HWND uia_hwnd = nullptr;  
				pElement->get_CurrentNativeWindowHandle(&uia_hwnd);  
				BSTR classname = nullptr;  
				pElement->get_CurrentClassName(&classname);  
  
				RECT rect;  
				pElement->get_CurrentBoundingRectangle(&rect);  
  
				Print("[accwindowfrompoint]"  
					"\nname: ", name,  
					"\nclassname: ", classname, "\n");  
			}  
			else  
				Print("[AccWindowFromPoint] failed to get ElementFromPoint\n");  
		}  
		else  
			Print("[AccWindowFromPoint] failed to CoCreateInstance\n");  
	}  
	else  
		Print("[AccWindowFromPoint] failed to CoInitialize\n");  
  
	//CoUninitialize();  
  
	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
C++
C++
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