可以做一个成员变量CDlg* pDlg
构造函数中 pDlg=NULL;
析构函数中 if(pDlg!=NULL) delete pDlg
调用(//大体):
pDlg=new CDlg;
pDlg->Create(....)
pDlg->(SW_SHOW);
构造函数中 pDlg=NULL;
析构函数中 if(pDlg!=NULL) delete pDlg
调用(//大体):
pDlg=new CDlg;
pDlg->Create(....)
pDlg->(SW_SHOW);
调试欢乐多
在里面DestroyWindow();//注释掉CDialog::OnClose();
DestroyWindow也是过,还是waring;
我不是要在父窗里杀他,我要他自杀。怎么办?
new之前判断pDlg是否NULL,若已Create了, ShowWindow就可以了
Enjoy it.
对非模态的对话框,可以采用像主窗口发消息的方式来关闭自己,这个方法你不愿意采用。那就不用它(其实这种方式挺好的)。也可以采用先使用GetMainWnd()来得到主窗口指针的方式,由于非模态对话框肯定是主窗口的一个成员(一般是指针),所以就可以使用指向主窗口的指针再获得指向非模态对话框的指针(如果指针是主窗口类的非public成员,就必须再加一个函数来返回这个指针),调用这个指针的DestroyWindow方法就可以了,然后删除这两个指针。此时非模态对话框已经销毁了。
然后,在OnOK里加上
EndDialod(1);
DestroyWindow();
我这样响应按扭的:
static int st=0;
if(st){delete dlg;st=0;return;}
dlg=new FineNext;
dlg->Create(IDD_FINENEXT,NULL);
dlg->ShowWindow(1);st=1;
还是waring
#if !defined(AFX_ALERTBOX_H__57D61BB6_F53F_4F3F_B421_68397B98F854__INCLUDED_)
#define AFX_ALERTBOX_H__57D61BB6_F53F_4F3F_B421_68397B98F854__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// AlertBox.h : header file
///////////////////////////////////////////////////////////////////////////////
// CAlertBox dialog
#include "Label.h"
class CAlertBox : public CDialog
{
// Construction
public:
DECLARE_DYNCREATE(CAlertBox)
~CAlertBox();
CAlertBox(CString Str);
void MessageOut(CString Str);
CAlertBox(CWnd* pParent = NULL); // standard constructor// Dialog Data
//{{AFX_DATA(CAlertBox)
enum { IDD = IDD_ALERT_MESSAGE_BOX };
CLabel m_Message;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAlertBox)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
void OnCancel();
void OnOK(); // Generated message map functions
//{{AFX_MSG(CAlertBox)
afx_msg void OnKillFocus(CWnd* pNewWnd);
virtual BOOL OnInitDialog();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_ALERTBOX_H__57D61BB6_F53F_4F3F_B421_68397B98F854__INCLUDED_)
// AlertBox.cpp : implementation file
//#include "stdafx.h"
#include "AlertBox.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAlertBox dialogIMPLEMENT_DYNAMIC(CAlertBox,CDialog)CAlertBox::CAlertBox(CWnd* pParent /*=NULL*/)
: CDialog(CAlertBox::IDD, pParent)
{
//{{AFX_DATA_INIT(CAlertBox)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CAlertBox::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAlertBox)
DDX_Control(pDX, IDC_STATIC_MESSAGE_INFO, m_Message);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAlertBox, CDialog)
//{{AFX_MSG_MAP(CAlertBox)
ON_WM_KILLFOCUS()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CAlertBox message handlersvoid CAlertBox::OnKillFocus(CWnd* pNewWnd)
{
CDialog::OnKillFocus(pNewWnd);
// TODO: Add your message handler code here
this->SetActiveWindow();
}void CAlertBox::MessageOut(CString Str)
{
Create(IDD_ALERT_MESSAGE_BOX);
m_Message.SetText(Str);
m_Message.SetTextColor(RGB(255,20,10));
m_Message.SetFontSize(18);
CenterWindow();
ShowWindow(SW_SHOW);
}void CAlertBox::OnOK()
{
CDialog::OnOK();
delete this;
}void CAlertBox::OnCancel()
{}CAlertBox::CAlertBox(CString Str)
{
MessageOut(Str);
}BOOL CAlertBox::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SetTimer(10,1000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}void CAlertBox::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CWnd *pWnd;
pWnd=this->GetActiveWindow();
if(pWnd!=NULL)
if(pWnd->GetRuntimeClass()->m_lpszClassName!=CString("CAlertBox"))
this->SetActiveWindow();
CDialog::OnTimer(nIDEvent);
}CAlertBox::~CAlertBox()
{
}//使用时
new CAlertBox("xxx");