MFC中弹出对话框

VS2010 C++ 中新的弹出对话框

1 添加头函数

#include "afxdialogex.h"
#include "afxtaskdialog.h"

2 代码如下

        //qiangguo 2010-5-26
	CString message("My message to the user");
	CString dialogTitle("My Task Dialog title");
	CString emptyString;
	// TODO: Add your control notification handler code here
	CString expandedLabel("Hide extra information");
	CString collapsedLabel("Show extra information");
	CString expansionInfo("This is the additional information to the user,/nextended over two lines.");


	if (CTaskDialog::IsSupported())
	{
		// 1 直接使用
		HRESULT result2 = CTaskDialog::ShowDialog(L"My error message", L"Error", L"New Title", TEMP_LINE1, TEMP_LINE2);

		// 2 详细设置
	    CTaskDialog taskDialog(message, emptyString, dialogTitle, TDCBF_OK_BUTTON);
		taskDialog.SetMainIcon(TD_WARNING_ICON);
		//taskDialog.DoModal();

		taskDialog.SetMainInstruction(L"Warning");
		taskDialog.SetCommonButtons(TDCBF_YES_BUTTON | TDCBF_NO_BUTTON | TDCBF_CANCEL_BUTTON);
		taskDialog.LoadCommandControls(TEMP_LINE1, TEMP_LINE2);
		taskDialog.SetExpansionArea(expansionInfo, collapsedLabel, expandedLabel);
		taskDialog.SetFooterText(L"This is the a small footnote to the user");
		taskDialog.SetVerificationCheckboxText(L"Remember your selection");
		INT_PTR result = taskDialog.DoModal();

		if (taskDialog.GetVerificationCheckboxState() )
		{
		   // PROCESS IF the user selects the verification checkbox 
		}
		switch (result)
		{
		   case TEMP_LINE1:
			  // PROCESS IF the first command line
			  break;
		   case TEMP_LINE2:
			  // PROCESS IF the second command line
			  break;
		   case IDYES:
			  // PROCESS IF the user clicks yes
			  break;
		   case IDNO:
			  // PROCESS IF the user clicks no
			  break;
		   case IDCANCEL:
			  // PROCESS IF the user clicks cancel
			  break;
		   default:
			  // This case should not be hit because closing the dialog box results in IDCANCEL
			  break;
		}
	}
	else
	{
	   AfxMessageBox(message);
	}
### MFC 应用程序中因内存不足而弹出对话框的解决方案 在开发MFC应用程序时,遇到内存不足的情况是一个常见的挑战。为了妥善处理这种情况并提供良好的用户体验,可以采取以下措施: #### 1. 捕获内存分配失败异常 通过捕获 `new` 或者 `malloc` 调用抛出的标准 C++ 异常(如 `std::bad_alloc`),可以在发生内存不足的情况下执行特定的操作。 ```cpp try { // 尝试分配大量内存 char *largeBuffer = new char[HUGE_BUFFER_SIZE]; } catch (const std::bad_alloc& e) { // 处理内存不足错误 } ``` #### 2. 使用全局新运算符替换 可以通过定义自己的全局 `operator new` 来监控所有的动态内存请求,并在其内部实现额外的日志记录或警告机制。 ```cpp void* operator new(size_t size) throw(std::bad_alloc){ void* pMem; while ((pMem = malloc(size)) == NULL && !IsOutOfMemory()){ HandleLowMemoryWarning(); } if (!pMem) throw std::bad_alloc(); return pMem; } bool IsOutOfMemory(){ static int lowMemoryCountdown = LOW_MEMORY_WARNING_COUNTDOWN; --lowMemoryCountdown; return lowMemoryCountdown <= 0; } void HandleLowMemoryWarning(){ AfxMessageBox(_T("Warning: Low on memory!")); } ``` #### 3. 创建专门用于报告内存问题的消息框 如果检测到了严重的内存短缺状况,应该向用户提供清晰的信息说明当前状态以及建议他们如何继续操作。这通常涉及到创建一个自定义的消息框来展示这些信息。 ```cpp if (_heapmin() != 0 || _CrtDumpMemoryLeaks()) { // 发生了内存泄漏或其他严重情况 CString message; message.Format(_T("The application has encountered a critical error due to insufficient memory.\n\nPlease save your work and restart the program.")); MessageBox(message, _T("Critical Error"), MB_ICONERROR | MB_OK); } ``` #### 4. 实现资源回收策略 对于长时间运行的应用程序来说,在每次尝试分配大块连续空间之前先释放不再使用的对象可以帮助缓解短期内的压力。定期清理缓存和其他临时数据结构也是有效的做法之一。 以上方法能够有效地帮助开发者应对可能出现的内存紧张局面,并确保即使在这种极端条件下也能给最终用户传达适当的通知[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值