user2686455
user2686455

Reputation: 107

Embed Unity3D app inside WPF *without* having it take up the whole window

I am trying to adapt the code posted in this question: https://stackoverflow.com/a/44059700

to allow me to embed a Unity3D app inside a WPF app.

This is my slightly edited version:

namespace WPFWithUnity
{
public partial class Page1 : Page
{
    [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);

    Frame p = MainWindow.Instance.floatingFrame;

    bool initialized = false;

    public Page1()
    {
        InitializeComponent();

        MainWindow.Instance.MainWindowClosing += Application_Exit;

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

    }

    void attemptInit(object sender, EventArgs e) {

        if (initialized)
            return;

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

        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 = "Child.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(hWnd, 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)p.Width, (int)p.Height, true);
        Console.WriteLine("RESIZED UNITY WINDOW TO: " + (int)p.Width + "x" + (int)p.Height);
        ActivateUnityWindow();
    }

    // Close Unity application
    private void Application_Exit(object sender, EventArgs e)
    {
        try
        {
            process.CloseMainWindow();

            Thread.Sleep(1000);
            while (!process.HasExited)
                process.Kill();
        }
        catch (Exception)
        {

        }
    }

    private void Form1_Activated(object sender, EventArgs e)
    {
        ActivateUnityWindow();
    }

    private void Form1_Deactivate(object sender, EventArgs e)
    {
        DeactivateUnityWindow();
    }
}
}

And here is the relevant part of the XAML:

<Frame Name="floatingFrame" Grid.Row="15" Grid.RowSpan="5" Grid.Column="0" Grid.ColumnSpan="2" Width="640" Height="480" Margin="100,0,0,0" Panel.ZIndex="100" Source="Page1.xaml"/>

Really, the only difference is that I'm trying to use a WPF Page inside a Frame instead of a WinForms panel (trying to avoid WinForms). The embedded Unity app starts up fine...except that it takes up the whole window (i.e. you can't see any of the WPF controls anymore).

So, the question: How do I get the Unity app to only stay inside the WPF page (which is inside a Frame)?

enter image description here

(The Y of this XY problem would be that I'm just trying to create a 3D graphics display of something inside a WPF app.)

Thanks in advance for any help.

Upvotes: 3

Views: 4888

Answers (2)

JoachimAlly
JoachimAlly

Reputation: 112

The problem with the above solution is that getting focus on the Unity-exe seems to be not possible. So yes, I was able to load the exe on a certain cell of a certain user control of a certain WPF application, but could not click on anything in the Unity-frame.

I spent the whole day searching for a solution and can now come with a solution, that is in my opinion cleaner and also solves the problem with the focus.

I describe what you can do in steps:

  1. Documentation of Unity : In here, it is explained how you can embed a Unity-exe in a Winforms control. There is even a .zip-file "EmbeddedWindow.zip" where you can download example code. Copy the essential files out of the Container enter image description here .

Form1.cs contains exactly the code as in this question.

public partial class Form1: Form
    {
        [DllImport("User32.dll")]
        private 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")]
        private 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);

        public Form1()
        {
            InitializeComponent();

            TopLevel = false;

            try
            {
                process = new Process();
                process.StartInfo.FileName = "[INSERT_FILE_NAME_OF_YOUR_EXE].exe";
                process.StartInfo.Arguments = "-parentHWND " + panel1.Handle.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(panel1.Handle, WindowEnum, IntPtr.Zero);

                unityHWNDLabel.Text = "Unity HWND: 0x" + unityHWND.ToString("X8");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ".\nCheck if Container.exe is placed next to Child.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, panel1.Width, panel1.Height, true);
            ActivateUnityWindow();
        }

        // Close Unity application
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                process.CloseMainWindow();

                Thread.Sleep(1000);
                while (process.HasExited == false)
                    process.Kill();
            }
            catch (Exception)
            {
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            ActivateUnityWindow();
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            DeactivateUnityWindow();
        }
    }
  1. Do not forget to have a look in "Form1.cs", it is possible that the exe you want to update is not "Child.exe", so if it is another one, just edit the string in the code. process.StartInfo.FileName = "[INSERT_FILE_NAME_OF_YOUR_EXE].exe"; Also check "SelectablePanel.cs", where the setting of Selectable to true is essential. This SelectablePanel is used in the Form1.Designer.cs .

The left panel1 of the SplitContainer is a SelectablePanel instead of a Panel !

    class SelectablePanel : Panel
    {
        public SelectablePanel()
        {
            this.SetStyle(ControlStyles.Selectable, true);
            this.TabStop = true;
        }
    }

Please also note that in the constructor of the Winforms-control, I needed to set Toplevel = false; . This was not mentioned in the example of Unity but is needed to avoid an exception when you embed it in your WPF application.

  1. Go to your WPF Application and make a User Control that will contain your Winforms-Control. Make a control similar to what is done in this link . In this example, you have a Grid named Grid_To_Embed_Winforms_Control_In and a small piece of code-behind, like the underlying code.
public partial class WPF_User_Control: UserControl 

{ 
    public bool Already_Loaded = false;

    public WPF_User_Control()
    {
        InitializeComponent();
    }

    private void On_Load(object sender, RoutedEventArgs e)
    {
        if (!Already_Loaded)
        {
            // Create the interop host control.
            var host =
                new WindowsFormsHost();

            // Embed the Winforms Control
            host.Child = new Embed_Unity_Exe_Winforms_Control();

            // Add the interop host control to the Grid
            // control's collection of child controls.
            Grid_To_Embed_Winforms_Control_In.Children.Add(host);
            Already_Loaded = true;
        }
    }
}  

  1. Do not forget to add using System.Windows.Forms.Integration; on top. Please note that I added the boolean Already Loaded to make sure that when the viewport changes (you want to view another page of your WPF application) the process is not started again. In my complete solution, I make use of Microsoft Dependency Injection and this control is in a viewmodel which is added as a singleton. In this way, I only start the process once.

So this is it, this worked for me.

What is better than other solutions I saw while googling:

  • The resizing is working better and done "automatically", I did not need to invoke a resize-method myself (except for the one in Form1.cs) .
  • I can also have control and focus over the Unity.

PS: If you want to make it look nicer and more "embedded", you can do the following things with the Winform-control:

  • Select splitcontainer1 and go to Properties
  • Set BorderStyle to 'None'
  • Set Panel2Collapsed to 'True'

Upvotes: 3

Leo Bartkus
Leo Bartkus

Reputation: 1946

Use a WindowsFormsHost or HwndHost control in your WPF. The hwnd is in the host control's Handle property. So you can change this line to put Unity in just the host control.

process.StartInfo.Arguments = "-parentHWND " + hwndHost.Handle.ToInt32() + " " + Environment.CommandLine;

And remove the code that gets the hwnd for the floating frame

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

Upvotes: 3

Related Questions