視圖類(lèi)調(diào)用非模態(tài)對(duì)話時(shí),如何銷(xiāo)毀分配的內(nèi)存
簡(jiǎn)要步驟:
1、視圖類(lèi)在調(diào)用非模態(tài)對(duì)話框時(shí),將自身的指針傳遞給非模態(tài)對(duì)話框。
2、當(dāng)非模態(tài)對(duì)話框結(jié)束時(shí),向視圖類(lèi)發(fā)送自定義消息,由自定義消息處理函數(shù)來(lái)釋放內(nèi)存。
具體代碼步驟:
1、定義自定義消息 #define WM_RELEASEPLAYSOUNDDC (WM_USER+1001)
2、修改非模態(tài)對(duì)話框類(lèi)的構(gòu)造函數(shù),便于將視圖指針傳遞進(jìn)去。
非模態(tài)對(duì)話框.h文件中
class CTestDialog : public CDialog
{
DECLARE_DYNAMIC(CPlaySound)
public:
//CTestDialog (CWnd* pParent = NULL); // standard constructor
CTestDialog (CView* pParent); // standard constructor
virtual ~CTestDialog ();
CView *pView;
// Dialog Data
enum { IDD = IDD_TEST_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg LRESULT OnNcHitTest(CPoint point);
};
非模態(tài)對(duì)話框.cpp文件中IMPLEMENT_DYNAMIC(CTestDialog, CDialog)
//CTestDialog::CTestDialog(CWnd* pParent /*=NULL*/)
CTestDialog::CTestDialog(CView* pParent)
: CDialog(CTestDialog::IDD, pParent)
{
pView = pParent;
}
3、在非模態(tài)對(duì)話框結(jié)束的時(shí)候發(fā)送自定義消息void CTestDialog::OnBnClickedBtnClose()
{
// TODO: Add your control notification handler code here
pView->PostMessage(WM_RELEASEPLAYSOUNDDC, 0, 0);
OnOK();
}
4、在視圖類(lèi).h文件中,增加定義:
afx_msg LRESULT OnReleaseDialog(WPARAM wparam,LPARAM lparam);
5、在視圖類(lèi).cpp文件中,增加消息映射部分:
ON_MESSAGE(WM_DIALOGOK,OnDialogOk)
6、在視圖類(lèi).cpp文件中定義消息處理函數(shù)的實(shí)現(xiàn):
LRESULT CTestView::OnReleaseDialog(WPARAM wparam,LPARAM lparam)
{
if (m_pPlaySound != NULL)
{
delete m_pPlaySound;
m_pPlaySound = NULL;
}
return 0;
}
posted on 2014-03-07 15:01
王海光 閱讀(1767)
評(píng)論(0) 編輯 收藏 引用 所屬分類(lèi):
MFC