You are creating a modeless dialog. In this case you must track the state of that dialog. Your existing code can be used as starting point:
void CMy3D_Med_ViewerDlg::OnBnClickedShowDicomTag()
{
m_DCMTagDlg.Create(IDD_DICOM_Tags, this);
m_DCMTagDlg.ShowWindow(SW_SHOW);
GetDlgItem(IDC_SHOW_DICOM_TAG)->EnableWindow(FALSE);
}
By disabling the button it is grayed out and the handler is not called anymore when clicked again.
But when the dialog is closed you must now enable the button again. Do this by handling the
WM_CLOSE
message:
void DCMTagDlg::OnClose()
{
CMy3D_Med_ViewerDlg *p = (CMy3D_Med_ViewerDlg *)GetParent();
p->GetDlgItem(IDC_SHOW_DICOM_TAG)->EnableWindow(TRUE);
CDialog::OnClose();
}
Note that the above can be only used when the dialog is called only from
CMy3D_Med_ViewerDlg
.
There is also a different approach for this problem: The child dialog is never closed but only hidden:
void DCMTagDlg::OnClose()
{
ShowWindow(SW_HIDE);
}
This requires using a pointer to the dialog instead of an instance (just replace
DCMTagDlg m_DCMTagDlg
by
DCMTagDlg *m_pDCMTagDlg
and initialise the pointer with
NULL
in the constructor):
void CMy3D_Med_ViewerDlg::OnBnClickedShowDicomTag()
{
if (m_pDCMTagDlg == NULL)
{
m_pDCMTagDlg = new DCMTagDlg();
m_pDCMTagDlg->Create(IDD_DICOM_Tags, this);
}
m_pDCMTagDlg->ShowWindow(SW_SHOW);
}
Note that you now have to destroy the dialog when the parent window is closed:
void CMy3D_Med_ViewerDlg::OnClose()
{
if (m_pDCMTagDlg)
{
m_pDCMTagDlg->Destroy();
delete m_pDCMTagDlg;
m_pDCMTagDlg = NULL;
}
CDialog::OnClose();
}
When using this you may still disable the button. When not doing so, clicking on the button will just show the existing dialog.