MFC tabbed documents - how do I enable the middle mouse button to close a document?

If you are creating a new MFC application (with the MFC Feature Pack) and using all the default values, click Finish. He creates an MDI application with a new style of Documents with Tabs.

alt text

I think this is great, except that it annoys me that I cannot close the tabbed document window by clicking on the tab with the middle mouse button.

This is possible in Firefox, IE, Chrome and, more importantly, in VS2008 . But pressing the middle button on the tab does nothing.

I can't figure out how to override the tab bar to let me handle the message ON_WM_MBUTTONDOWN. Any ideas?

Edit : I think I need to create a subclass of CMFCTabCtrl returned from CMDIFrameWndEx :: GetMDITabs ...

+3
source share
1 answer

No subclass (phew) required. Managed to get it working by capturing the PreTranslateMessage mainframe. If the current message is a middle mouse button message, I check the location of the click. If it was on a tab, I close this tab.

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
    switch (pMsg->message)
    {
        case WM_MBUTTONDBLCLK:
        case WM_MBUTTONDOWN:
        {
            //clicked middle button somewhere in the mainframe.
            //was it on a tab group of the MDI tab area?
            CWnd* pWnd = FromHandle(pMsg->hwnd);
            CMFCTabCtrl* tabGroup = dynamic_cast<CMFCTabCtrl*>(pWnd);
            if (tabGroup)
            {
                //clicked middle button on a tab group.
                //was it on a tab?
                CPoint clickLocation = pMsg->pt;
                tabGroup->ScreenToClient(&clickLocation);
                int tabIndex = tabGroup->GetTabFromPoint(clickLocation);
                if (tabIndex != -1)
                {
                    //clicked middle button on a tab.
                    //send a WM_CLOSE message to it
                    CWnd* pTab = tabGroup->GetTabWnd(tabIndex);
                    if (pTab)
                    {
                        pTab->SendMessage(WM_CLOSE, 0, 0);
                    }
                }
            }
            break;
        }
        default:
        {
            break;
        }
    }
    return CMDIFrameWndEx::PreTranslateMessage(pMsg);
}
+2
source

Source: https://habr.com/ru/post/1714423/


All Articles