这是个老外写的例子,挺不错的,有学习的意义!特别要注意加粗字段,就是有时候问题所在!
Create Skeleton of the Dll
Use MFC AppWizard to create an extension dll's skeleton
Select MFC Extension Dll
Click Finish. As you see on this dialog, Main Source code in mcExtnDll.h and cpp
Adding Dialog Resource
Add a new dialog by using ResourceView. Add three edit controls, three static controls and two buttons. Now dialog should look like this:
Make sure to check Number style of all three edit controls.
Adding Dialog Class
Now Add a new class CmcDlg derived from CDialog by using Insert->New Class menu from your main menu. Make sure the Dialog ID is IDD_DIALOG1, your dialog's resource ID.
Now write click handler for Add and Multiply buttons by double clicking on the buttons.
This action adds OnAdd and OnMultiply functions to the dialog class.
Adding Code to the OnAdd and OnMultiply Functions
Now write this code to the OnAdd and OnMultiply functions. OnAdd function reads the values of first and second edit controls and write the sum to the third edit control. And OnMultiply function reads the values of first and second edit controls and write the multiplication of these numbers to the third edit control.
void CmcDlg::OnAdd()
{
int iVar1 = GetDlgItemInt( IDC_EDIT1 );
int iVar2 = GetDlgItemInt( IDC_EDIT2 );
int iRes = iVar1 + iVar2 ;
SetDlgItemInt( IDC_EDIT3, iRes, TRUE );
UpdateData(FALSE);
}
void CmcDlg::OnMultiply()
{
int iVar1 = GetDlgItemInt( IDC_EDIT1 );
int iVar2 = GetDlgItemInt( IDC_EDIT2 );
int iRes = iVar1 * iVar2 ;
SetDlgItemInt( IDC_EDIT3, iRes, TRUE );
UpdateData(FALSE);
}
Adding a Class and Its Members
In my project I add a new class called ExportData. I will add all of my export functions to this class.
Add a new class Right click on mcExtnDll Classes in Class View and Click New Class. Select Generic Class from the drop-down, type your class name and click OK.
This action adds a new class ExportData to your project. It has only a constructor and a destructor. Now lets add member functions to this class.
Add Member Functions Right click on the ExportData class and Click Add Member Functions and add a function, CallDlg.
This function will call the dialog and we will call this function from the client program. Include "mcdlg.h" and add code on CallDlg function. This function creates an instance of CmcDlg class and calls DoModal() of CDialog.
// ExportData.cpp: implementation of the ExportData class.
#include "stdafx.h"
#include "mcdlg.h"
#include "ExportData.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// Construction/Destruction
ExportData::ExportData()
{
}
ExportData::~ExportData()
{
}
void ExportData::CallDlg()
{
CmcDlg dlg;
dlg.DoModal();
}
Edit ExportData.h Class
Double click on ExportData.h class and edit AFX_EXT_CLASS in front of ExportData. See bold part here.
class AFX_EXT_CLASS ExportData
{
public:
void CallDlg();
ExportData();
virtual ~ExportData();
};
Build the Project and Copy the DLL: Read It
Now compile and build the project. You will get three compilation errors.
ExportData.cpp
h:/code/mcextndlg/mcdlg.h(21) : error C2065: 'IDD_DIALOG1' : undeclared identifierh:/code/mcextndlg/mcdlg.h(21) : error C2057: expected constant expression
mcDlg.cpp
H:/Code/mcExtnDlg/mcDlg.cpp(5) : fatal error C1083: Cannot open include file: ' / add additional includes here': Invalid argument
Generating Code...
Error executing cl.exe.
Open your stdafx.h and #include "resource.h" in the last of all include files and delete #include " / add additional includes here" line from mcDlg.cpp class. That will include IDD_DIALOG1 resource to your project. You can go to the same line by double clicking on error # C1083. See bold part in the errors.
Now again build the project. Hope this time you got no errors.
Now copy the dll mcExtnDlg.dll from your Release (You will get exception in Debug version. Its a Microsoft bug) directory to the Winnt/System32 ( Windows/System for Windows 98 ) directory or your client exe's current directory. I copy dll to client exe's current directory because I don't want mess in my Windows/System directory.[u]A DLL Test Client Program
I have built an MDI application to test my dll. I have added one menu item Test. Write a handler for this menu item by using ClassWizard.
Edit MainFrame Class
1. Copy ExportData.h file from your DLL's project directory.
3. Link the Library. Copy mcExtnDlg.lib from your DLL's Release directory to your test project directory and enter the path in Object/library modules text box.
4. Include header file. Include 'exportdata.h' in your project's stdafx.h file or mainframe.h.
#endif // _AFX_NO_AFXCMN_SUPPORT
#include "exportdata.h"
//{{AFX_INSERT_LOCATION}}
5. Create Object of the class. Create an object of ExportData and call its CallDlg method on OnTest.
void CMainFrame::OnTest()
{
ExportData dll;
dll.CallDlg();
}
7. Build and Run your project. Now build and run your application and Click Test. You will get this dialog. Add two numbers in Value 1 and Value 2 fields and click Add or Multiply buttons and see the result.
一个MFC Extension DLL的Dialog例子
最新推荐文章于 2020-12-22 16:46:41 发布