problem of toolbar on dialog

Posted on 2002-07-01
Medium Priority
Last Modified: 2013-11-20

I have a dialog need to have toolbar on it. I searched the web and found some examples about how to put toolbar on the dialog. And it works.

But I also need to declare the dialog as static. So the first time the dialog is opened, the toolbar on the dialog is okay. But if I close the dialog and opent is again, there will be some error. I know the reason is that the toolbar object already exists and should not be created again. But how can I solve this problem in detail? I tried to destory the toolbar object when the dialog is closed. But I failed. Can anyone help me in this? Thanks!!

Question by:yeshengl

Expert Comment

ID: 7123313
Can you post the code where you destroy the toolbar ...????


Author Comment

ID: 7123574
Hi Ber,

Thank you very much for you help! Here is the code.

1. When dialog is initialized:
BOOL MyDialog::OnInitDialog()
     static bool firstTime = true;
     // Set the icon for this dialog.
     SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_LEVEL_PATIENT), FALSE);
     // Get the client area before putting in Menu and ToolBar
     CRect rcClientStart, rcClientNow;

     if (!m_cToolBar.Create( this ) ||     !m_cToolBar.LoadToolBar(IDR_TOOLBAR_WORKLIST) )
          TRACE0("Failed to create toolbar\n");
          return -1;      // fail to create

     // Put in ToolBar
     RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0, reposQuery, rcClientNow);

     // Get the new client area after putting in Menu and ToolBar
          m_iNewClientTop = rcClientNow.top - rcClientStart.top;
          m_iNewClientLeft = rcClientNow.left - rcClientStart.left;
     // Move all the controls to be in the same relative position
     // within the remaining client area
     CPoint     ptOffset(m_iNewClientLeft, m_iNewClientTop);
     CRect     rcChild;
     CWnd*     pwndChild = GetWindow(GW_CHILD);
     while (pwndChild)
          pwndChild->MoveWindow(rcChild, FALSE);
          pwndChild = pwndChild->GetNextWindow();

     // Adjust the dialog window dimensions
     CRect rcWindow;
     rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
     rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
     MoveWindow(rcWindow, FALSE);

     // And position the control bars

2. Here is how I use the dialog:
void CMyApp::OnFileWorklist()
     static MyDialog md;

3. Here is how I deletthe toolbar:
void MyDialog::OnClose()
   delete m_cToolBar;


Accepted Solution

fl0yd earned 75 total points
ID: 7123612
Just skimmed through your code, not really going in to much detail. One terrible thing I noticed is in 3.): m_cToolBar is an object while delete only applies to pointers. If it does compile at all, your m_cToolBar is implicitly cast to a pointer. Deleting the object/structure that this is pointing to is causing all sort of trouble and is certainly not what you tried to do. Use m_cToolBar.DestroyWindow() instead.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.


Author Comment

ID: 7123624

Thanks for your advice! What I want to do is COMPLETELY destroy the object. But the problem is that I can not. I tried m_cToolBar.DestroyWindow(). But that function only delete the window of it. The object still exist. So when I open the dialog the second time, the statement:
if (!m_cToolBar.Create( this ) ||     !m_cToolBar.LoadToolBar(IDR_TOOLBAR_WORKLIST) )
         TRACE0("Failed to create toolbar\n");
         return -1;      // fail to create

will fail and exit.

Expert Comment

ID: 7123900
Try this:
m_cToolBar.m_bAutoDelete = TRUE;
right after you created the toolbar and use DestroyWindow to destroy it. Hope this solves the problem. I'm not sure though.
LVL 45

Assisted Solution

AndyAinscow earned 75 total points
ID: 7127879
Just a guess - The second time you open the dialog see if the toolbar has a valid hWnd (GetSafeHwnd()) and if it has then DON'T run the code to create it again.

Alternatively change it to a pointer, create it with the new keyword and use delete to clean it up when closing the dialog.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…

597 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