CToolBar derived Toolbar repositioning

Posted on 2009-12-23
Last Modified: 2013-12-14
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;
CWinApp* pApp = AfxGetApp();
B->GetWindowText(Nome,49); // name of the ToolBar
pApp->WriteProfileInt(Nome,"Left",  rcNormalPosition.left); // position
 pApp->WriteProfileInt(Nome,"Right", rcNormalPosition.right);
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();
  if (Floating)
     CPoint pp;
     FloatControlBar(B,pp,AFX_IDW_DOCKBAR_TOP); // tried any Docking    
Thanks in advance
Question by:gpol
    LVL 49

    Expert Comment

    FloatControlBar expects screen coordinates, but you are converting them to client coordinates.  See:
    As a diagnostic aid, do the code when running as a non-maximized app.  Move the main window down and to the right, and save the toolbar locations.  Check the registry and eyeball the values... the top/left should be much larger than for a maximized app.
    LVL 49

    Expert Comment

    One other thought:  MFC Feaure pack supports a feature that automatically saves and restores all toolbars completely automatically, and taking into consideration the many complications involved.  See:
        MFC Feature Pack for VS 2008 and 2010

    Author Comment

    Dear Dan
    I would like you were right... As a matter of fact, I took away the call to ScreenToClient (I uncorrectly read that DockControlBar was working in client coords), and the situation is now that showed in the two attached pictures: the toolbar goes downwards now.  What happens is well illustrated by the following sequence:
    1) I go to the registry and put 0 on the top coord.
    2) Running program, it correctly displays the Toolbar at 0 of the screen, covering partially the mainframe
    3) I exit the program without doing anything. During this exit, the program executes the code for getting WindowRect of the ToolBar and saves it to the registry: top value is now 18 (I always mean decimal values). So, despite the ToolBar is on the top, at 0 y, GetWindowRect return 18 for top rectangle coord...
    4) Reentering the program, the toolbar appears at a lower position, of course. Iit seems of the height of the title bar.
    So my opinion is that the GetWindowRect of the ToolBar is not returning the "all window rectangle" but only the part with the buttons, or, at least, the part without the title bar. I guess that I have to investigate better the "metrics" of the toolbar..
    Let me know whether I am right or not, and, if possible, what to do. I will also investigate this feature pack you suggest.
    Thanks for now

    Author Comment

    the images, but what is happening is clear.

    Author Comment

    Dear Dan
    I think to have resolved, because I subtracted the value given by  ::GetSystemMetrics(SM_CXSIZE) to the top and bottom values returned by GetWindowRect, and now it seems to work. Of course I am still open to your comments.
    By the way, do you know why GetWindowRect behaves so on ToolBars? Is it (as usual) my fault, or something not very clear in this situation?.
    LVL 49

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    Perl Awk Need Help 3 85
    How to know only "File created" with EventLog 3 24
    NotAlone Challenge 20 65
    Fibonacci challenge 11 61
    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now