如何在Windows应用程序中显示非Windows应用程序

时间:2018-10-26 05:23:48

标签: c# winforms

如何使用c#在Windows窗体中嵌入非本机应用程序(Windows 7中安装了应用程序)。

到目前为止,我已经尝试过:

public partial class Form1 : Form
{
    Process process = new Process();


    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("User32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    public Form1()
    {
        InitializeComponent();
        // panel1.Container.Add(process);
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = false;
         string path = Path.Combine(Path.GetFullPath(@"C:\Program Files (x86)\Plan-G v3.2.0"), "Plan-G3.exe");
         process = Process.Start(path);

         Debug.WriteLine(process.MainWindowHandle);
         while (process.MainWindowHandle == IntPtr.Zero)
        {
             Thread.Sleep(1000); // Don't hog the CPU
            process.Refresh(); // You need this since `MainWindowHandle` is cached


        }

        Form1.SetForegroundWindow(process.MainWindowHandle);
        SetForegroundWindow(process.MainWindowHandle);

        SetParent(process.MainWindowHandle, panel1.Handle);

        MoveWindow(process.MainWindowHandle, 0, 0, panel1.Width - 90, panel1.Height, true);

    }
}

当我从debugging开始时,有时我可以使它正常工作。

但是,当我开始时没有debugging时,应用程序总是在表单外部打开。我不明白为什么会这样。

我还尝试了[ForceForegroundWindow][1]()解决方法。

我要实现的目标是可能的吗?

根据其他论坛的答案,似乎可能不是...

0 个答案:

没有答案