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

Leonardo 96 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 development | Windows API - Win32
Developer technologies | C++
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-08-17T06:50:06.573+00:00

    You can use a WH_MOUSE_LL hook

    (if (wParam == WM_LBUTTONDOWN)... then MSLLHOOKSTRUCT.pt to get POINT, then same code)

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.