Solved

problem of toolbar on dialog

Posted on 2002-07-01
8
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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!

 

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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 216
repeatEnd java challenge 42 85
deburging in oracle form 12 120
mapAB Challlenge 35 186
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

710 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