Come sapere se qualcun altro ha gestito OnContextMenu
-
26-09-2019 - |
Domanda
Ho una classe derivata CWnd
che ha un gestore WM_CONTEXTMENU
(OnContextMenu
), che ha il mio menu di scelta rapida di default. Questa classe viene utilizzata in vari punti nella mia applicazione.
Alcuni dei luoghi in cui è in uso anche gestire il WM_CONTEXTMENU
a livello genitore (genitore finestra). In sostanza sostituiscono il menu di contesto di default.
Quando sono all'interno della classe derivata CWnd
, io fondamentalmente voglio sapere se qualcun altro (una finestra padre) ha gestito il menu di contesto.
Ad esempio:
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
// ...
}
E 'possibile tramite il quadro Win32 / MFC?
Soluzione
Ho trovato un modo per scoprire se è successo qualcosa durante l'attuazione gestore predefinito. Esso non può essere la soluzione più elegante, ma qui è:
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
// ...
}
}