⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index148.htm

📁 一本不错的VC编程的参考书
💻 HTM
字号:
<html>
<style type="text/css"><!--
.p9 {  font-family: "宋体"; font-size: 9pt}a        {text-transform: none; text-decoration: none;}
a:hover {text-decoration: underline; color: #FF0000;}
--></style>
<body background="../di2001.jpg">
<h3 align="center"><font COLOR="#AOAO99"></font></h3>
<table width="100%" border="1" cellspacing="1">
<tr><td><p align="center"><font color="#FF0000">怎样从MFC扩展动态链结库(DLL)中显示一个对话框?</font></td></tr>
<tr><td><p>
</Br>
我在过去的几天中试着在DLL中定义的函数中显示一个对话框,可是已经在DLL中定义好的对话框资源,在常规DLL调用时,我可以正常的显示出来,为什么在扩展DLL中同样的资源我却不能显示.<Br>
</Br>
当你在DLL中使用资源时,有些小细节需要注意,首先,在DLL运行时,必须保存DLL的实例,可以通过AfxInitExtensionModule<Br>
</Br>
static AFX_EXTENSION_MODULE extensionDLL;<Br>
</Br>
extern "C" int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID)<Br>
{<Br>
&nbsp;&nbsp;&nbsp;if (dwReason == DLL_PROCESS_ATTACH)<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Extension DLL one-time initialization<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!AfxInitExtensionModule(extensionDLL, hInstance))<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<Br>
</Br>
&nbsp;&nbsp;&nbsp;return(true);<Br>
}<Br>
然后,每次使用DLL资源时,你必须改变资源的句柄,使其指向DLL,并保存exe的资源,以便以后正确恢复<Br>
</Br>
void get_DLL_resource(void)<Br>
{<Br>
&nbsp;&nbsp;&nbsp;/* this function changes the resource handle to that of the DLL */<Br>
&nbsp;&nbsp;&nbsp;//这个函数改变资源句柄使其指向DLL<Br>
&nbsp;&nbsp;&nbsp;if (resource_counter == 0)<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;save_hInstance = AfxGetResourceHandle();<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AfxSetResourceHandle(extensionDLL.hModule);<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<Br>
</Br>
&nbsp;&nbsp;&nbsp;resource_counter++;<Br>
}<Br>
接着你需要其它函数来恢复资源句柄<Br>
</Br>
void reset_DLL_resource(void)<Br>
{<Br>
&nbsp;&nbsp;&nbsp;/* this function restores the resource handle set by<Br>
'get_DLL_resource()' */<Br>
</Br>
&nbsp;&nbsp;&nbsp;if (resource_counter > 0)<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resource_counter--;<Br>
</Br>
&nbsp;&nbsp;&nbsp;if (resource_counter == 0)<Br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AfxSetResourceHandle(save_hInstance);<Br>
}<Br>
接下来一点非常重要,只要有可能就必须恢复资源句柄,否则,你将会遇到许多问题.原因是可执行文件必须重画工具条等等,比如说,如果用户移动DLL的对话框,如果资源句柄仍然为DLL的资源,程序就崩溃了,我发现最好恢复句柄的时机在对话框的OnInitDialog()中,这时对话框的模板等已经读出了.<Br>
</Br>
</Br>
</p></td></tr>
</table>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -