网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

VC++动态链接库(DLL)编程深入浅出(四)

http://www.diybl.com/ 2008-1-14  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

 MFC扩展DLL的内涵为MFC的扩展,用户使用MFC扩展DLL就像使用MFC本身的DLL一样。除了可以在MFC扩展DLL的内部使用MFC以外, MFC扩展DLL与应用程序的接口部分也可以是MFC。我们一般使用MFC扩展DLL来包含一些MFC的增强功能,譬如扩展MFC的CStatic、 CButton等类使之具备更强大的能力。

 

  使用Visual C++向导生产MFC扩展DLL时,MFC向导会自动增加DLL的入口函数DllMain:

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
 // Remove this if you use lpReserved
 UNREFERENCED_PARAMETER(lpReserved);

 if (dwReason == DLL_PROCESS_ATTACH)
 {
  TRACE0("MFCEXPENDDLL.DLL Initializing!\n");

  // Extension DLL one-time initialization
  if (!AfxInitExtensionModule(MfcexpenddllDLL, hInstance))
   return 0;

  // Insert this DLL into the resource chain
  // NOTE: If this Extension DLL is being implicitly linked to by
  //  an MFC Regular DLL (such as an ActiveX Control)
  //  instead of an MFC application, then you will want to
  //  remove this line from DllMain and put it in a separate
  //  function exported from this Extension DLL.  The Regular DLL
  //  that uses this Extension DLL should then explicitly call that
  //  function to initialize this Extension DLL.  Otherwise,
  //  the CDynLinkLibrary object will not be attached to the
  //  Regular DLL''s resource chain, and serious problems will
  //  result.

  new CDynLinkLibrary(MfcexpenddllDLL);
 }
 else if (dwReason == DLL_PROCESS_DETACH)
 {
  TRACE0("MFCEXPENDDLL.DLL Terminating!\n");
  // Terminate the library before destructors are called
  AfxTermExtensionModule(MfcexpenddllDLL);
 }
 return 1;   // ok
}

  上述代码完成MFC扩展DLL的初始化和终止处理。

  由于MFC扩展DLL导出函数和变量的方式与其它DLL没有什么区别,我们不再细致讲解。下面直接给出一个MFC扩展DLL的创建及在应用程序中调用它的例子。

6.1 MFC扩展DLL的创建

  下面我们将在MFC扩展DLL中 导出一个按钮类CSXButton(扩展自MFC的CButton类),类CSXButton是一个用以取代 CButton的类,它使你能在同一个按钮上显示位图和文字,而MFC的按钮仅可显示二者之一。类CSXbutton的源代码在Internet上广泛流 传,有很好的“群众基础”,因此用这个类来讲解MFC扩展DLL有其特殊的功效。

  MFC中包含一些宏,这些宏在DLL和调用DLL的应用程序中被以不同的方式展开,这使得在DLL和应用程序中,使用统一的一个宏就可以表示出输出和输入的不同意思:

// for data
#ifndef AFX_DATA_EXPORT
 #define AFX_DATA_EXPORT __declspec(dllexport)
#endif
#ifndef AFX_DATA_IMPORT
 #define AFX_DATA_IMPORT __declspec(dllimport)
#endif

// for classes
#ifndef AFX_CLASS_EXPORT
 #define AFX_CLASS_EXPORT __declspec(dllexport)
#endif
#ifndef AFX_CLASS_IMPORT
 #define AFX_CLASS_IMPORT __declspec(dllimport)
#endif

// for global APIs
#ifndef AFX_API_EXPORT
 #define AFX_API_EXPORT __declspec(dllexport)
#endif
#ifndef AFX_API_IMPORT
 #define AFX_API_IMPORT __declspec(dllimport)
#endif

#ifndef AFX_EXT_DATA
 #ifdef _AFXEXT
  #define AFX_EXT_CLASS       AFX_CLASS_EXPORT
  #define AFX_EXT_API         AFX_API_EXPORT
  #define AFX_EXT_DATA        AFX_DATA_EXPORT
  #define AFX_EXT_DATADEF
 #else
  #define AFX_EXT_CLASS       AFX_CLASS_IMPORT
  #define AFX_EXT_API         AFX_API_IMPORT
  #define AFX_EXT_DATA        AFX_DATA_IMPORT
  #define AFX_EXT_DATADEF
 #endif
#endif

  导出一个类,直接在类声明头文件中使用AFX_EXT_CLASS即可,以下是导出CSXButton类的例子:

#ifndef _SXBUTTON_H
#define _SXBUTTON_H

#define SXBUTTON_CENTER -1

class AFX_EXT_CLASS CSXButton : public CButton
{
// Construction
public:
 CSXButton();

// Attributes
private:
 // Positioning
 BOOL  m_bUseOffset;    
 CPoint  m_pointImage;
 CPoint  m_pointText;
 int   m_nImageOffsetFromBorder;
 int   m_nTextOffsetFromImage;

 // Image
 HICON  m_hIcon;     
 HBITMAP  m_hBitmap;
 HBITMAP  m_hBitmapDisabled;
 int   m_nImageWidth, m_nImageHeight;

 // Color Tab
 char  m_bColorTab;    
 COLORREF m_crColorTab;

 // State
 BOOL  m_bDefault;
 UINT  m_nOldAction;
 UINT  m_nOldState;
 
// Operations
public:
 // Positioning
 int  SetImageOffset( int nPixels );
 int  SetTextOffset( int nPixels );
 CPoint SetImagePos( CPoint p );
 CPoint SetTextPos( CPoint p );

 // Image
 BOOL SetIcon( UINT nID, int nWidth, int nHeight );
 BOOL SetBitmap( UINT nID, int nWidth, int nHeight );
 BOOL SetMaskedBitmap( UINT nID, int nWidth, int nHeight, COLORREF crTransparentMask );
 BOOL HasImage() { return (BOOL)( m_hIcon != 0  | m_hBitmap != 0 ); }

 // Color Tab
 void SetColorTab(COLORREF crTab);

 // State
 BOOL SetDefaultButton( BOOL bState = TRUE );
private:
 BOOL SetBitmapCommon( UINT nID, int nWidth, int nHeight, COLORREF crTransparentMask,

欢迎光临DIY部落,点击这里查看更多文章教程   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言