- CMFCTabCtrl::EnableActiveTabCloseButton
用于在CMFCTabCtrl的Tab页显示一个关闭按钮,看看关闭按钮怎么实现:
- void CMFCBaseTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- m_bWindowPosChanged = FALSE;
- CWnd::OnLButtonDown(nFlags, point);
- if (m_rectCloseButton.PtInRect(point))
- {
- m_bTabCloseButtonPressed = TRUE;
- RedrawWindow(m_rectCloseButton);
- return;
- }
- ...
- void CMFCBaseTabCtrl::OnLButtonUp(UINT nFlags, CPoint point)
- {
- if (m_bTabCloseButtonPressed)
- {
- m_bTabCloseButtonPressed = FALSE;
- m_bTabCloseButtonHighlighted = FALSE;
- RedrawWindow(m_rectCloseButton);
- if (m_rectCloseButton.PtInRect(point))
- {
- CWnd* pWndActive = GetActiveWnd();
- if (pWndActive != NULL)
- {
- pWndActive->SendMessage(WM_CLOSE);
- }
- return;
- }
- }
- ...
应该就这些了。
在例子TabbedView中添加这个功能:
- int CTabbedViewView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CTabView::OnCreate(lpCreateStruct) == -1)
- return -1;
- AddView (RUNTIME_CLASS (CView1), _T("Simple"), 100);
- AddView (RUNTIME_CLASS (CView2), _T("List"), 101);
- AddView (RUNTIME_CLASS (CView3), _T("Form"), 102);
- AddView (RUNTIME_CLASS (CView4), _T("ScrollView"), 102);
- m_wndTabs.EnableActiveTabCloseButton();
- return 0;
- }
然后点击一个页面上的关闭按钮,会发现这个tab页并没有消失,而切换到其他页面,程序就中断了,问题出在:
- BOOL CMFCTabCtrl::SetActiveTab(int iTab)
- {
- ...
- if (m_iActiveTab != -1 && m_bHideInactiveWnd)
- {
- //--------------------
- // Hide active window:
- //--------------------
- CWnd* pWndActive = GetActiveWnd();
- if (pWndActive != NULL)
- {
- pWndActive->ShowWindow(SW_HIDE);
- }
- }
由于窗口已经销毁,而该Tab页仍然存在,所以m_iActiveTab就是被销毁的窗口所在页面,pWndActive是已经被销毁的窗口,自然调用其ShowWindow方法就出错了。