5

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

Community
  • 1
  • 1
ricky
  • 2,058
  • 4
  • 23
  • 49

2 Answers2

1

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.

Luca Angioloni
  • 2,243
  • 2
  • 19
  • 28
  • Thanks Luca, it works for me, although there is still some minor issues. I'll accept it as answer and give the awards later if there is no better solution. – ricky Oct 17 '16 at 13:35
  • It's not "found on the internet", it's found on SO here: http://stackoverflow.com/questions/5028598/hosting-external-app-in-wpf-window – Simon Mourier Oct 24 '16 at 06:25
  • @SimonMourier well I actually didn't know it was there. I found it on the Internet (possibly that answer) a year ago and I just copied the code I had on a project. Sorry – Luca Angioloni Oct 25 '16 at 06:26
  • @SimonMourier to be fair, I believe this answer is somewhat different from the SO post you mentioned. Because I have tried that solution, but failed to make it work with QT app. – ricky Oct 31 '16 at 02:03
  • @LucaAngioloni have you got the bounty? I've been lost access to SO last fortnight because the GFW... – ricky Oct 31 '16 at 02:10
  • @LucaAngioloni I'm so sorry. It's my bad. It has been subtracted from my score... – ricky Oct 31 '16 at 02:27
  • @ricky - never said it was the exact same answer, but one should cite his/her source, and for example, explain why it does not work, explain what was changed to make it work, etc. – Simon Mourier Oct 31 '16 at 06:50
0

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). Download the project. Find "notepad.exe" in the project and replace it with the file name of your QT application. 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.

What it looks like: enter image description here

ctc chen
  • 1,336
  • 1
  • 7
  • 7
  • I tried this before ask this question. And it seems not work with a QT app – ricky Oct 31 '16 at 02:02
  • in this file: TestWpfAppControl\MainWindow.xaml.cs, register the environment variables required by your QT. For my case, I put all my QT executables under the folder of my WPF exe. And I modify the MainWindow.xaml.cs file as:appControl.ExeName = "myQT.exe"; Environment.SetEnvironmentVariable("PATH", "bin"); – ctc chen Oct 31 '16 at 08:32