tags:

views:

64

answers:

2

I have hooked WM_SETFOCUS message by calling API

hhookCallWndProc = SetWindowsHookEx(WH_CALLWNDPROC, HookCallWndProc, hInst, threadID);

Hook Procedure is

extern "C"   LRESULT _declspec(dllexport) __stdcall CALLBACK HookCallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
      if (nCode == HC_ACTION) {
        CWPSTRUCT* info = (CWPSTRUCT*) lParam;
        if(info->message == WM_SETFOCUS )
        {
            if(info->hwnd == hControl)
            {
                MessageBox(NULL,L"Focus on control",L"Focus",MB_OK);
                }
        }
      }
      return CallNextHookEx(hhookCallWndProc , nCode, wParam, lParam);
    }

Now when I focus on the control, this hook procedure is getting called . MessageBox is shown. But as soon as I click on Ok , another message pops up.

Messages keep on popping up infinitely. I want to get messagebox only once whenever I focus on control, but here I am getting messages infinitely.

Anything I am doing wrong.

+1  A: 

Quick guess - doesn't closing a message box force a re-focus of the control and therefore call your function again?

Max Shawabkeh
+1 I think that this is the problem.
gyurisc
A: 

I think the problem is that with the message box you take away the focus and when clicking ok you give back the focus to the control, so your hook is called again. I would recommend to try print out something using the OutputDebugString instead of using a message box.

gyurisc