yeshengl
asked on
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!!
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!!
ASKER
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()->LoadI con(IDI_IC ON_LEVEL_P ATIENT), FALSE);
// Get the client area before putting in Menu and ToolBar
CRect rcClientStart, rcClientNow;
GetClientRect(rcClientStar t);
if (!m_cToolBar.Create( this ) || !m_cToolBar.LoadToolBar(ID R_TOOLBAR_ WORKLIST) )
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// Put in ToolBar
RepositionBars(AFX_IDW_CON TROLBAR_FI RST, 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(r cChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffse t);
pwndChild->MoveWindow(rcCh ild, 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_CON TROLBAR_FI RST, 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();
}
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()->LoadI
// Get the client area before putting in Menu and ToolBar
CRect rcClientStart, rcClientNow;
GetClientRect(rcClientStar
if (!m_cToolBar.Create( this ) || !m_cToolBar.LoadToolBar(ID
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// Put in ToolBar
RepositionBars(AFX_IDW_CON
// 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,
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
{
pwndChild->GetWindowRect(r
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffse
pwndChild->MoveWindow(rcCh
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_CON
}
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();
}
I assume that m_cToolBar is class of CToolBar type?
so you should try this:
m_cToolBar.DestroyWindow() ;
Hope it helps...
so you should try this:
m_cToolBar.DestroyWindow()
Hope it helps...
ASKER
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(ID R_TOOLBAR_ WORKLIST) )
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
It seems the m_cToolBar object is still not destroyed. Thanks!
if (!m_cToolBar.Create( this ) || !m_cToolBar.LoadToolBar(ID
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
It seems the m_cToolBar object is still not destroyed. Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
not itsn`t
IMHO I use pointers while you use no dynamically allocated class member.
IMHO I use pointers while you use no dynamically allocated class member.
On BIG question.
I was looking at your code and see that you are using
if (!m_cToolBar.Create( this ) || !m_cToolBar.LoadToolBar(ID R_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.
I was looking at your code and see that you are using
if (!m_cToolBar.Create( this ) || !m_cToolBar.LoadToolBar(ID
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.
ASKER
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!
Thank you so much for you excellent suggestion! The problem is solved! Yes, I think dynamically allocating class member works in this problem. Thanks!
ASKER
Thank you very much, migel!
something like this:
in .h
class CMyClass
{
static bool m_bToolbarAlreadyCreated;
};
in.cpp
bool CMyClass::m_bToolbarAlread
in
CMyClass::OnInitDialog()
{
if(!CMyClass::m_bToolbarAl
{
// create toolbar
CMyClass::m_bToolbarAlread
}
}
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....