Hosting windows forms in WPF grid

656 Views Asked by At

There are three different windows form applications projects. I decided to merge them in a wpf form application. Because i think that i am able to show forms in a grid like in wpf XAML part forms will shown like below.

    <Grid  x:Name="winform"  Grid.Column="1">
    </Grid>

In order to host a windows form application in wpf WindowsFormsHost can be used but in that case i am going to use a form not a windows form control.

In order to do that first i declare three host for each windows form projects and projects form as a global variables.

      System.Windows.Forms.Integration.WindowsFormsHost host;
      System.Windows.Forms.Integration.WindowsFormsHost host1;
      System.Windows.Forms.Integration.WindowsFormsHost host2;

      Project1.Form            form;
      Project2.Form            form1;
      Project3.Form            form2;

In wpf mainwindow loaded event i instantieded the global variables.

    private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
    {
        host = new System.Windows.Forms.Integration.WindowsFormsHost();
        host.Name = "Form";

        host1 = new System.Windows.Forms.Integration.WindowsFormsHost();
        host1.Name = "Form1";

        host2 = new frm.Integration.WindowsFormsHost();
        host2.Name = "Form2";

        form = new Project1.Form();
        form1 = new Project2.Form();
        form2 = new Project3.Form();
     }

The application logic is simple there are three buttons which related each of the three projects. Each button bind a function like below. In the function below , since each form has different width and heigth i equalize form size with wpf mainwindows size.

Then add host as a child in the grid. If grid has no child add the clicked child if grid has child remove the child then add clicked child(form)

    public void ShowProject2Form()
    {
        form1.TopLevel = false;
        form1.ControlBox = false;

        winform.Width = form1.Width + 100;
        winform.Height = form1.Height + 100;

        Application.Current.MainWindow.Height = winform.Height;
        Application.Current.MainWindow.Width = winform.Width + 100;

        form1.ControlBox = false;
        host1.Width = form1.Width + 100;
        host1.Height = form1.Height;


        if (winform.Children.Count == 0)
        {
            host1.Child = form1;
            winform.Children.Add(host1);                                
            winform.UpdateLayout();
        }
        else
        {
            if ((winform.Children[0] as WindowsFormsHost).Name == "Form2")
            {
                winform.Children.Remove(host2);
            }
            else if ((winform.Children[0] as WindowsFormsHost).Name == "Form")
            {
                winform.Children.Remove(host);
            }
            else
                winform.Children.Remove(host1);

            winform.UpdateLayout();
        }
    }

But the problem is when adding host to grid. After executing the program there immediately appears a blank shadow form though when i declare the form application execution as a class library not windows application. In addition to that in task manager they counts as a process. What could be reason of this?

enter image description here

0

There are 0 best solutions below