-1

I'm trying to hosting an exe on the WPF program by using WinfowsFormsHost and System.Windows.Forms.Panel.Here is the code:


    public void AddPuttyPage(Process process)
        {
            var picPan = new System.Windows.Forms.Panel() { 
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            var winform = new WindowsFormsHost
            {
                Child = picPan,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                VerticalAlignment = System.Windows.VerticalAlignment.Stretch
            };
            //Tabs are bind to the TabControl on a xaml
            Tabs.Add(new TabItem { Header = "Tab1", Content = winform });
            process.WaitForInputIdle();

            SetParent(process.MainWindowHandle, picPan.Handle);
        }

I want the System.Windows.Forms.Panel fill the container so that the exe application fill the window. I set Dock = System.Windows.Forms.DockStyle.Fill,But it doesn't work,the Panel still has default size 100*200.

So what should I do to let the Panel fill it's container?(Panel's container is WindowsFormsHost,and WindowsFormsHost is the content of the TabItem, so I want the panel fill the tabPage in fact)

pete the pagan-gerbil
  • 3,136
  • 2
  • 28
  • 49
indexalice
  • 137
  • 1
  • 9
  • What is the size of `winform` control? In other words: is it wpf layout problem or winforms docking problem? – Sinatr Nov 20 '20 at 08:22
  • The size of the `WindowsFormsHost` is 0*0. I think it's a wpf layout problem.If I can get the height and width of the tabControl,I can define the WindowsFormsHost and Panel with those. I try to get height and width of the tabControl by dateBind `` but they are 0. – indexalice Nov 20 '20 at 08:42
  • 1
    Height and Width are the sizes you want to set initially. You have to use ActualHeight and ActualWidth when you want to manipulate other controls Height and Width. – Nawed Nabi Zada Nov 20 '20 at 09:17
  • But how to use ActualHeight and ActualWidth? I don't know how to bind them to the ViewModel – indexalice Nov 20 '20 at 09:38
  • [Hosting Win32 Content in WPF](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/hosting-win32-content-in-wpf) -- [Walkthrough: Host a Win32 Control in WPF](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-hosting-a-win32-control-in-wpf) -- [Layout Considerations for the WindowsFormsHost Element](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/layout-considerations-for-the-windowsformshost-element) – Jimi Nov 20 '20 at 13:13

1 Answers1

0

Finally,I did it by get the ActualHeight and ActualWidth of the tabControl.ActualHeight and ActualWidth can be bind to viewModel by this solution :the first answer and then just init the Panel like this:

           var picPan = new System.Windows.Forms.Panel() { 
                Height = AcutalHeight,
                Width = AcutalWidth,
                Dock = System.Windows.Forms.DockStyle.Fill
            };

finally use ShowWindow(handle, SW_MAXIMIZE)let the window fill the panel.

indexalice
  • 137
  • 1
  • 9