I converted the Pascal Sertac Akyuz code to C ++
#include "Windows.h"
#include <psapi.h> // For access to GetModuleFileNameEx
#include <iostream>
#include <string>
#ifdef _UNICODE
#define tcout wcout
#define tcerr wcerr
#else
#define tcout cout
#define tcerr cerr
#endif
HWND GetFocusGlobal()
{
HWND Wnd;
HWND Result = NULL;
DWORD TId, PId;
Result = GetFocus();
if (!Result)
{
Wnd = GetForegroundWindow();
if(Wnd)
{
TId = GetWindowThreadProcessId(Wnd, &PId);
if (AttachThreadInput(GetCurrentThreadId(), TId, TRUE))
{
Result = GetFocus();
AttachThreadInput(GetCurrentThreadId(), TId, FALSE);
}
}
}
return Result;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::wstring state;
while(1)
{
HWND focus_handle = GetFocusGlobal();
if(focus_handle)
{
TCHAR text[MAX_PATH];
GetClassName(focus_handle, text, MAX_PATH);
const std::wstring cur_path(text);
if(cur_path != state)
{
std::tcout << "new:" << focus_handle << " " << text << std::endl;
state = cur_path;
}
}
Sleep(50);
}
}
source
share