📄 vc++动态链接库编程之mfc规则dll-开发者网络-vc-天极yes2ky.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0040)http://dev.yesky.com/241/2144741_1.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>VC++动态链接库编程之MFC规则DLL-开发者网络-VC-天极Yesky</TITLE>
<META content=VC++动态链接库编程之MFC规则DLL, name=description>
<META content=VC++动态链接库编程之MFC规则DLL, name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all
href="VC++动态链接库编程之MFC规则DLL-开发者网络-VC-天极Yes2ky.files/y_mobile_content.css"
type=text/css rel=stylesheet>
<SCRIPT src="VC++动态链接库编程之MFC规则DLL-开发者网络-VC-天极Yes2ky.files/showlay.js"></SCRIPT>
<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A
href="http://product.yesky.com/">产品报价</A>| <A
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A>
| <A href="http://digital.yesky.com/">数码</A> | <A
href="http://notebook.yesky.com/">笔记本</A> | <A
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A>
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A>
| <A href="http://dh.yesky.com/">数字家庭</A> | <A
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> |
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A>
| <A href="http://pic.yesky.com/">图片</A> | <A
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> |
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A>
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户
src="VC++动态链接库编程之MFC规则DLL-开发者网络-VC-天极Yes2ky.files/logo.gif" border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select()
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广东</A> <A
href="http://xa.yesky.com/">西安</A><BR><A href="http://cq.yesky.com/">重庆</A> <A
href="http://cd.yesky.com/">成都</A> <A href="http://sy.yesky.com/">沈阳</A> <A
href="http://js.yesky.com/">江苏</A> </DIV>
<DIV class=rightarea_right><A
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT src="VC++动态链接库编程之MFC规则DLL-开发者网络-VC-天极Yes2ky.files/content_text_ad.js"
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="VC++动态链接库编程之MFC规则DLL-开发者网络-VC-天极Yes2ky.files/flash.js"
type=text/javascript></SCRIPT>
<DIV id=childmenu><A href="http://soft.yesky.com/">软件频道></A><A
href="http://dev.yesky.com/">程序开发></A><A
href="http://dev.yesky.com/devjava/">Java</A><A
href="http://dev.yesky.com/ysdevvb">VB</A><A
href="http://dev.yesky.com/ysdevvc">VC</A><A
href="http://dev.yesky.com/ysdevdelphi">Delphi</A><A
href="http://dev.yesky.com/ysdevc">C/C++</A><A
href="http://dev.yesky.com/web/">Web开发</A><A
href="http://dev.yesky.com/msdn/">微软专栏</A><A
href="http://dev.yesky.com/devydkf/">移动</A><A
href="http://dev.yesky.com/devsjk/">数据库</A><A
href="http://dev.yesky.com/ysdevcxrs">程序人生</A><A
href="http://dev.yesky.com/ysdevrjgc">软件工程</A>|<A
href="http://soft.yesky.com/lesson/">产品中心</A><A
href="http://www.mydown.com/soft/develop/news.shtml">下载</A><A
href="http://q.yesky.com/group/202019"><FONT color=#ff0000>群乐:<FONT
face=Verdana>Vista系统</FONT></FONT></A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/">天极网</A> > <A
href="http://dev.yesky.com/">开发频道</A> > VC++动态链接库编程之MFC规则DLL</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://dev.yesky.com/241/2144741_1.shtml#pls"
target=_self>评论</A></DIV>
<DIV class=kr></DIV>
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
</DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>VC++动态链接库编程之MFC规则DLL</H1></DIV>
<DIV id=conauthor><SPAN>2005-10-09 10:52 </SPAN><SPAN>作者: 宋宝华 </SPAN><SPAN>出处:
作者授权转载 </SPAN><SPAN>责任编辑:<A title=向本编辑提问
href="http://comments.yesky.com/t/·½ÖÛ/6,324/2144741.shtml" target=_blank>方舟</A>
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN id=contentAdv></SPAN></DIV> 5.3
一个简单的MFC规则DLL<BR><BR> 这个DLL的例子(属于静态链接到MFC 的规则DLL)中提供了一个如图11所示的对话框。<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG
src="VC++动态链接库编程之MFC规则DLL-开发者网络-VC-天极Yes2ky.files/6hb0hlv43ptr.jpg"
border=0><BR>图11
MFC规则DLL例子</DIV></TD></TR></TBODY></TABLE><BR> 在DLL中添加对话框的方式与在MFC应用程序中是一样的。
在图11所示DLL中的对话框的Hello按钮上点击时将MessageBox一个“Hello,pconline的网友”对话框,下面是相关的文件及源代码,其中删除了MFC向导自动生成的绝大多数注释(下载本工程):<BR><BR> 第一组文件:CWinApp继承类的声明与实现<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>// RegularDll.h : main header file for the REGULARDLL DLL<BR><BR>#if
!defined(AFX_REGULARDLL_H__3E9CB22B_588B_4388_B778_B3416ADB79B3__INCLUDED_)<BR>#define
AFX_REGULARDLL_H__3E9CB22B_588B_4388_B778_B3416ADB79B3__INCLUDED_<BR><BR>#if
_MSC_VER > 1000<BR>#pragma once<BR>#endif // _MSC_VER >
1000<BR><BR>#ifndef __AFXWIN_H__<BR>#error include ’stdafx.h’ before
including this file for PCH<BR>#endif<BR>#include "resource.h" // main
symbols<BR><BR>class CRegularDllApp : public
CWinApp<BR>{<BR> public:<BR> CRegularDllApp();<BR> DECLARE_MESSAGE_MAP()<BR>};<BR><BR>#endif
<BR><BR>// RegularDll.cpp : Defines the initialization routines for the
DLL.<BR><BR>#include "stdafx.h"<BR>#include "RegularDll.h"<BR><BR>#ifdef
_DEBUG<BR>#define new DEBUG_NEW<BR>#undef THIS_FILE<BR>static char
THIS_FILE[] = __FILE__;<BR>#endif<BR><BR>BEGIN_MESSAGE_MAP(CRegularDllApp,
CWinApp)<BR>END_MESSAGE_MAP()<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR><BR>//
CRegularDllApp
construction<BR><BR>CRegularDllApp::CRegularDllApp()<BR>{<BR>}<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR>//
The one and only CRegularDllApp object<BR><BR>CRegularDllApp
theApp;</TD></TR></TBODY></TABLE><BR> 分析:<BR><BR> 在这一组文件中定义了一个继承自CWinApp的类CRegularDllApp,并同时定义了其的一个实例theApp。乍一看,您会以为它是一个MFC应用程序,因为MFC应用程序也包含这样的在工程名后添加“App”组成类名的类(并继承自CWinApp类),也定义了这个类的一个全局实例theApp。<BR><BR> 我们知道,在MFC应用程序中CWinApp取代了SDK程序中WinMain的地位,SDK程序WinMain所完成的工作由CWinApp的三个函数完成:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>virtual BOOL InitApplication( );<BR><BR>virtual BOOL InitInstance(
);<BR><BR>virtual BOOL Run( ); //传说中MFC程序的“活水源头”
</TD></TR></TBODY></TABLE><BR> 但是MFC规则DLL并不是MFC应用程序,它所继承自CWinApp的类不包含消息循环。这是因为,MFC规则DLL不包含CWinApp::Run机制,主消息泵仍然由应用程序拥有。如果DLL
生成无模式对话框或有自己的主框架窗口,则应用程序的主消息泵必须调用从DLL
导出的函数来调用PreTranslateMessage成员函数。<BR><BR> 另外,MFC规则DLL与MFC 应用程序中一样,需要将所有
DLL中元素的初始化放到InitInstance 成员函数中。<BR><BR> 第二组文件 自定义对话框类声明及实现<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>#if
!defined(AFX_DLLDIALOG_H__CEA4C6AF_245D_48A6_B11A_A5521EAD7C4E__INCLUDED_)<BR>#define
AFX_DLLDIALOG_H__CEA4C6AF_245D_48A6_B11A_A5521EAD7C4E__INCLUDED_<BR><BR>#if
_MSC_VER > 1000<BR>#pragma once<BR>#endif // _MSC_VER > 1000<BR>//
DllDialog.h : header
file<BR>/////////////////////////////////////////////////////////////////////////////<BR>//
CDllDialog dialog<BR><BR>class CDllDialog : public CDialog<BR>{<BR> //
Construction<BR> public:<BR> CDllDialog(CWnd* pParent = NULL); //
standard constructor<BR> enum { IDD = IDD_DLL_DIALOG
};<BR> protected:<BR> virtual void DoDataExchange(CDataExchange* pDX); //
DDX/DDV support<BR> // Implementation<BR> protected:<BR> afx_msg void
OnHelloButton();<BR> DECLARE_MESSAGE_MAP()<BR>};<BR>#endif <BR><BR>//
DllDialog.cpp : implementation file<BR><BR>#include "stdafx.h"<BR>#include
"RegularDll.h"<BR>#include "DllDialog.h"<BR>#ifdef _DEBUG<BR>#define new
DEBUG_NEW<BR>#undef THIS_FILE<BR>static char THIS_FILE[] =
__FILE__;<BR>#endif<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR>//
CDllDialog dialog<BR><BR>CDllDialog::CDllDialog(CWnd* pParent
/*=NULL*/)<BR>: CDialog(CDllDialog::IDD, pParent)<BR>{}<BR><BR>void
CDllDialog::DoDataExchange(CDataExchange*
pDX)<BR>{<BR> CDialog::DoDataExchange(pDX);<BR>}<BR><BR>BEGIN_MESSAGE_MAP(CDllDialog,
CDialog)<BR> ON_BN_CLICKED(IDC_HELLO_BUTTON,
OnHelloButton)<BR>END_MESSAGE_MAP()<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR>//
CDllDialog message handlers<BR><BR>void CDllDialog::OnHelloButton()
<BR>{<BR> MessageBox("Hello,pconline的网友","pconline");<BR>}</TD></TR></TBODY></TABLE><BR> 分析:<BR><BR> 这一部分的编程与一般的应用程序根本没有什么不同,我们照样可以利用MFC类向导来自动为对话框上的控件添加事件。MFC类向导照样会生成类似ON_BN_CLICKED(IDC_HELLO_BUTTON,
OnHelloButton)的消息映射宏。<BR><BR> 第三组文件 DLL中的资源文件<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>//{{NO_DEPENDENCIES}}<BR><BR>// Microsoft Developer Studio generated
include file.<BR><BR>// Used by RegularDll.rc<BR><BR>//<BR><BR>#define
IDD_DLL_DIALOG 1000<BR><BR>#define IDC_HELLO_BUTTON
1000</TD></TR></TBODY></TABLE><BR> 分析:<BR><BR> 在MFC规则DLL中使用资源也与在MFC应用程序中使用资源没有什么不同,我们照样可以用Visual
C++的资源编辑工具进行资源的添加、删除和属性的更改。<BR><BR> 第四组文件 MFC规则DLL接口函数<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>#include "StdAfx.h"<BR>#include "DllDialog.h"<BR><BR>extern "C"
__declspec(dllexport) void ShowDlg(void) <BR>{<BR> CDllDialog
dllDialog;<BR> dllDialog.DoModal();<BR>}</TD></TR></TBODY></TABLE><BR> 分析:<BR><BR> 这个接口并不使用MFC,但是在其中却可以调用MFC扩展类CdllDialog的函数,这体现了“规则”的概类。<BR><BR> 与非MFC
DLL完全相同,我们可以使用__declspec(dllexport)声明或在.def中引出的方式导出MFC规则DLL中的接口。<BR><BR>
<DIV class=right>共4页。 <A class=page
href="http://dev.yesky.com/241/2144741.shtml" target=_self><FONT
face=Webdings>9</FONT></A> <A class=page
href="http://dev.yesky.com/241/2144741.shtml" target=_self><FONT
face=Webdings>7</FONT></A> <A class=page
href="http://dev.yesky.com/241/2144741.shtml" target=_self><B>1</B></A> <A
class=page href="http://dev.yesky.com/241/2144741_1.shtml" target=_self><FONT
color=#666666><B>2</B></FONT></A> <A class=page
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -