主题:请教有关互斥量和非模式对话框唯一的问题
//点击按钮弹出非模式对话框:
void CMutexTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString strName;
m_edtName.GetWindowText(strName);
CMutexDlg *pdlg = new CMutexDlg(strName);
if(pdlg->GetSafeHwnd() == NULL)
{
if(pdlg->Create() == TRUE)
m_staticMsg.SetWindowText( strName + " 成功创建!");
else
m_staticMsg.SetWindowText( strName + " 已经存在!");
}
}
//非模式对话框内创建互斥量, 使对话框唯一,其中互斥量名为传入参数。
CMutexDlg::CMutexDlg(CString str)
{
//{{AFX_DATA_INIT(CMutexDlg)
m_strName = str;
//}}AFX_DATA_INIT
}
BOOL CMutexDlg::Create()
{
m_hMutex = CreateMutex(NULL,TRUE,m_strName);
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageBox("窗口已存在");
CloseHandle(m_hMutex);
return FALSE;
}
return CDialog::Create(CMutexDlg::IDD);
}
//关闭窗口 释放并关闭互斥量
void CMutexDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
ReleaseMutex(m_hMutex);
CloseHandle(m_hMutex);
CDialog::OnClose();
}
这样做确实能使非模式对话框唯一,但问题是关闭对话框以后没有释放互斥量,请问为什么?有什么解决方法没? 还有怎么通知父窗口该非模式对话框已经关闭?
void CMutexTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString strName;
m_edtName.GetWindowText(strName);
CMutexDlg *pdlg = new CMutexDlg(strName);
if(pdlg->GetSafeHwnd() == NULL)
{
if(pdlg->Create() == TRUE)
m_staticMsg.SetWindowText( strName + " 成功创建!");
else
m_staticMsg.SetWindowText( strName + " 已经存在!");
}
}
//非模式对话框内创建互斥量, 使对话框唯一,其中互斥量名为传入参数。
CMutexDlg::CMutexDlg(CString str)
{
//{{AFX_DATA_INIT(CMutexDlg)
m_strName = str;
//}}AFX_DATA_INIT
}
BOOL CMutexDlg::Create()
{
m_hMutex = CreateMutex(NULL,TRUE,m_strName);
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageBox("窗口已存在");
CloseHandle(m_hMutex);
return FALSE;
}
return CDialog::Create(CMutexDlg::IDD);
}
//关闭窗口 释放并关闭互斥量
void CMutexDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
ReleaseMutex(m_hMutex);
CloseHandle(m_hMutex);
CDialog::OnClose();
}
这样做确实能使非模式对话框唯一,但问题是关闭对话框以后没有释放互斥量,请问为什么?有什么解决方法没? 还有怎么通知父窗口该非模式对话框已经关闭?