Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

The QT application don't need to deal with mouse/keyboard input.

I've tried approach mentioned in this SO Post. Seems all those approach does not work for a QT application.

enter image description here

like image 885
ricky Avatar asked Nov 09 '22 08:11

ricky


1 Answers

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.

like image 53
Luca Angioloni Avatar answered Nov 15 '22 06:11

Luca Angioloni