非模态对话矿如何自杀?

king_koo 2002-03-15 03:21:37
我做了个查找对话框,查完后,
要在那里delete this呢?我试过各种方法都得到:
Warning: calling DestroyWindow in CDialog::~CDialog --
OnDestroy or PostNcDestroy in derived class will not be called.
...全文
49 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Xsean 2002-03-19
  • 打赏
  • 举报
回复
to king_koo (向东) :我试了一下,确实有warning ,你知道原因了吗?不过我觉得对程序运行没什么影响吧
rovoboy 2002-03-15
  • 打赏
  • 举报
回复
一个实用的报警框,允许多个同时出现


#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 dialog

IMPLEMENT_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 handlers

void 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");
king_koo 2002-03-15
  • 打赏
  • 举报
回复
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
Xsean 2002-03-15
  • 打赏
  • 举报
回复
重载OnDestroy,在里面delete this
然后,在OnOK里加上
EndDialod(1);
DestroyWindow();
tpProgramer 2002-03-15
  • 打赏
  • 举报
回复
一般不是使用 delete this来自杀的。
对非模态的对话框,可以采用像主窗口发消息的方式来关闭自己,这个方法你不愿意采用。那就不用它(其实这种方式挺好的)。

也可以采用先使用GetMainWnd()来得到主窗口指针的方式,由于非模态对话框肯定是主窗口的一个成员(一般是指针),所以就可以使用指向主窗口的指针再获得指向非模态对话框的指针(如果指针是主窗口类的非public成员,就必须再加一个函数来返回这个指针),调用这个指针的DestroyWindow方法就可以了,然后删除这两个指针。此时非模态对话框已经销毁了。
e_notimpl 2002-03-15
  • 打赏
  • 举报
回复
TN017 in MSDN
Enjoy it.
kook 2002-03-15
  • 打赏
  • 举报
回复
杀它干什么?退出时析构不是delete了嘛
new之前判断pDlg是否NULL,若已Create了, ShowWindow就可以了
ahr 2002-03-15
  • 打赏
  • 举报
回复
我的PostNcDestroy中delete this,怎么没waring呀?
pigczc 2002-03-15
  • 打赏
  • 举报
回复
OnCancel()
111222 2002-03-15
  • 打赏
  • 举报
回复
king_koo(向东) :

自杀你SetTimer一下啊
king_koo 2002-03-15
  • 打赏
  • 举报
回复
to kook:
我不是要在父窗里杀他,我要他自杀。怎么办?
king_koo 2002-03-15
  • 打赏
  • 举报
回复
我知道要,delete this可是会出先waring啊;
DestroyWindow也是过,还是waring;
whz_time 2002-03-15
  • 打赏
  • 举报
回复
delete this;
111222 2002-03-15
  • 打赏
  • 举报
回复
哦,我刚明白你问的问题的意思。

你建立WM_CLOSE消息的映射函数(ClassWizard会使吧)
在里面DestroyWindow();//注释掉CDialog::OnClose();
111222 2002-03-15
  • 打赏
  • 举报
回复
不要在西狗函数里调用DestroyWindow
111222 2002-03-15
  • 打赏
  • 举报
回复


DestroyWindow();
kook 2002-03-15
  • 打赏
  • 举报
回复
可以做一个成员变量CDlg* pDlg
构造函数中 pDlg=NULL;
析构函数中 if(pDlg!=NULL) delete pDlg
调用(//大体):
pDlg=new CDlg;
pDlg->Create(....)
pDlg->(SW_SHOW);

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧