hook and subclassing - Programmer

This is a discussion on hook and subclassing - Programmer ; hello I'd like to detect which button is pressed on a typed YES/NO/CANCEL message box that is displayed by MS Office ( for instance, when you made modifications on a word document and try to quit without saving, a message ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: hook and subclassing

  1. hook and subclassing

    hello

    I'd like to detect which button is pressed on a typed YES/NO/CANCEL
    message box that is displayed by MS Office ( for instance, when you
    made modifications on a word document and try to quit without saving, a
    message box appears and asks you if you want to save or not the
    modifications)

    I manage to detect ( with a hook) the message box but I don't know how
    to determine which button is pressed by the user.

    could someone help me please ?

    thanks in advance.

    yann

    here is my code :

    declspec(dllexport) void SetHook(HINSTANCE h)
    {
    hhook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, hInst,
    (DWORD)NULL);
    }

    _declspec(dllexport) void RemoveHook(void)
    {
    UnhookWindowsHookEx(hhook);
    }

    LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
    {

    if(code < 0)
    return CallNextHookEx(hhook, code, wParam, lParam);

    if(code == HCBT_CREATEWND /*HCBT_ACTIVATE*/)
    {

    char text[TXTLENGTH];

    GetClassName((HWND)wParam, text, TXTLENGTH);

    /// is it a message box ?
    if (strcmp(text, "#32770") == 0 )
    {

    /// how to get the name of the message box ?
    /// how to know which button is pressed ?

    }

    }

    return CallNextHookEx(hhook, code, wParam, lParam);
    }


  2. Re: hook and subclassing


    "cyan" wrote in message
    news:1121760100.145061.136710@g44g2000cwa.googlegr oups.com...
    > hello
    >
    > I'd like to detect which button is pressed on a typed YES/NO/CANCEL
    > message box that is displayed by MS Office ( for instance, when you
    > made modifications on a word document and try to quit without saving, a
    > message box appears and asks you if you want to save or not the
    > modifications)
    >
    > I manage to detect ( with a hook) the message box but I don't know how
    > to determine which button is pressed by the user.
    >


    To get the text of the message box, can you call GetWindowText()?

    To determine the button pushed, install a WH_GETMESSAGE hook when your first
    hook receives HCBT_CREATEWND. Your Get Message hook can intercept calls to
    WM_COMMAND of the message box and tell which button was pressed.

    -- David
    http://www.dcsoft.com



  3. Re: hook and subclassing

    I managed to set the hook but could tell me how to recognize messages ?

    here is what I did :

    _declspec(dllexport) void SetHook(void)
    {
    if(!bHooked)
    {
    hInst = GetModuleHandle("Hook.dll");

    hhook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, hInst,
    (DWORD)NULL);
    hhookevent = SetWindowsHookEx( WH_GETMESSAGE, (HOOKPROC)GetMsgProc,
    hInst, (DWORD)NULL);

    if( hhook != 0 )
    bHooked = true;
    }
    }



    LRESULT CALLBACK GetMsgProc( int nCode, WPARAM wParam, LPARAM lParam )
    {
    if(nCode < 0)
    return CallNextHookEx(hhookevent, nCode, wParam, lParam);


    MSG* pMsg = (MSG*)lParam;

    // test : WM_LBUTTONDOWN is trapped but WM_COMMAND is not
    trapped
    if( pMsg->message == WM_LBUTTONDOWN )
    {
    trapped = true;
    }

    return CallNextHookEx(hhookevent, nCode, wParam, lParam);
    }


  4. Re: hook and subclassing


    "cyan" wrote in message
    news:1122044739.325777.259690@g44g2000cwa.googlegr oups.com...
    >I managed to set the hook but could tell me how to recognize messages ?
    >
    > LRESULT CALLBACK GetMsgProc( int nCode, WPARAM wParam, LPARAM lParam )
    > {
    > if(nCode < 0)
    > return CallNextHookEx(hhookevent, nCode, wParam, lParam);
    >
    >
    > MSG* pMsg = (MSG*)lParam;
    >
    > // test : WM_LBUTTONDOWN is trapped but WM_COMMAND is not
    > trapped
    > if( pMsg->message == WM_LBUTTONDOWN )
    > {
    > trapped = true;
    > }
    >
    > return CallNextHookEx(hhookevent, nCode, wParam, lParam);
    > }
    >


    That's funny. I ran Spy on the WinWord dialog asking whether to save the
    document when you exit. It is a normal dialog (class #32770). Spy saw the
    message:

    WM_COMMAND wNotifyCode:BN_CLICKED wID:IDYES

    so I'm not sure why your GetMessage proc didn't get it. Maybe you should
    put the trapped code at the very beginning? If this doesn't work, try using
    a WH_MSGFILTER hook instead, which is especially for dialogs.

    Another option is to just use your CBT hook and monitor the HCBT_SETFOCUS
    message, which should indicate which button has the focus so you know which
    one was clicked. This is probably the simplest thing... wish I had thought
    of it before.

    Cheers,
    David




+ Reply to Thread