在WPF页面中嵌入Unity3D应用程序导致导航到新页面后进程名称消失

时间:2019-02-17 06:38:10

标签: c# wpf unity3d process embed

我正在使用此问题中发布的代码进行一些小的更改: Embed Unity3D app inside WPF *without* having it take up the whole window

这种方法效果很好,但是当我查看任务管理器流程时。当我退出运行统一播放器的页面并加载另一个页面时,我的主要WPF可执行文件名称将从进程中删除。该应用程序仍然可以正常运行。任务管理器仅显示一个图标,其旁边没有进程名称。这只是一个问题,因为我还有另一个后台服务监视我的WPF应用程序,并根据其名称远程启动和停止它。有什么建议吗?

下面显示的页面代码。

    public System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
    [DllImport("User32.dll")]
    static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);

    internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
    [DllImport("user32.dll")]
    internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);

    [DllImport("user32.dll")]
    static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    private Process process;
    private IntPtr unityHWND = IntPtr.Zero;

    private const int WM_ACTIVATE = 0x0006;
    private readonly IntPtr WA_ACTIVE = new IntPtr(1);
    private readonly IntPtr WA_INACTIVE = new IntPtr(0);
    System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    bool initialized = false;
    public UnityPlayer()
    {
        InitializeComponent();
        if (this.grid1.Children.Count == 0)
        {
            this.grid1.Children.Add(host);
        }


        dispatcherTimer.Tick += attemptInit;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();

    }

    void attemptInit(object sender, EventArgs e)
    {
        if (process != null) 
        {
            if (process.HasExited)
            {
                Thread.Sleep(1000);
                FSC.GoHome();
            }

        }

        if (initialized)
            return;

        HwndSource source = (HwndSource)HwndSource.FromVisual(host);

        Console.WriteLine("attempting to get handle...");

        if (source == null)
        {
            Console.WriteLine("Failed to get handle source");
            return;
        }

        IntPtr hWnd = source.Handle;

        try
        {
            process = new Process();
            process.StartInfo.FileName = "Block Breaker.exe";
            process.StartInfo.Arguments = "-parentHWND " + hWnd.ToInt32() + " " + Environment.CommandLine;

            process.StartInfo.UseShellExecute = true;
            process.StartInfo.CreateNoWindow = true;

            process.Start();

            process.WaitForInputIdle();
            // Doesn't work for some reason ?!
            //unityHWND = process.MainWindowHandle;
            EnumChildWindows(host.Handle, WindowEnum, IntPtr.Zero);

            //unityHWNDLabel.Text = "Unity HWND: 0x" + unityHWND.ToString("X8");
            Console.WriteLine("Unity HWND: 0x" + unityHWND.ToString("X8"));

            panel1_Resize(this, EventArgs.Empty);

            initialized = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + ".\nCheck if Container.exe is placed next to UnityGame.exe.");
        }
    }

    private void ActivateUnityWindow()
    {
        SendMessage(unityHWND, WM_ACTIVATE, WA_ACTIVE, IntPtr.Zero);
    }

    private void DeactivateUnityWindow()
    {
        SendMessage(unityHWND, WM_ACTIVATE, WA_INACTIVE, IntPtr.Zero);
    }

    private int WindowEnum(IntPtr hwnd, IntPtr lparam)
    {
        unityHWND = hwnd;
        ActivateUnityWindow();
        return 0;
    }

    private void panel1_Resize(object sender, EventArgs e)
    {
        MoveWindow(unityHWND, 0, 0, (int)host.Width, (int)host.Height, true);
        Console.WriteLine("RESIZED UNITY WINDOW TO: " + (int)host.Width + "x" + (int)host.Height);
        ActivateUnityWindow();
    }


    private void Page_Unloaded(object sender, RoutedEventArgs e)
    {
        dispatcherTimer.Stop();
        dispatcherTimer = null;
        host.Dispose();
    }

Image Show Process Before the pages loads

Image show Process While the page is running

Image show Process After the pages exits and loads new page

0 个答案:

没有答案