Why does CMainFrame::OnMdiNext is not called?

Flaviu_ 1,071 Reputation points
2025-07-16T15:05:14.8366667+00:00

I have a MFC MDI app. I want to override WM_MDINEXT message. And I did:

header:

protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg LRESULT OnMdiNext(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()

implementation:

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWndEx)
ON_WM_CREATE()
ON_MESSAGE(WM_MDINEXT, &CMainFrame::OnMdiNext)
END_MESSAGE_MAP()

and

LRESULT CMainFrame::OnMdiNext(WPARAM wParam, LPARAM lParam)
{
TRACE(_T("\n\nWM_MDINEXT intercepted!\n\n"));
MDINext();
// Default behavior:
return __super::DefWindowProc(WM_MDINEXT, wParam, lParam);
}

But when I switch child frames using Ctrl+Tab key, no WM_MDINEXT event fired, even if the child frames are switching ... why? Can you help here?

Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 50,066 Reputation points
    2025-07-16T15:41:09.8866667+00:00

    You can get WM_MDINEXT by subclassing the MDI client window. For example --

    In CMainFrame class -

    public:
    	static LRESULT CALLBACK MDIClientProc(HWND, UINT, WPARAM, LPARAM, UINT_PTR, DWORD_PTR);
    

    In CMainFrame::OnCreate -

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CMDIFrameWndEx::OnCreate(lpCreateStruct) == -1)
    		return -1;
    
    	SetWindowSubclass(m_hWndMDIClient, MDIClientProc, 42, (DWORD_PTR) this);
    //....Rest of code
    }
    

    Subclass Proc -

    LRESULT CMainFrame::MDIClientProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uiSubclass, DWORD_PTR dwRefData)
    {
    	if (msg == WM_MDINEXT)
    		TRACE(_T("Got WM_MDINEXT\n"));
    	else if (msg == WM_NCDESTROY)
    		RemoveWindowSubclass(hwnd, MDIClientProc, uiSubclass);
    
    	return DefSubclassProc(hwnd, msg, wParam, lParam);
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.