MFC:根据请求将 CListView 选项卡添加到 CTabView (CBCGPTabView) 不会调用 CMyListView::OnInitialUpdate()

MFC: Adding CListView tab to a CTabView (CBCGPTabView) on request doesn't call CMyListView::OnInitialUpdate()

我有一个从 CBCGPTabView (CTabView) 派生的 CMyTabView,我想在其中动态添加标签。正在添加的选项卡将是从 CListView 派生的 CMyListView。如果我在 CMyTabView::OnCreate() 期间添加标签,它工作正常。如果我尝试通过自定义消息执行此操作,它会添加选项卡,但它是空白的(从未调用 CMyListView::OnInitialUpdate())。

我需要做什么才能让它工作?

这是有效的(测试选项卡):

int CMyTabView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (__super::OnCreate(lpCreateStruct) == -1)
    return -1;

  CBCGPTabWnd &tabctrl=GetTabControl();

  int index=AddView(RUNTIME_CLASS(CMyForm), _T("My Form"));
  if (index!=-1) {
    m_IDTabMyForm=tabctrl.GetTabID(index);
  }

  AddView(RUNTIME_CLASS(CMyListView), _T("Test"));

  tabctrl.HideNoTabs();

  return 0;
}

这不是(除了添加带有空白的选项卡 window)。

afx_msg LRESULT CMyTabView::OnUwmAddMyListViewTab(WPARAM wparam, LPARAM lparam)
{
  CString mytabname;
  mytabname=_T("My ListView");
  
  // check if tab already exists - if so select it
  CBCGPTabWnd &tabcontrol=GetTabControl();
  for (int i=0; i<tabcontrol.GetTabsNum(); i++) {
    CString tablabel;
    if (tabcontrol.GetTabLabel(i, tablabel)) {
      if (tablabel==mytabname) {
        tabcontrol.SetActiveTab(i);
        return 0;
      }
    }
  }
  // new tab so add it
  int index=AddView(RUNTIME_CLASS(CMyListView), mytabname);
  if (index!=-1) {
    tabcontrol.SetActiveTab(index);
  }
  
  return 0;
}

原来还得自己调用,以下版本有效:

afx_msg LRESULT CMyTabView::OnUwmAddMyListViewTab(WPARAM wparam, LPARAM lparam)
{
  CString mytabname;
  mytabname=_T("My ListView");
  
  // check if tab already exists - if so select it
  CBCGPTabWnd &tabcontrol=GetTabControl();
  for (int i=0; i<tabcontrol.GetTabsNum(); i++) {
    CString tablabel;
    if (tabcontrol.GetTabLabel(i, tablabel)) {
      if (tablabel==mytabname) {
        tabcontrol.SetActiveTab(i);
        return 0;
      }
    }
  }
  // new tab so add it
  int index=AddView(RUNTIME_CLASS(CMyListView), mytabname);
  if (index!=-1) {
    CView* thetabview=GetView(index);
    if (thetabview) {
      // we need to call OnInitUpdate ourself
      thetabview->SendMessage(WM_INITIALUPDATE);
      //make sure any child windows of the view get the message too
      thetabview->SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE);
    }

    tabcontrol.SetActiveTab(index);
  }
  
  return 0;
}