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

ric*_*cky 5 wpf qt winforms-interop wpf-interop

我正在尝试创建 WPF GUI 应用程序来托管已经存在的 QT GUI 应用程序作为主 UI 的一部分。

QT 应用程序不需要处理鼠标/键盘输入。

我已经尝试过这个SO Post 中提到的方法。似乎所有这些方法都不适用于 QT 应用程序。

在此处输入图片说明

Luc*_*oni 1

我不知道这样做是否正确,但这就是我有时用来嵌入其他应用程序的方法(在互联网上找到):

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;
}
Run Code Online (Sandbox Code Playgroud)

只需根据您的需要修改此骨架即可。让我知道它是否有效。