无法在我的WPF项目中“附加”多个应用程序

时间:2015-02-22 10:16:57

标签: c# wpf

我试图在我的C#WPF应用程序中打开写字板和记事本,然后附加'他们到我的应用程序(在WPF TabControl内)。每个应用程序都将存在于不同的TabItem

我遇到了一个问题,我只能在1个程序中实现这个目标。所以,如果我用记事本试试它,它可以按照需要工作。如果我用写字板尝试它,它可以按照需要工作。如果我同时尝试两者,它只会#34;码头"其中一个应用程序进入我的项目 - 另一个应用程序仍然执行(它加载),但没有生活'在我的WPF应用程序中。

我完全迷失了我做错了什么。我可以分享代码的唯一方法是分享所有代码 - 我试图尽可能地减少代码。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Size = System.Windows.Size;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private System.Windows.Forms.Panel _panel;
        private Process _process;
        public MainWindow()
        {
            InitializeComponent();
            _panel = new System.Windows.Forms.Panel();

            var list = new Dictionary<string, string>();//process and params

            list.Add("notepad.exe", null);
            list.Add(@"wordpad.exe", null);

            foreach (var path in list)
            {
                BeginThisThing(path.Key.ToString(), Convert.ToString(path.Value)); //I know the naming is poor, for now I'm only testing!!
                var host1 = new System.Windows.Forms.Integration.WindowsFormsHost();

                host1.Child = this._panel;
                try
                {
                    TabItem ti = new TabItem();
                    this.Tabby.Items.Add(ti);
                    ti.Header = path.Key.ToString();

                    ti.Content = host1;
                }
                catch (Exception ex)
                {
                    string s = ex.ToString();
                }
            }
        }

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

        private void BeginThisThing(string path, string param)
        {
            ProcessStartInfo psi = null;

            psi = (String.IsNullOrEmpty(param)) ? new ProcessStartInfo(path) : new ProcessStartInfo(path, param);

            _process = Process.Start(psi);
            _process.WaitForInputIdle();
            Thread.Sleep(500);
            SetParent(_process.MainWindowHandle, _panel.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();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            if (_process != null)
            {
                _process.Refresh();
                _process.Close();
            }
        }

        private void ResizeEmbeddedApp()
        {
            if (_process == null)
                return;

            SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)_panel.ClientSize.Width, (int)_panel.ClientSize.Height, SWP_NOZORDER | SWP_NOACTIVATE);
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            Size size = base.MeasureOverride(availableSize);
            ResizeEmbeddedApp();
            return size;
        }
    }
}

XAML就是

<Grid>
     <TabControl x:Name="Tabby"></TabControl>        
</Grid>

1 个答案:

答案 0 :(得分:1)

您在类中声明为某个字段的面板必须对每个主机都是唯一的,但在您的代码中,它们是在它们之间共享的,这会导致您看到的行为。