Question : CToolBar derived Toolbar repositioning

The problem is to put floating CToolBars in same position than that was saved at program end.
A GetWindowRect is done for each ToolBar and the rectangle is saved to the Registry. These coords should be screen-coords. When running the program, it reads the saved rectangle, and, to use FloatControlBar, it converts the above rectangle to Client coords with ScreenToClient.
But floating ToolBars reposition at a wrong position, as shown in the attached pictures.
Code is the following:
WRITING: (B is the CToolBar derived object pointer)
RECT rcNormalPosition;
B->GetWindowRect(&rcNormalPosition);
CWinApp* pApp = AfxGetApp();
B->GetWindowText(Nome,49); // name of the ToolBar
pApp->WriteProfileInt(Nome,"Left",  rcNormalPosition.left); // position
pApp->WriteProfileInt(Nome,"Top",   rcNormalPosition.top);
 pApp->WriteProfileInt(Nome,"Right", rcNormalPosition.right);
 pApp->WriteProfileInt(Nome,"Bottom",rcNormalPosition.bottom);
pApp->WriteProfileInt(Nome,"State", B->IsFloating());
READING AND POSITIONING: (Again B is the CToolBar object pointer)
RECT r; char Nome[50]; BOOL Floating;
CWinApp* pApp = AfxGetApp();
B->GetWindowText(Nome,49);
r.left=pApp->GetProfileInt(Nome,"Left",0);
r.top=pApp->GetProfileInt(Nome,"Top",0);
r.right=pApp->GetProfileInt(Nome,"Right",0);
r.bottom=pApp->GetProfileInt(Nome,"Bottom",0);
Floating=pApp->GetProfileInt(Nome,"State",0);
  if (Floating)
  {
     CPoint pp;
     pp.x=r.left; pp.y=r.top;
     ScreenToClient(&pp);
     FloatControlBar(B,pp,AFX_IDW_DOCKBAR_TOP); // tried any Docking    
  }
Thanks in advance

Answer : CToolBar derived Toolbar repositioning

That does make sense, and yours is a good solution.  I'll describe another below.

In the case where a toolbar has been floated, it actually becomes a child of the little floater  window.  When you query the window location, you still get the location the toolbar -- not its floating container window.

What you could do is:  
See if it is floating, and if so, save the screen coordinates of its parent window.  Apparently that is the window that will get positioned at the time of FloatControlBar.  Another possibility would be to call MoveWindow() on tath parent "mini-frame"

To verify all of this, I suggest that you use the program named Spy++  It will be a lot clearer about which window is a child of which other window.  Another way to solve these problems is to breakpoint the code and single-step into the MFC source code.  It usually becomes clear what is happening soon enough.

Random Solutions  
 
programming4us programming4us