Solved

problem of toolbar on dialog

Posted on 2002-07-01
10
311 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
  • 4
  • 4
  • 2
10 Comments
 
LVL 4

Expert Comment

by:mblat
ID: 7123231
One thing you can do is to declare static variable and check for it value

something like this:

in .h
class CMyClass
{
 static bool m_bToolbarAlreadyCreated;
};

in.cpp

bool CMyClass::m_bToolbarAlreadyCreated = false;

in
CMyClass::OnInitDialog()
{
  if(!CMyClass::m_bToolbarAlreadyCreated)
  {
    // create toolbar
    CMyClass::m_bToolbarAlreadyCreated  = true;
  }
}

second thing is of cause to destroy toolbar.  What exactly you need to do to achive that is difficult to say without knowing exactly how you created it....or at least how it fails when you creating dialog second time....  It is likely that you need to call Detach() on you CWnd class that represent dialog....

0
 

Author Comment

by:yeshengl
ID: 7123613
Hi Mblat,

Thank you very much for you help! Here is how I create and use the toolbar. Can you show me how to destroy the toolbar object?

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 4

Expert Comment

by:mblat
ID: 7123698
I assume that m_cToolBar is class of CToolBar type?

so you should try this:

m_cToolBar.DestroyWindow();


Hope it helps...
0
 

Author Comment

by:yeshengl
ID: 7123702
Yes, the m_cToolBar is CToolBar type. I tried m_cToolBar.DestroyWindow(); But when the second time I open the dialog, the following statement will fail.

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

It seems the m_cToolBar object is still not destroyed. Thanks!

0
 
LVL 12

Accepted Solution

by:
migel earned 50 total points
ID: 7123954
Hi!
your problem is that MFC classes do not free HANDLERS during window destroying. So you have invalild Window Handle after toolbar window destroing while MFC class still exist.
Try switch to the dynamic allocation of the ToolBar and destroying its object in the WM_DESTROY dialog handler.
for eample:
CMyDialog::OnInitDialog()
{
    m_pTool = new CToolBar();
    m_pTool->Create(....);
}

CMyDialog::OnDestroy();
{
   m_pTool->DestroyWindow();
   delete m_pTool;
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:mblat
ID: 7124771
2 migel:
wasn't it my suggestion?

2 yeshengl:

How does it fail?  Try to step into library code as far as possible and tell us the line it is failing at.
0
 
LVL 12

Expert Comment

by:migel
ID: 7125059
not itsn`t
IMHO I use pointers while you use no dynamically allocated class member.
0
 
LVL 4

Expert Comment

by:mblat
ID: 7125383
On BIG question.

I was looking at your code and see that you are using

if (!m_cToolBar.Create( this ) ||     !m_cToolBar.LoadToolBar(IDR_TOOLBAR_WORKLIST) )
   


and later

delete m_cToolBar;

It doesn't compute here.  If you are using pointers you should use m_cToolBar->Create

and if not you shoudn't do
delete m_cToolBar.  It's one or the other really.

0
 

Author Comment

by:yeshengl
ID: 7125480
Hi Migel,

Thank you so much for you excellent suggestion! The problem is solved! Yes, I think dynamically allocating class member works in this problem. Thanks!
0
 

Author Comment

by:yeshengl
ID: 7125481
Thank you very much, migel!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
if loop in java 3 130
abstract class with all non abstract mentods 6 76
changeXy challenge 13 69
Trying to run powershell  in a batch file. How do I do this? 8 68
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 …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now