To encapsulate the pop-up window in the MFC dynamic library, you first need to select "MFC Extension DLL" when MFC creates the dynamic library:
After creation, create a new dialog box, and create a class for the dialog box to bind to it, and then the various coding methods of the dialog box are the same as the MFC application. After completing the pop-up window logic, remember to give the class and class in the header file. Add a __declspec(dllexport) between the names to export the class and compile it. However, this usage is because another application must include the header file of the dialog box, which causes the application to include both the resource.h used by the application itself and the resource.h used by the dynamic library, so when defining the resource name, try to Don't repeat it, but I have tried it. Even if I repeat it, there will be a warning when compiling, but it can still be compiled and run successfully. For example, in the example code I posted, IDD_LOGIN is the conflicting macro definition that I deliberately modified. .
First post the various files of the dynamic library:
resource.h of the dynamic library:
//{{NO_DEPENDENCIES}} // Microsoft Developer Studio generated include file. // Used by dlgdll.rc // #define IDD_LOGIN 3000 #define IDC_UNAME 3000 #define IDC_UPASS 3001 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 3001 #define _APS_NEXT_COMMAND_VALUE 32771 #define _APS_NEXT_CONTROL_VALUE 3002 #define _APS_NEXT_SYMED_VALUE 3000 #endif #endif
Login.h of the dynamic library
#if !defined(AFX_LOGIN_H__E89E0B64_27D5_4647_9BFF_DBEFF5152794__INCLUDED_) #define AFX_LOGIN_H__E89E0B64_27D5_4647_9BFF_DBEFF5152794__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "resource.h" / // CLogin dialog class __declspec(dllexport) CLogin : public CDialog { // Construction public: CLogin(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CLogin) enum { IDD = IDD_LOGIN }; // NOTE: the ClassWizard will add data members here //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CLogin) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: // Generated message map functions //{{AFX_MSG(CLogin) virtual void OnOK(); //}}AFX_MSG DECLARE_MESSAGE_MAP() public: CString GetUserName() {return m_username;} CString GetPassword() {return m_password;} private: CString m_username; CString m_password; }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_LOGIN_H__E89E0B64_27D5_4647_9BFF_DBEFF5152794__INCLUDED_)
Login.cpp of the dynamic library, enter the user name and password, click the OK button, before the pop-up window disappears, save the user name and password into the private member variable of the object.
// Login.cpp : implementation file // #include "stdafx.h" #include "Login.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif / // CLogin dialog CLogin::CLogin(CWnd* pParent /*=NULL*/) : CDialog(CLogin::IDD, pParent) { //{{AFX_DATA_INIT(CLogin) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } void CLogin::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CLogin) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CLogin, CDialog) //{{AFX_MSG_MAP(CLogin) //}}AFX_MSG_MAP END_MESSAGE_MAP() / // CLogin message handlers void CLogin::OnOK() { CString username, password; GetDlgItemText(IDC_UNAME, username); GetDlgItemText(IDC_UPASS, password); if (username.IsEmpty()) { MessageBox("please enter user name"); return; } if (password.IsEmpty()) { MessageBox("Please enter password"); return; } m_username = username; m_password = password; CDialog::OnOK(); }
Dynamic library dialog:
Here's the MFC application calling the DLL method:
First, add a login button to the dialog box of the pop-up application:
Add the import header file and dynamic library to the StdAfx.h header file:
#include "../dlgdll/Login.h" #ifdef _DEBUG #pragma comment(lib, "../dlgdll/Debug/dlgdll.lib") #else #pragma comment(lib, "../dlgdll/Release/dlgdll.lib") #endif
Double-click the login button, write the response function: call the dialog box, if the user presses the OK button and the pop-up window disappears, then the user name and password can be retrieved from the object.
void CTestdllDlg::OnLogin() { CLogin dlg; int result = dlg.DoModal(); if (result == IDOK) { MessageBox("username:" + dlg.GetUserName() + " password:" + dlg.GetPassword()); } }
Effect:
Compilation log:
--------------------Configuration: testdll - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
testdll.cpp
d:\vscode\testdll\resource.h(9) : warning C4005: 'IDD_LOGIN' : macro redefinition
d:\vscode\dlgdll\resource.h(5) : see previous definition of 'IDD_LOGIN'
testdllDlg.cpp
d:\vscode\testdll\resource.h(9) : warning C4005: 'IDD_LOGIN' : macro redefinition
d:\vscode\dlgdll\resource.h(5) : see previous definition of 'IDD_LOGIN'
Generating Code...
Linking...
Creating library Debug/testdll.lib and object Debug/testdll.exp
testdll.exe - 0 error(s), 2 warning(s)