Hello,
You will have to use SetWinEventHook function.
Details here:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd373640%28v=vs.85%29.aspx[
^]
JAFC
#include <windows.h>
#include <process.h>
#include <oleacc.h>
#pragma comment (lib, "oleacc.lib")
#include <cstdio>
void CALLBACK HandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
IAccessible* pAcc = NULL;
VARIANT varChild;
HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild);
if ((hr == S_OK) && (pAcc != NULL))
{
VARIANT varResult = {};
pAcc->get_accRole(varChild, &varResult);
if (varResult.lVal == ROLE_SYSTEM_PAGETAB || varResult.lVal == NULL)
{
BSTR bstrName;
pAcc->get_accName(varChild, &bstrName);
if (event == EVENT_OBJECT_CREATE)
{
printf("Create: ");
}
else if (event == EVENT_OBJECT_DESTROY)
{
printf("Destroy: ");
}
printf("%S\n", bstrName);
SysFreeString(bstrName);
}
pAcc->Release();
}
}
unsigned __stdcall hook(void* args)
{
HWND hWindow = FindWindow(TEXT("Notepad++"), NULL);
if (hWindow != NULL)
{
DWORD ProcessId, ThreadId;
ThreadId = GetWindowThreadProcessId(hWindow, &ProcessId);
CoInitialize(NULL);
HWINEVENTHOOK hHook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_DESTROY, NULL, HandleWinEvent, ProcessId, ThreadId, WINEVENT_OUTOFCONTEXT);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0);
UnhookWinEvent(hHook);
CoUninitialize();
}
return 0;
}
int main()
{
unsigned ThreadId;
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, hook, NULL, 0, &ThreadId);
for (;; Sleep(100))
{
if (GetAsyncKeyState(VK_END) & 0x8000) {
PostThreadMessage(ThreadId, WM_QUIT, 0, 0);
WaitForSingleObject(hThread, 3000);
break;
}
}
CloseHandle(hThread);
return 0;
}
Run Notepad++ first, then the program above, create some new tabs in Notepad++ and you'll see the tab names output to the console.
Note: The destroy event is less reliable, properties (name, role, etc) are NULL, you may want to create a map of child ids and their names from EVENT_OBJECT_CREATE, then whenever you receive a destroy event, check the map for the id and handle appropriately.
Pasted from:
http://www.cplusplus.com/forum/windows/58791/[
^]