Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am Dinesh Suthar.
I am creating a Windows Application in C#.net and a notepad.
In the application there is a textbox which holds the content of notepad.

The problem is: I am asked to include a new functionality in such a way that whenever application is running and if any changes happen (during runtime) in notepad those should be reflected back in to the textbox automatically without using any refresh button.

Please help me in this regards....
Posted
Updated 23-Apr-14 0:58am
v2

1 solution

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

C++
#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) // Press End to exit
		{
			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/[^]
 
Share this answer
 
v2
Comments
[no name] 23-Apr-14 6:53am    
Could you please explain using a simple example because i didnt understand the content deeply in that link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900