SetWindowHookEx 做消息响应

时间:2022-12-16 19:29:36
HHOOK g_Hook = NULL;

LRESULT CALLBACK CallWndProc(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
CWPSTRUCT * pMsg = (CWPSTRUCT*) lParam;
if(pMsg->message == 0x500)
{
TCHAR szDebugMsg[] = {};
_stprintf_s(szDebugMsg,_T("我的消息 消息内容:%s\n"),
(TCHAR*)pMsg->wParam);
OutputDebugString(szDebugMsg); } return CallNextHookEx(g_Hook,nCode,wParam,lParam);
} void CTestHookWindowProcedureDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
g_Hook = ::SetWindowsHookEx(WH_CALLWNDPROC,CallWndProc,::GetModuleHandle(NULL),::GetCurrentThreadId());
if(g_Hook == NULL)
{
TCHAR szError[] = {};
_stprintf_s(szError,_T("Error:%d"),::GetLastError());
AfxMessageBox(szError);
} } UINT __cdecl MyControllingFunction( LPVOID pParam )
{
HWND hWnd = ((CTestHookWindowProcedureDlg*)pParam)->m_hWnd;
while()
{
TCHAR szBuf[] = _T("张东升");
OutputDebugString(_T("发送前\n"));
::SendMessage(hWnd,0x500,(WPARAM)szBuf,);
OutputDebugString(_T("发送后\n")); Sleep();
}
}
void CTestHookWindowProcedureDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
AfxBeginThread(MyControllingFunction,this);
}