图8-4 添加类对话框
按“添加”钮,弹出“MFC类向导”对话框。在“类名”栏中键入“CCompute”,在“基类”下拉式列表,选“CObject”,参见图8-5。按“完成”钮,添加该类到ExtDll项目。
图8-5 MFC类向导对话框
8.2.3编写导出类代码我们将整个CCompute类设为导出类,并在里面添加2个成员变量、1个构造函数和4个用于四则运算的成员函数,外加1个演示导出函数的取模全局函数Mod。
下面是CCompute类的头文件(Compute.h),其中红色的部分是自己添加:(注意导出宏AFX_EXT_CLASS的使用)
#pragma once
// CCompute 命令目标
class AFX_EXT_CLASS CCompute : public CObject
{
public:
int m_data1, m_data2;
public:
CCompute();
CCompute(int d1, int d2);
virtual ~CCompute();
public:
int Add();
int Sub();
int Mul();
double Div();
};
AFX_EXT_CLASS int Mod(int d1, int d2);
下面是CCompute类的代码源文件(Compute.cpp),其中红色为自己添加的部分:
// Compute.cpp : 实现文件
//
#include "stdafx.h"
#include "Compute.h"
// CCompute
CCompute::CCompute()
{
}
CCompute::CCompute(int d1, int d2)
{
m_data1 = d1;
m_data2 = d2;
}
CCompute::~CCompute()
{
}
// CCompute 成员函数
int CCompute::Add()
{
return m_data1 m_data2;
}
int CCompute::Sub()
{
return m_data1 - m_data2;
}
int CCompute::Mul()
{
return m_data1 * m_data2;
}
double CCompute::Div()
{
if (m_data2 == 0 ) {
AfxMessageBox(L"Divided by zero!");
return 0;
}
return (double)m_data1 / m_data2;
}
int Mod(int d1, int d2)
{
if (d2 == 0 ) {
AfxMessageBox(L"Modulo by zero!");
return 0;
}
return d1 % d2;
}