【问题标题】:WindowsFormsHost in WPF Not Displaying Hosted ApplicationWPF 中的 WindowsFormsHost 不显示托管应用程序
【发布时间】:2021-02-09 16:20:29
【问题描述】:

在我的 WPF 中,我有一个包含 WindowsFormsHost 的网格,它将承载一个 .exe。 我的 .xaml 代码是:

<Window x:Class="PracticeWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PracticeWPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="800" Width="1300">
    <Grid x:Name="grid">

        <Button x:Name="BtnRun" Content="Run" 
                HorizontalAlignment="Left" Margin="10,10,0,0"
                VerticalAlignment="Top" Click="BtnRun_Click"/>

        <WindowsFormsHost x:Name="formshost" Margin="0,39,0,0"
            Height="720" Width="1280"/>

    </Grid>
</Window>

还有我的cs:

namespace PracticeWPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll")]
        public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        private void BtnRun_Click(object sender, EventArgs e)
        {
            Process proc = Process.Start("notepad.exe"); 
            proc.WaitForInputIdle();

            while (proc.MainWindowHandle == IntPtr.Zero)
            {
                Thread.Sleep(100);
                proc.Refresh();
            }
            SetParent(proc.MainWindowHandle, formshost.Handle);
        }    
    }
}

当我使用 WinForm 时一切正常,但当我在 WPF 中使用 WindowsFormsHosting 时,.exe 不可见。 任何帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: c# wpf exe windowsformshost


    【解决方案1】:

    在这个链接Docking Window inside another Window 我想你的答案。我把它和你的代码匹配了

    xaml

    <Window x:Class="PracticeWPF.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
            xmlns:local="clr-namespace:PracticeWPF"
            mc:Ignorable="d"
            Title="MainWindow" Height="800" Width="1300">
        <Grid x:Name="grid">
    
            <Button x:Name="BtnRun" Content="Run" 
                    HorizontalAlignment="Left" Margin="10,10,0,0"
                    VerticalAlignment="Top" Click="BtnRun_Click"/>
    
            <WindowsFormsHost x:Name="formshost" Margin="0,39,0,0"
                Height="720" Width="1280">
                 <wf:Button Text="by button" Visible="False"></wf:Button>
            </WindowsFormsHost>
        </Grid>
    </Window>
    

    和 ma​​inwindow.cs

    namespace PracticeWPF
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void BtnRun_Click(object sender, EventArgs e)
            {
                //Process proc = Process.Start("notepad.exe");
               //proc.WaitForInputIdle();
    
                //while (proc.MainWindowHandle == IntPtr.Zero)
                //{
                //    Thread.Sleep(100);
                //    proc.Refresh();
                //}
                this.Width = formshost.Width;
                this.Height = formshost.Height;
                dockIt();
            }
    
    
            [DllImport("user32.dll")]
            public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    
            private Process pDocked;
            private IntPtr hWndOriginalParent;
            private IntPtr hWndDocked;
    
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    
    
            private void dockIt()
            {
                if (hWndDocked != IntPtr.Zero) //don't do anything if there's already a window docked.
                    return;
                IntPtr hWndParent = IntPtr.Zero;
    
                pDocked = Process.Start(@"notepad");
                while (hWndDocked == IntPtr.Zero)
                {
                    pDocked.WaitForInputIdle(1000); //wait for the window to be ready for input;
                    pDocked.Refresh();              //update process info
                    if (pDocked.HasExited)
                    {
                        return; //abort if the process finished before we got a handle.
                    }
                    hWndDocked = pDocked.MainWindowHandle;  //cache the window handle
                }
                //Windows API call to change the parent of the target window.
                //It returns the hWnd of the window's parent prior to this call.
                hWndOriginalParent = SetParent(hWndDocked, formshost.Handle);
    
                //Wire up the event to keep the window sized to match the control
                formshost.SizeChanged += new SizeChangedEventHandler(Panel1_Resize);
                //Perform an initial call to set the size.
                Panel1_Resize(new Object(), new EventArgs());
            }
    
            private void undockIt()
            {
                //Restores the application to it's original parent.
                SetParent(hWndDocked, hWndOriginalParent);
            }
    
            private void Panel1_Resize(object sender, EventArgs e)
            {
                //Change the docked windows size to match its parent's size. 
                MoveWindow(hWndDocked, 0, 0, (int)formshost.Width, (int)formshost.Height, true);
            }
        }
    }
    

    【讨论】:

    • 谢谢,这就是我想要做的。还要感谢您帮助调整大小和填充窗口部分,这将很难解决。只是好奇,为什么按钮需要在 WindowsFormsHost 中,当我删除按钮时,应用程序变得不可见,但我没有看到与 cs 中按钮的连接。
    • 我一直在寻找相同的点,并在@mm8中找到了解决方案。 hWndOriginalParent = SetParent (hWndDocked, new System.Windows.Interop.WindowInteropHelper (this) .Handle);
    • 这可能就是为什么WindowsFormsHost没有子节点并且当外部应用程序是Places时编译器隐藏它并且不显示它的原因。而这一切都是在加载窗口时完成的。
    • 或者加载时,窗体呈现为零高度,放置外部程序时无法调整大小,创建新样本时,用外部程序渲染窗口。跨度>
    【解决方案2】:

    恐怕WindowsFormsHost 不能用于在另一个应用程序中托管整个 Windows 窗体应用程序。

    它用于在 WPF 视图中承载单独的 Windows 窗体控件。

    如果您想获得 WPF 窗口的句柄,请尝试:

    new System.Windows.Interop.WindowInteropHelper(this).Handle
    

    你想使用这个句柄而不是formshost.Handle。

    【讨论】:

    • 啊,我只是认为 WindowsFormsHost 就像将 Windows 窗体添加到 WPF 中一样。我试过了,它解决了一切。感谢您的帮助
    • 你意识到你接受了另一个答案...?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多