win32 CTabctrl: Drawing "child" windows in the active tab

2.4k views Asked by At

I have a resizable dialog that contains a CTabCtrl, the tab control has 4 tabs that when clicked on displays one of four different CTreeCtrls.

I have derived a class from CTabCtrl, which keeps track of its "child" controls like so:

...
class Container: public CTabCtrl {
vector<CWnd*> _children;
....
int Container::AddTab(CWnd* Child) {
 CString txt;Child->GetWindowText(txt);
 _children.push_back(Child);
 int idx = this->InsertItem(this->GetItemCount(), txt, 0);
 if(idx == 0) {
  CRect c;
  this->GetWindowRect(&c);
  GetParent()->ScreenToClient(&c);
  this->AdjustRect(FALSE, c);
  Child->SetWindowPos(&wndTop, c.left, c.top, c.Width(), c.Height(), SWP_SHOWWINDOW);
  this->SetCurSel(idx);
 } else Child->ShowWindow(SW_HIDE);
 return idx;
}

And I attempt to draw the child controls like so:

void Container::OnTabChanging(NMHDR*, LRESULT* pResult)  { // hide the changed from tab
    int selected = this->GetCurSel();
    if(selected != -1)
    {
        // move old window to bottom of the zorder and hide
        _children[selected]->SetWindowPos(&wndBottom, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_HIDEWINDOW);
        ASSERT(!_children[selected]->IsWindowVisible());
    }
    *pResult = 0;
}
// show the child for the tab being changed to
void CNodeContainer::OnTabChanged(NMHDR* pNMHDR, LRESULT* pResult) {
 int selected = this->GetCurSel();
 ASSERT(selected!=-1);
 CRect c;
 this->GetWindowRect(&c);
 GetParent()->ScreenToClient(&c);
 this->AdjustRect(FALSE, c);
 _children[selected]->SetWindowPos(&wndTop, c.left, c.top, c.Width(), c.Height(), SWP_SHOWWINDOW|SWP_FRAMECHANGED);
 *pResult = 0;
}

However the child controls, whilst they appear, don't always draw correctly, they sort of mix up their content together and only show the right content when i click on them (the actual tree controls).

Is this the best way of drawing and moving windows around in the zorder, what am I missing?

Many thanks

bg

3

There are 3 answers

0
push 22 On BEST ANSWER

Its fixed now - the problem came from the fact that the in the resize code for the tabctrl, I was using movewindow to move the child windows into place - This was changing the zorder of the child windows.

0
Sunscreen On

This could solve the problem after your window or tab apears. Try to use

this->RedrawWindow();

In OnTabChanging() function before it returns.

0
Mark Ingram On

Instead of just changing the z-order of your children, completely hide every child except the top one. I use the same approach in a custom CTabCtrl and it works fine.