1 创建MFC动态链接库项目:8470
2 在资源视图中添加dialog资源并修改属性,名称
Border:None
ID:IDD_MYDLG
Style:Child
System Menu:False
3 右键添加类MyDialog
4 添加代码:
/*8470.h*/
extern "C" {
_declspec(dllexport) void WINAPI CreateMyDialog(HWND hwnd);
}
/*8470.cpp*/
void WINAPI CreateMyDialog(HWND hwnd) {
MyDialog *pMydlg = new MyDialog;
pMydlg->Create(IDD_MYDLG, CWnd::FromHandle(hwnd));
pMydlg->ShowWindow(SW_NORMAL);
}
4 生成8470.dll
5 创建winform应用,添加panel控件修改名称:dynamicPanel,并添加代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm8470
{
public partial class Form1 : Form
{
[DllImport("8470.dll")]
public static extern void CreatMyDlg(int hwnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int hwnd;
hwnd = dynamicPanel.Handle.ToInt32();
CreatMyDlg(hwnd);
}
}
}
6 将之前生成的8470.dll拷贝进应用程序目录下,运行
资源链接
https://download.csdn.net/download/weixin_43115795/89095288