vs创建dll,然后在dll里面创建对话框,并通过导出函数的方式创建时,会碰到各种各样的问题。比如edit控件获取不了焦点,无法输入;子对话框无法移动;各种button的焦点混乱等各种问题。
原因分析:
dll中创建的mfc对话框底层有自己的窗口map列表,exe中的某些消息有可能传递不到dll中,因此需要将exe中的程序传递到dll,具体的底层原因,以后在分析加入
//dll中导出函数:
BOOL WINAPI FilterDllMsg(LPMSG lpMsg)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
TRY
{
return AfxGetThread()->PreTranslateMessage(lpMsg);
}
END_TRY
return FALSE;
}
//如果是dll导出的非子对话框在exe的app中如下调用:
BOOL CScreenCapApp::PreTranslateMessage(MSG* pMsg)
{
if (CWinApp::PreTranslateMessage(pMsg))
return(TRUE);
return(FilterDllMsg(pMsg));
}
// 如果dll导出的是子对话框,则需要在相应父对话框的PreTranslateMessage内调用,如下:
BOOL CYourDialog::PreTranslateMessage(MSG* pMsg)
{
// 传递给子对话框的消息和传递给父对话框的消息是有交集的,因此注意某些消息需要只交给一方处理。
// ? 不只为何先处理Prexxx,再调用Filter不行
if (FilterDllMsg(pMsg))
return(TRUE);
return(CDialogEx::PreTranslateMessage(pMsg));
}