进一步再举一例:本例要实现一个绘制一个椭圆形功能
<1> : 根据前一篇"vs2010 MFC DLL创建和调用"文章新建MFC Dll工程,假设工程名CMFCDLLDemo02;
<2> : 在CMFCDLLDemo02.h中添加:
#include "resource.h" // main symbols
// CCMFCDLLDemo02App
// See CMFCDLLDemo02.cpp for the implementation of this class
//
extern "C" void PASCAL EXPORT DrawEllipse( CRect rect, CDC *pDC );
class CCMFCDLLDemo02App : public CWinApp
{
public:
CCMFCDLLDemo02App();
// Overrides
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
<3> : 然后在CMFCDLLDemo02.cpp中添加函数体:
extern "C" void PASCAL EXPORT DrawEllipse( CRect rect, CDC *pDC)
{
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,255));
pDC->SelectObject(&brush);
pDC->Ellipse(&rect);
}
<4> : def文件配置:
; CMFCDLLDemo02.def : Declares the module parameters for the DLL.
LIBRARY "MFCDLLDEMO"
EXPORTS
; Explicit exports can go here
DrawEllipse
<5> : ->build CMFCDLLDemo02工程,既可以.
<6> : 调用MFCDLLDEMO.dll,新建工程一个MFC工程(假设单文档工程吧).
<7> : 在视窗view的OnDraw函数中增加:
void CMFCApView::OnDraw(CDC* pDC) { // DLL MFC Library function call CRect rect; rect.top=10; rect.left=10; rect.right=200; rect.bottom=200; DrawEllipse(rect,pDC); // DLL class object call int a, b, c; CString str; DLLclass classFromDLL; classFromDLL.Arg = 6; a = classFromDLL.Add(3, 2); b = classFromDLL.Sub(3, 2); c = classFromDLL.Arg; // Display data in window int y = 250, dy; TEXTMETRIC tm; pDC->GetTextMetrics(&tm); dy = tm.tmHeight + tm.tmExternalLeading; str.Format("DLL class Add function return: %d", a); pDC->TextOut(20, y, str); y += dy; str.Format("DLL class Sub function return: %d", b); pDC->TextOut(20, y, str); y += dy; str.Format("DLL class Arg Variable return: %d", c); pDC->TextOut(20, y, str); y += dy; a = DLLArg; b = DLLfun2(30); str.Format("DLL class Arg Variable return: %d", a); pDC->TextOut(20, y, str); y += dy; str.Format("DLL function \"DLLfun2\" return: %d", b); pDC->TextOut(20, y, str); }
<8> 编译运行,既可以看见椭圆体图形.