C# 嵌入Qt窗口

需求:将Qt 窗口 封装成dll ,供c#嵌入

一,利用第三方库 将qt 窗口封装成dll,详见 qtwinmigrate\examples\qtdll 例子

 https://github.com/qtproject/qt-solutions

#include "widget.h"
#include <qmfcapp.h>
#include <qwinwidget.h>
#include <windows.h>
#include <QApplication>

BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpvReserved*/ )
{
    static bool ownApplication = FALSE;

    if ( dwReason == DLL_PROCESS_ATTACH )
    ownApplication = QMfcApp::pluginInstance( hInstance );
    if ( dwReason == DLL_PROCESS_DETACH && ownApplication )
    delete qApp;

    return TRUE;
}

QWinWidget *win=nullptr;
extern "C" __declspec(dllexport) bool initWindow( HWND parent )
{
    win = new QWinWidget(parent);
    Widget *widget = new Widget(win);
    widget->show();
    win->move(0,0);
    win->show();

    return TRUE;
}

extern "C" __declspec(dllexport) bool destroyWindow( HWND parent )
{
    if(win!=0){
        delete win;
    }

    return TRUE;
}

二,c# 直接引用 dll 并执行接口

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WinFormsApp
{
    public partial class Form1 : Form
    {

        [DllImport("VTKWindow.dll", EntryPoint = "initWindow", CharSet = CharSet.Ansi)]
        public extern static bool initWindow(IntPtr parent);

        [DllImport("VTKWindow.dll", EntryPoint = "destroyWindow", CharSet = CharSet.Ansi)]
        public extern static bool destroyWindow();
        public Form1()
        {
            InitializeComponent();
            initWindow(this.Handle);
        }

    }
}

参考:C# 调用Qt编写的控件_zuoyefeng1990的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/weixin_38416696/article/details/129213549