MFC中调用DoModal()后窗体未弹出 之Rich Edit 2.0控件惹的祸

MFC中调用DoModal()后窗体未弹出之Rich Edit 2.0控件惹的祸
一.分析思路
1.场景
通过窗体1中的某个Button单击事件,希望能弹出一个新的对话框。
2.实现流程
1)创建一个对话框2,并对此对话框添加类;
2)在窗体1中添加Button1
3)在窗体1的类中添加Button1的单击事件响应函数在这里插入图片描述
4)在3)绑定的函数中调用对话框2
void CGeneralSymbolDlg::OnBUTTONSaveAsSymbol()
{
if( !m_pSymbolSelected )
return;

CString strSymLibFolder = YYcat::GetSymbolPath(0);
strSymLibFolder += "\\GeoSymbol.sym";

DlgSymbolSave dlgSymSave = new DlgSymbolSave(this);
dlgSymSave.PutDefaultSymbolLibFolder(strSymLibFolder);
if(dlgSymSave.DoModal() != IDC_SaveSymbolOk)
{
return; //调试代码在此处直接return了
}
CString strName,strFilePath;
long lCode;
dlgSymSave.GetSymbolName(&strName);
dlgSymSave.GetSymbolCode(&lCode);
dlgSymSave.GetSymolLibFilePath(&strFilePath);

ISymbolPtr pSymbol = NULL;
IClonePtr ptrNewSym = pSymbol;
IClonePtr pClone = m_pSymbolSelected;
pClone->Clone(&ptrNewSym);

CComBSTR bstrName,bstrFilePath;
bstrName = strName.GetBuffer(0);
bstrFilePath = strFilePath.GetBuffer(0);

pSymbol->put_Name(bstrName);
pSymbol->put_Code(lCode);

VARIANT_BOOL varExcuteOk = VARIANT_FALSE;
IGeoSymbolLibPtr pGeoSymbolLib(CLSID_GeoSymbolLib);
pGeoSymbolLib->put_SymbolLibPath(bstrFilePath);
pGeoSymbolLib->Add(pSymbol);
pGeoSymbolLib->Save();

ListSymbols();
::MessageBox(NULL, _T("符号另存为成功!"), _T("信息"), MB_OK);
return;

}

3.解决过程
1、重新新建一个空的对话框3,什么控件都不加时,调用DoModal(),结果正常
2、在1的基础上拖Button、Text、Edit控件,调用DoModal(),结果正常。
3、在2的基础上拖Rich Edit 2.0控件,调用DoModal(),结果错误。
至此发现是【Rich Edit 2.0控件】导致的MFC窗体调用DoModal()窗体无法弹出。
4、在网上搜索解决方案
解决办法是在应用程序类的InitInstance函数体内添加以下语句:
AfxInitRichEdit()?/初始化richedit控件
亲自实验,可以解决问题!
在这里插入图片描述
*备注:当跨工程调用弹出的MFC窗体异常时,在DoModal调用之前添加 AFX_MANAGE_STATE(AfxGetStaticModuleState()); 可解决问题。

二.查资料*
官方解释:
If you are using a rich edit control in a dialog box (regardless whether your application is SDI, MDI, or dialog-based), you must call AfxInitRichEdit once before the dialog box is displayed. A typical place to call this function is in your program’s InitInstance member function. You do not need to call it for each time you display the dialog box, only the first time. You do not have to call AfxInitRichEdit if you are working with CRichEditView.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值