views:

27

answers:

1

I have a CWnd derived class that has a WM_CONTEXTMENU handler (OnContextMenu), which has my default context menu. This class is being used at several places in my application.

Some of the places where it's being used also handle the WM_CONTEXTMENU at the parent level (the window parent). They basically override the default context menu.

When I am inside the CWnd derived class, I basically want to know if someone else (a window parent) handled the context menu.

for example:

void MyDerivedWnd::OnContextMenu( CWnd* in_pWnd, CPoint in_point )
{
    LRESULT res = __super::Default();

    // Now, how to I know of something happened inside __super::Default();??

    // Show my default menu
    // ...
}

It is possible via the Win32/MFC framework?

A: 

I found a way to discover if something happened during the default handler implementation. It may not be the most elegant solution, but here it is:

bool g_bWindowCreated = false;
HHOOK g_hHook = NULL;
LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
{
    if( code == HCBT_CREATEWND )
        g_bWindowCreated = true;

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

void MyDerivedWnd::OnContextMenu( CWnd* in_pWnd, CPoint in_point )
{
    // Setup a hook to know if a window was created during the 
    // Default WM_CONTEXT_MENU handler
    g_bWindowCreated = false;
    g_hHook = SetWindowsHookEx( WH_CBT, HookProc, NULL,  GetCurrentThreadId() );

    // Call the default handler
    LRESULT res = __super::Default();

    UnhookWindowsHookEx( g_hHook );

    if( !g_bWindowCreated )
    {
        // Show my default menu
        // ...
    }
}
decasteljau