加载包含控件类的dll

将控件或窗体类包含在DLL中,然后导出函数,此函数创建控件到主窗口上。
/* @breif 加载包含控件的dll,并显示控件
*  同时在当前工程下添加控件类的.h文件
*  复制dll到当前工程下
*  日期:2016.5.30
*/
#ifndef __LOADDLL__
#define __LOADDLL__
#include <vcl.h>
#include <windows.h>

typedef  TMetaClass * (__stdcall * pCreateControl )(TApplication*,TForm*,TScreen*);

template <class T>
class  TLoadControlDll
{
    private:
       HINSTANCE   m_handle;
       pCreateControl  m_create_control_fuction;
       TMetaClass * m_pClass;

       T  * m_control;
       
    public:
       //构造函数,加载dll
       TLoadControlDll(const char * libName);
       //获取创建控件类函数
       void GetFunction(const char * functionName);
       //创建控件并显示
       void Show(TForm * m);
       
       ~TLoadControlDll();
};

template <class T>
TLoadControlDll<T>::TLoadControlDll(const char * libName)
{
    m_handle = LoadLibraryA(libName);
}

template <class T>
void TLoadControlDll<T>::GetFunction(const char * functionName)
{
    m_create_control_fuction = (pCreateControl)GetProcAddress(m_handle, functionName);
}

template <class T>
void TLoadControlDll<T>::Show(TForm * m)
{
    m_pClass = m_create_control_fuction(Application,m,Screen);
    Application->CreateForm(m_pClass, &m_control);
    m_control->Parent = m;
    m_control->Show();
}

template <class T>
TLoadControlDll<T>::~TLoadControlDll()
{
    delete m_control;
    FreeLibrary(m_handle); 
}
#endif
在主窗体中调用类,创建控件,并显示在主窗口上,代码如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "mainUnit1.h"
#include "loaddll.h"
#include "scalewin.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//dll中的包含的类TMyFrame1
//通过.h文件引入
TMyFrame1 * p1;
//dll引导类
TLoadControlDll<TMyFrame1> * pCreateMainFrame;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
   //调用dll中的创建控件类
   pCreateMainFrame = new TLoadControlDll<TMyFrame1>("MainFrame.dll");
   pCreateMainFrame->GetFunction("getScaleFrame");
   pCreateMainFrame->Show(this);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    delete pCreateMainFrame;
}
//---------------------------------------------------------------------------
工程需要带包编译和发行。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值