SetWindowHookEx 做消息响应

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[256] = {0};
        _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[256] = {0};
        _stprintf_s(szError,_T("Error:%d"),::GetLastError());
        AfxMessageBox(szError);
    }

}

UINT __cdecl MyControllingFunction( LPVOID pParam )
{
    HWND hWnd = ((CTestHookWindowProcedureDlg*)pParam)->m_hWnd;
    while(1)
    {
        TCHAR szBuf[] = _T("张东升");
        OutputDebugString(_T("发送前\n"));
        ::SendMessage(hWnd,0x500,(WPARAM)szBuf,0);
        OutputDebugString(_T("发送后\n"));

        Sleep(5000);
    }
}
void CTestHookWindowProcedureDlg::OnBnClickedButton2()
{
    // TODO: 在此添加控件通知处理程序代码
    AfxBeginThread(MyControllingFunction,this);
}

 

你可能感兴趣的:(window)