Link to home
Start Free TrialLog in
Avatar of gymsam
gymsam

asked on

Tool bar alignment

In an Doc/View MFC application i have five toolbars .I want to align it one after another in an linear fashion below the Menu.I have created these toolbar as CBRS_ALIGN_ANY.I use the following logic

I have now five toolbar objects;

Now i call CMainFrame::PlaceControlBar() in the following order.

PlaceControlBar( NULL, &m_wndToolBar );
PlaceControlBar( &m_wndToolBar, &m_wndToolBar2 );
PlaceControlBar( &m_wndToolBar2,&m_wndMapTool);
PlaceControlBar( &m_wndMapTool, &m_wndDateBar );
PlaceControlBar( &m_wndDateBar, &m_wndViewScale);

void CMainFrame::PlaceControlBar( CControlBar* pLast, CControlBar* pCurrent )
{
     if( pLast == NULL )
     {
          DockControlBar(pCurrent, AFX_IDW_DOCKBAR_TOP, NULL );
          RecalcLayout();
          return;
     }

     ASSERT( pLast != NULL );
     ASSERT( pCurrent != NULL );

     CRect rcLast;
     
     // Toolbar placement forces non-client areas to overlap.
     // Hence we use the client rectangle and not the window rectangle.
     pLast->GetClientRect( &rcLast );
     pLast->ClientToScreen( &rcLast );
     
     CRect rcDock(rcLast.right, rcLast.top, rcLast.right+1, rcLast.top+1 );
     
     DockControlBar(pCurrent, AFX_IDW_DOCKBAR_TOP, &rcDock );

     RecalcLayout();
}

now comming to the problem.Only the first three toolbars are aligned one after another in one line,but the next two toolbar is taking the place of next row even though there is enough space in first row.How to solve this problem?
Avatar of DanRollins
DanRollins
Flag of United States of America image

DockControlBar expects screen coords, you are passing client coords.  This...

CRect rcPrev, rcCur;

pBarPrev->GetWindowRect( &rcPrev );
pBarCur->GetWindowRect( &rcCur );
rcCur= rcPrev;
rcCur.OffsetRect( rcPrev.Width(), 0 );
DockControlBar( pBarCur, AFX_IDW_DOCKBAR_TOP, rcCur);

...will work, right up until you try too add too many toolbars.  For instance, if no part of toolbar #5 will fit, then RecalcLayout() will force it to move down to the second line.

The only way to avoid that is to check rcPrev.right and if it is more than the Frame window's right side, then hide it.

Furthermore, if the user re-sizes the frame, it can cause the rightmost bar to move down to a lower line.  This is by design.  It is standard Windows UI and if you want to do something different, you are breaking User Interface rules and will confuse your users.  Also, you will need to do extra work (hide and show bars in your OnSize handler).

-- Dan
Avatar of gymsam
gymsam

ASKER

I tried your option still its not aligning single row.3 tool bars in first row and rest two in next row.i think GetWindowRect will not give screen coordinates.Any other way?
It works.  That is functional, tested code.  It assumes that you have called Create for each bar and that you have called RecalcLayout() on the Frame after creating each bar and also called EnableDocking(CBRS_ALIGN_TOP) for each bar.

If there is room for any part of the third bar, it will appear in the top row.  If there is room for any part of the fourth bar, it will also appear on the top row.  Otherwise, there will be two rows of bars.

-- Dan
Avatar of gymsam

ASKER

hi gymsam,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
Avatar of gymsam

ASKER

I will get you little latter
Dan: yeah, he'll get all of us ;)

gymsam: just an idea - I just encountered a similar problem today, with a almost stupid solution: I had a dialog bar with the dialog resource in use much wider than necessary. guess what happened... the dialog bar was placed on a new row, although the controls itself would have fit....
Yes, and the "wrapping" problem also goes away if you make one or two large toolbars instead of many small ones.

-- Dan
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Comment from DanRollins accepted as answer.

Thank you
Computer101
Community Support Moderator