可以做一个成员变量CDlg* pDlg
构造函数中  pDlg=NULL;
析构函数中  if(pDlg!=NULL) delete pDlg
调用(//大体):
    pDlg=new CDlg;
    pDlg->Create(....)
    pDlg->(SW_SHOW);

解决方案 »

  1.   

    不要在西狗函数里调用DestroyWindow
      

  2.   

    哦,我刚明白你问的问题的意思。你建立WM_CLOSE消息的映射函数(ClassWizard会使吧)
    在里面DestroyWindow();//注释掉CDialog::OnClose();
      

  3.   

    我知道要,delete this可是会出先waring啊;
    DestroyWindow也是过,还是waring;
      

  4.   

    to kook:
       我不是要在父窗里杀他,我要他自杀。怎么办?
      

  5.   

    king_koo(向东) :   自杀你SetTimer一下啊
      

  6.   

    我的PostNcDestroy中delete this,怎么没waring呀?
      

  7.   

    杀它干什么?退出时析构不是delete了嘛
    new之前判断pDlg是否NULL,若已Create了, ShowWindow就可以了
      

  8.   

    TN017 in MSDN
    Enjoy it.
      

  9.   

    一般不是使用 delete this来自杀的。
    对非模态的对话框,可以采用像主窗口发消息的方式来关闭自己,这个方法你不愿意采用。那就不用它(其实这种方式挺好的)。也可以采用先使用GetMainWnd()来得到主窗口指针的方式,由于非模态对话框肯定是主窗口的一个成员(一般是指针),所以就可以使用指向主窗口的指针再获得指向非模态对话框的指针(如果指针是主窗口类的非public成员,就必须再加一个函数来返回这个指针),调用这个指针的DestroyWindow方法就可以了,然后删除这两个指针。此时非模态对话框已经销毁了。
      

  10.   

    重载OnDestroy,在里面delete this
    然后,在OnOK里加上
    EndDialod(1);
    DestroyWindow();
      

  11.   

    Xsean() :按你的方法还是有那个waring tpProgramer:主窗口发消息的方式来关闭自己?
             我这样响应按扭的:
             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
      

  12.   

    一个实用的报警框,允许多个同时出现
    #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");
      

  13.   

    to king_koo (向东) :我试了一下,确实有warning ,你知道原因了吗?不过我觉得对程序运行没什么影响吧