简体   繁体   English

是否可以将QT应用程序托管到WPF应用程序中?

[英]Is it possible to host a QT application into a WPF application?

I'm trying to create WPF GUI application to host an already exist QT GUI application as part of the main UI. 我正在尝试创建WPF GUI应用程序来托管已存在的QT GUI应用程序作为主UI的一部分。

The QT application don't need to deal with mouse/keyboard input. QT应用程序不需要处理鼠标/键盘输入。

I've tried approach mentioned in this SO Post . 我试过这个SO帖子中提到的方法。 Seems all those approach does not work for a QT application. 似乎所有这些方法都不适用于QT应用程序。

在此输入图像描述

I don't know if it's a right thing to do, but that's what I used some times to embedd other apps (found on the internet): 我不知道这是否是正确的做法,但这是我用了一些时间来嵌入其他应用程序(在互联网上找到):

public partial class MainWindow : Window
{
private Process _process;

[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);

[DllImport("user32")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int GWL_STYLE = -16;
private const int WS_CAPTION = 0x00C00000;
private const int WS_THICKFRAME = 0x00040000;
const string patran = "patran";
public MainWindow()
{
    InitializeComponent();

    Loaded += (s, e) => LaunchChildProcess();
}

private void LaunchChildProcess()
{
    _process = Process.Start("/path/to/QtExecutable.exe");
    _process.WaitForInputIdle();

    var helper = new WindowInteropHelper(this);

    SetParent(_process.MainWindowHandle, helper.Handle);

    // remove control box
    int style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
    style = style & ~WS_CAPTION & ~WS_THICKFRAME;
    SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);
    // resize embedded application & refresh
    ResizeEmbeddedApp();
}

private void ResizeEmbeddedApp()
{
    if (_process == null)
        return;
    SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)ActualWidth, (int)ActualHeight, SWP_NOZORDER | SWP_NOACTIVATE);
}

protected override Size MeasureOverride(Size availableSize)
{
    Size size = base.MeasureOverride(availableSize);
    ResizeEmbeddedApp();
    return size;
}

Just modify this skeleton to your necessities. 只需将此骨架修改为您的必需品。 Let me know if it works. 如果有效,请告诉我。

Yes. 是。 It is possible. 有可能的。 A very simple way for your quick start without the effort of coding. 一种非常简单的快速启动方式,无需编码。

Check this link: Hosting EXE Applications in a WPF Window Application ( http://www.codeproject.com/Tips/673701/Hosting-EXE-Applications-in-a-WPF-Window-Applicati ). 检查此链接:在WPF窗口应用程序中托管EXE应用程序( http://www.codeproject.com/Tips/673701/Hosting-EXE-Applications-in-a-WPF-Window-Applicati )。 Download the project. 下载项目。 Find "notepad.exe" in the project and replace it with the file name of your QT application. 在项目中找到“notepad.exe”并将其替换为QT应用程序的文件名。 Just a remind: for the WPF exe to launch the QT application, you may need to take care of the environment variables required by QT. 提醒一下:对于WPF exe来启动QT应用程序,您可能需要处理QT所需的环境变量。

What it looks like: 它看起来像什么: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM