Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

problem of toolbar on dialog

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
yeshengl
Asked:
yeshengl
2 Solutions
 
BerCommented:
Can you post the code where you destroy the toolbar ...????

Cheers...
Ber...
0
 
yeshenglAuthor Commented:
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
 
fl0ydCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
yeshenglAuthor Commented:
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
 
fl0ydCommented:
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
 
AndyAinscowCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now