码迷,mamicode.com
首页 > Windows程序 > 详细

SetWindowHook 做消息响应

时间:2015-07-16 09:36:42      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

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);
}

 

SetWindowHook 做消息响应

标签:

原文地址:http://www.cnblogs.com/zhangdongsheng/p/4650191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!