如何在Windows应用程序中捕获记事本的事件 [英] How to capture the events of a notepad in windows application

查看:260
本文介绍了如何在Windows应用程序中捕获记事本的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我是Dinesh Suthar。

我正在用C#.net创建一个Windows应用程序和一个记事本。

应用程序有一个文本框,其中包含记事本的内容。



问题是:我被要求以这样的方式包含一个新功能:每当应用程序 正在运行 如果在记事本中发生任何变化(在运行期间),那些应该自动反映到文本框中不使用任何刷新按钮



请在这方面帮助我....

解决方案

您好,



您必须使用SetWinEventHook功能。



详细信息:< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/dd373640%28v=vs.85%29.aspx> 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 < CST dio >

void CALLBACK HandleWinEvent (HWINEVENTHOOK钩,DWORD <跨度类= 代码关键字>事件,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( 创建:);
}
其他 如果 event == EVENT_OBJECT_DESTROY)
{
printf( Destroy:);
}
printf( %S \ n,bstrName);
SysFreeString(bstrName);
}
pAcc-> Release();
}
}

unsigned __ stdcall hook( void * args)
{
HWND hWindow = FindWindow(TEXT( < span class =code-string> 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) // 按End退出
{
PostThreadMessage(ThreadId,WM_QUIT, 0 0 );
WaitForSingleObject(hThread, 3000 );
break ;
}
}
CloseHandle(hThread);
return 0 ;
}





首先运行Notepad ++,然后运行上面的程序,在Notepad ++中创建一些新选项卡,你会看到标签名称输出到控制台。



注意:destroy事件不太可靠,属性(名称,角色等)为NULL,您可能要创建一个地图来自EVENT_OBJECT_CREATE的子ID及其名称,然后每当收到destroy事件时,请检查映射中的id并正确处理。



粘贴自: http://www.cplusplus。 com / forum / windows / 58791 / [ ^


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....

解决方案

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) // 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/[^]


这篇关于如何在Windows应用程序中捕获记事本的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆