-
July 2nd, 2004, 05:53 PM
#1
How can I test an Object to see if it already exists?
REF : Visual C++ 6
Hello
I have created a dialog and it's class;
I created a member variable of the created dialog class in the main dialog;
CGraphDlg m_dlgGraph;
Later in the Main dialog code I create the second dialog and show it.
void CSmithGraphDlg::OnBopenGraph()
{
// TODO: Add your control notification handler code here
m_dlgGraph.Create(IDD_SGRAPH, this);
m_dlgGraph.ShowWindow(SW_SHOW);
}
Is there a way to test 'm_dlgGraph' variable, before I try and create the dialog object to see if is already attached to an existing dialog object?
if there is a method for this, will it work with other objects ?
thanks
-
July 5th, 2004, 12:53 PM
#2
You can test m_hWnd to see if it's valid or not.
Also, you can declare your variable dinamically:
Code:
CGraphDlg *m_dlgGraph = NULL;
and test it for NULL:
Code:
void CSmithGraphDlg::OnBopenGraph()
{
// TODO: Add your control notification handler code here
if(m_dlgGraph == NULL)
m_dlgGraph = new CGraphDlg();
m_dlgGraph->.Create(IDD_SGRAPH, this);
m_dlgGraph->ShowWindow(SW_SHOW);
}
This way you can delete ar recreate it over an over:
Code:
void CSmithGraphDlg::OnBdeleteGraph()
{
// TODO: Add your control notification handler code here
if(m_dlgGraph != NULL)
{
delete m_dlgGraph;
m_dlgGraph = NULL;
}
}
-
July 5th, 2004, 02:49 PM
#3
You can test whether there is another CGraphDlg active in the app by putting a static member in the CGraphDlg class.
DWORD m_dwCount;
This variable is shared by all objects of the CGraphDlg class because it is static. Initialize it at the beginning of the .cpp file:
DWORD CGraphDlg::m_dwCount = 0;
In the constructor of CGraphDlg:
m_dwCount++;
In the destructor of CGraphDlg:
m_dwCount--;
You could then:
CGraphDlg dlg; //Create object
if (dlg.m_dwCount > 1)
//There is already another object of the dlg class
Steve
-
July 5th, 2004, 04:39 PM
#4
Re: How can I test an Object to see if it already exists?
Originally posted by hickock6
Is there a way to test 'm_dlgGraph' variable, before I try and create the dialog object to see if is already attached to an existing dialog object?
Code:
if(!m_dlgGraph.GetSafeHwnd())
m_dlgGraph.Create(IDD_SGRAPH, this);
m_dlgGraph.ShowWindow(SW_SHOW);
Originally posted by hickock6
if there is a method for this, will it work with other objects ?
It will work with all 'CWnd' derived ones...
-
July 8th, 2004, 01:06 PM
#5
Thanks to all the answers :
Will let you know how it works out..
Agian thanks for the help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|