Solved

problem of toolbar on dialog

Posted on 2002-07-01
8
317 Views
Last Modified: 2013-11-20
Hi,

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!!


0
Comment
Question by:yeshengl
8 Comments
 
LVL 2

Expert Comment

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

Cheers...
Ber...
0
 

Author Comment

by:yeshengl
ID: 7123574
Hi Ber,

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

1. When dialog is initialized:
BOOL MyDialog::OnInitDialog()
{
     CDialog::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;
     GetClientRect(rcClientStart);


     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
     if(firstTime)
     {
          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->GetWindowRect(rcChild);
          ScreenToClient(rcChild);
          rcChild.OffsetRect(ptOffset);
          pwndChild->MoveWindow(rcChild, FALSE);
          pwndChild = pwndChild->GetNextWindow();
     }

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

     // And position the control bars
     RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
}

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

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

0
 
LVL 8

Accepted Solution

by:
fl0yd earned 25 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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:yeshengl
ID: 7123624
Hi,

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.
0
 
LVL 8

Expert Comment

by:fl0yd
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.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 25 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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

13 Experts available now in Live!

Get 1:1 Help Now