非模态对话框DLL

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/rmaly/article/details/21460727

最近在弄一个DLL,涉及到一个非模态对话框

对话框的建立无区模态非模态,区别在于创建的方式。模态对话框是使用DoModal的方式创建,而非模态对话框的方式是:

CDialog *pDlg = new CDialog(父窗口指针);

pDlg->Create(ID, 父窗口指针);

pDlg->ShowWindow(SW_SHOW);

这样非模态对话框便显示出来了

然后……

关闭对话框后发现一大堆内存泄露……

资源释放,解决方法:

在对话框类中重写PostNcDestroy

PostNcDestroy中添加delete this,释放自身资源,当然其中还可以添加一些你想要释放的资源

OK,搞掂了,在外面给对话框发送一个WM_DESTROY消息过来,资源释放问题解决了。


展开阅读全文

模态对话框出错,模态对话框

08-17

我先说一下我创建非模态对话框的思路,rn资源视图中创建一个对话框并创建类Coption,在菜单中加入一个按键coption,rn在CChildView类中创建一个Coption类对象指针,在CChildView类中用Coption类对象指针创建非模态对话框,并判断Coption类对象指针是否为null,如果不为null,则不创建,将焦点给非模态对话框就行,创建的时候,将CChildView类作为Coption类构造参数传入Coption类中,当非模态对话框点击取消时,销毁非模态对话框,并发送消息给CChildView类,让他将Coption类对象指针赋值为null,否则非模态对话框只能出来一次,但是现在发送消息会报错,rn下面贴出四个文件代码:rnrn```rnCoption.h:rn#pragma oncernrnrn// Coption 对话框rnrnclass Coption : public CDialogExrnrn DECLARE_DYNAMIC(Coption)rnrnpublic:rn Coption(CWnd* pParent = nullptr); // 标准构造函数rn Coption(CView* pview); // 标准构造函数rn virtual ~Coption();rnrn// 对话框数据rn#ifdef AFX_DESIGN_TIMErn enum IDD = IDD_DIALOG1 ;rn#endifrnrnprotected:rn virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持rnrn DECLARE_MESSAGE_MAP()rnpublic:rn afx_msg void OnBnClickedCancel();rn virtual void PostNcDestroy();rnprivate:rn CView* p_view;rnpublic:rn afx_msg void OnBnClickedOk();rn;rnrn```rnCoption.cpp:rn// Coption.cpp: 实现文件rn//rnrn#include "stdafx.h"rn#include "MFCApplication5.h"rn#include "Coption.h"rn#include "afxdialogex.h"rnrnrn// Coption 对话框rnrnIMPLEMENT_DYNAMIC(Coption, CDialogEx)rnrnCoption::Coption(CWnd* pParent /*=nullptr*/)rn : CDialogEx(IDD_DIALOG1, pParent)rnrnrnrnrnCoption::Coption(CView* pview /*=nullptr*/)rnrn p_view = pview;rnrnrnCoption::~Coption()rnrnrnrnrnvoid Coption::DoDataExchange(CDataExchange* pDX)rnrn CDialogEx::DoDataExchange(pDX);rnrnrnrnBEGIN_MESSAGE_MAP(Coption, CDialogEx)rn ON_BN_CLICKED(IDCANCEL, &Coption::OnBnClickedCancel)rn ON_BN_CLICKED(IDOK, &Coption::OnBnClickedOk)rnEND_MESSAGE_MAP()rnrnrn// Coption 消息处理程序rnrnrnvoid Coption::OnBnClickedCancel()rnrn // TODO: 在此添加控件通知处理程序代码rn rn //CDialogEx::OnCancel();rn DestroyWindow();rnrnrnrnrnvoid Coption::PostNcDestroy()rnrn // TODO: 在此添加专用代码和/或调用基类rn CDialogEx::PostNcDestroy();rnrn p_view->SendMessage(WM_GOODBYE, 0,0); //屏蔽这一行就不会报错 rnrnrnrnrnrnvoid Coption::OnBnClickedOk()rnrn // TODO: 在此添加控件通知处理程序代码rn //CDialogEx::OnOK();rn DestroyWindow();rnrnrnrn```rnCChildView.cpp:rnrn// ChildView.cpp: CChildView 类的实现rn//rnrn#include "stdafx.h"rn#include "MFCApplication5.h"rn#include "ChildView.h"rn#include "resource.h" // 主符号rnrn#ifdef _DEBUGrn#define new DEBUG_NEWrn#endifrnrnrn// CChildViewrnrnCChildView::CChildView()rnrn m_option = NULL;rnrnrnrnCChildView::~CChildView()rnrnrnrnrnBEGIN_MESSAGE_MAP(CChildView, CWnd)rn ON_WM_PAINT()rn ON_COMMAND(ID_32771, &CChildView::On32771)rn ON_MESSAGE(WM_GOODBYE,ON_GOODBYE)rnEND_MESSAGE_MAP()rn rnLRESULT CChildView::ON_GOODBYE(WPARAM wparam, LPARAM lparam)rnrn /*delete m_option;*/rn m_option = NULL;rn rn return 0;rn rnrn// CChildView 消息处理程序rnrnBOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) rnrn if (!CWnd::PreCreateWindow(cs))rn return FALSE;rnrn cs.dwExStyle |= WS_EX_CLIENTEDGE;rn cs.style &= ~WS_BORDER;rn cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, rn ::LoadCursor(nullptr, IDC_ARROW), reinterpret_cast(COLOR_WINDOW+1), nullptr);rnrn return TRUE;rnrnrnvoid CChildView::OnPaint() rnrn CPaintDC dc(this); // 用于绘制的设备上下文rn rn // TODO: 在此处添加消息处理程序代码rn rn // 不要为绘制消息而调用 CWnd::OnPaint()rnrnrnrnrnvoid CChildView::On32771()rnrn if (NULL==m_option)rn rn /*SetFocus();rn MessageBox(_T("m_option不为空"));*/rn m_option = new Coption;rn m_option->Create(IDD_DIALOG1, this);rn MessageBox(_T("m_option为空"));rn rn rn rn m_option->ShowWindow(SW_SHOW);rn rn rn // TODO: 在此添加命令处理程序代码rnrnrnrn```rnCChildView.h:rnrn// ChildView.h: CChildView 类的接口rn//rnrnrn#pragma oncern#include"Coption.h"rnrn// CChildView 窗口rnrnclass CChildView : public CWndrnrn// 构造rnpublic:rn CChildView();rnrn// 特性rnpublic:rnrn// 操作rnpublic:rnrn// 重写rn protected:rn virtual BOOL PreCreateWindow(CREATESTRUCT& cs);rnrn// 实现rnpublic:rn virtual ~CChildView();rnrn // 生成的消息映射函数rnprotected:rn afx_msg void OnPaint();rn DECLARE_MESSAGE_MAP()rnprotected:rn Coption* m_option;rnpublic:rn afx_msg void On32771();rn afx_msg LRESULT ON_GOODBYE(WPARAM wparam, LPARAM lparam);rn;rnrnrn```rn报错如下:rn![图片说明](https://img-ask.csdn.net/upload/201908/15/1565872965_493989.png)rnrn如果实在没人愿意回答,讲一下编写非模态对话框心得也行 问答

没有更多推荐了,返回首页