2016-09-28 124 views

回答

0

我不知道這是否是做了正確的事情,但是這就是我用了一些時間來使嵌入其他應用程序(在互聯網上找到):

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; 
} 

只需修改此骨架到你的必需品。 讓我知道它是否有效。

+0

由於盧卡,它爲我工作,但仍存在一些小問題。如果沒有更好的解決方案,我會接受它作爲答案並在稍後頒發獎項。 – ricky

+0

@ricky好的,謝謝你 –

+0

它不是「在互聯網上找到」,它在這裏被發現:http://stackoverflow.com/questions/5028598/hosting-external-app-in-wpf-window –

0

是的。有可能的。一個非常簡單的方法,可以讓您在沒有編碼的情況下快速入門。

檢查此鏈接:在WPF窗口應用程序中託管EXE應用程序(http://www.codeproject.com/Tips/673701/Hosting-EXE-Applications-in-a-WPF-Window-Applicati)。下載該項目。在項目中找到「notepad.exe」,並將其替換爲QT應用程序的文件名。只是提醒一下:對於WPF exe啓動QT應用程序,您可能需要照顧QT所需的環境變量。

是什麼樣子: enter image description here

+0

我在嘗試此問題之前嘗試了此操作。而且這個文件似乎不適用於QT應用程序 – ricky

+0

:TestWpfAppControl \ MainWindow.xaml.cs,註冊您的QT所需的環境變量。對於我的情況,我把我所有的QT可執行文件放在我的WPF exe文件夾下。我修改MainWindow.xaml.cs文件爲:appControl.ExeName =「myQT.exe」; Environment.SetEnvironmentVariable(「PATH」,「bin」); –

相關問題