我遇到的问题:
我又一个A.exe调用B.dll中的对话框,对话框中有一个自定义控件。对话框在DoModal()时没有任何反应,OnInitDialog也没走。跟了一遍DoModal,猜测是资源没有对应好。随后做了测试,把B改为启动项,运行可以正常弹出对话框看到自定义控件,验证自定义控件的资源并没有被识别。
解决方法:
在A中调用对话框之前做了切换资源。ResourceHandleAuto rs("B.dll");
注册自定义控件 RegisterWndClass(LoadLibrary("B.dll"),"MyControl",CS_DBLCLKS, 0, 0);
然后再对话框DoModal();对话框和自定义控件正常显示。
部分代码:
class ResourceHandleAuto
{
public:
ResourceHandleAuto(const CString& sHandleName)
{
hOld = AfxGetResourceHandle();
AfxSetResourceHandle(LoadLibrary(sHandleName));
}
ResourceHandleAuto()
{
AfxSetResourceHandle(hOld);
}
private:
HINSTANCE hOld;
};
BOOL RegisterWndClass(HINSTANCE hInstance, LPCTSTR lpszClassName, UINT style, HICON hIcon, HBRUSH hbrBackground)
{
WNDCLASS wndcls;
ZeroMemory(&wndcls, sizeof(wndcls));
wndcls.style = style;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.hInstance = hInstance ? hInstance : XTPGetInstanceHandle();
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.lpszClassName = lpszClassName;
wndcls.hIcon = hIcon;
wndcls.hbrBackground = hbrBackground;
return AfxRegisterClass(&wndcls);
}