描述:
大家好,我要写一个win32的DLL,需用到一个OCX控件,而且这个控件还有事件需要处理,我找了很多资料,都没有写的很清楚!在这里求大家帮帮忙给个简单的例子吧!
MFC的DLL也行!
解决方案1:
ocx在dll里可以直接添加为类,跟一般应用程序工程一样的。如果你要实现事件,建议你重新建一个ocx工程,原先的ocx功能接口可以由现在的ocx适配。
解决方案2: ocx 如果你regsvr32了
不可以 直接引入进来码
接口 会自动 生成的
// TestDll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "TestDll.h"
#include "DlgDBConnect.h"
#include "DlgRecordset.h"
#include "DlgTableDesign.h"
#include "DlgTest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HWND hMainWnd;
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CTestDllApp
BEGIN_MESSAGE_MAP(CTestDllApp, CWinApp)
//{{AFX_MSG_MAP(CTestDllApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDllApp construction
CTestDllApp::CTestDllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CTestDllApp object
CTestDllApp theApp;
BOOL bIsOpenDB(FALSE);
BOOL CTestDllApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
/* if (!AfxOleInit())
{
AfxMessageBox("ole ³õʼ»¯´íÎó");
return FALSE;
}*/
AfxEnableControlContainer();//
return CWinApp::InitInstance();
}
extern "C" __declspec(dllexport) void showDBConndlg(HWND hWndParent)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDlgDBConnect dlg(CWnd::FromHandle(hWndParent));
hMainWnd = hWndParent;
dlg.DoModal();
bIsOpenDB=TRUE;
}
extern "C" __declspec(dllexport) void showRecorddlg()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDlgRecordset dlg(CWnd::FromHandle(hMainWnd));
//hMainWnd=hWndParent;
if(bIsOpenDB)
dlg.DoModal();
else
::MessageBox(NULL,"Êý¾Ý¿âδ´ò¿ª£¬ÇëÏÈÁ¬½ÓÊý¾Ý¿â£¡","Ìáʾ",MB_OK);
}
extern "C" __declspec(dllexport) void closeconn()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// CDlgTest dlg(CWnd::FromHandle(hMainWnd));
//dlg.DoModal();
if(theApp.m_pConnection)
{
theApp.m_pConnection->Close();
theApp.m_pConnection.Release();
bIsOpenDB=FALSE;
}
}
extern "C" __declspec(dllexport) void showTableDesigndlg()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDlgTableDesign dlg(CWnd::FromHandle(hMainWnd));
//hMainWnd=hWndParent;
if(bIsOpenDB)
dlg.DoModal();
else
::MessageBox(NULL,"Êý¾Ý¿âδ´ò¿ª£¬ÇëÏÈÁ¬½ÓÊý¾Ý¿â£¡","Ìáʾ",MB_OK);
}
extern "C" __declspec(dllexport) void showTestdlg()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDlgTest dlg(AfxGetApp()->GetMainWnd());
//hMainWnd=hWndParent;
if(bIsOpenDB)
dlg.DoModal();
else
::MessageBox(NULL,"Êý¾Ý¿âδ´ò¿ª£¬ÇëÏÈÁ¬½ÓÊý¾Ý¿â£¡","Ìáʾ",MB_OK);
}