Outlook bar auto size to tree control

206 views Asked by At

I created an Outlook bar with tree controls and would like to have it auto size to always fully display the text of the tree control. Ideally the border in the picture would move so that "Healthcare Merchandising" is fully visible.

DrillSizing

newDimbar is a CMFCOutlookBar object created in CMainFrame.

I have tried stretching it:

void CTreeDrill::OnTvnItemexpanded(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
HTREEITEM hItem = pNMTreeView->itemNew.hItem;
RECT treeRect;
GetItemRect(GetChildItem(hItem), &treeRect, FALSE);
CMainFrame *pMain = (CMainFrame*)AfxGetMainWnd();
int iStretch = treeRect.right;
pMain->m_ctlNewDimBar.StretchPane(iStretch, FALSE);
EnsureVisible(GetChildItem(hItem));
}

and using move:

void CTreeDrill::OnTvnItemexpanded(NMHDR *pNMHDR, LRESULT *pResult)
{
RECT treeRect;
RECT newRect;
RECT dimRect;
GetItemRect(GetChildItem(hItem), &treeRect, FALSE);
CMainFrame *pMain = (CMainFrame*)AfxGetMainWnd();
pMain->m_ctlNewDimBar.GetWindowRect(&dimRect);
newRect = dimRect;
newRect.right = treeRect.right;
pMain->m_ctlNewDimBar.MoveWindow(newRect);
EnsureVisible(GetChildItem(hItem));
}

without luck. What am I missing?

1

There are 1 answers

0
Robson On BEST ANSWER

Solution

I'm happy i found it because this is something i would have to do too in the future, you need to use CWnd::SetWindowPos to change the CMFCOutlookBar's size, check this tutorial that has more infos, after you change the size of CMFCOutlookBar you will have to use CFrameWndEx::RecalcLayout method of the frame that contains the CMFCOutlookBar.

Why using CWnd::SetWindowPos?

i don't know, it's the only one that worked for me

Why use CFrameWndEx::RecalcLayout and not only call RecalcLayout() of the CMFCOutlookBar?

Because if you just recalculate the layout of the CMFCOutlookBar only the CMFCOutlookBar will be updated and then if you have anything attached to the CMFCOutlookBar it will not recieve the changes, so you might end with your CMFCOutlookBar overlapping some other control or your document's view, calling CFrameWndEx::RecalcLayout will make the whole frame reacalculate and so if you have tabbed document views they will be updated/resized accordingly.

Your case

you will have to calculate the whole width of the tree, not only the item, and then use the CWnd::SetWindowPos on the CMFCOutlookBar with the updated value width value but keeping the height of the CMFCOutlookBar.

newDimbar.GetWindowRect(pos);
ScreenToClient(&pos);
UINT flags = SWP_NOZORDER | SWP_NOMOVE;
newDimbar.SetWindowPos(NULL, 0, 0, iNewWidth, pos.Height(), flags);