C# 如何在wpf中处理窗口

C# 如何在wpf中处理窗口,c#,wpf,C#,Wpf,我想在我的窗口中打开一个应用程序。我的代码在windows窗体中运行良好,但在wpf中不起作用。下面是代码和说明 private Process pDocked; private IntPtr hWndOriginalParent; private IntPtr hWndDocked; [DllImport("user32.dll")] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNe

我想在我的窗口中打开一个应用程序。我的代码在windows窗体中运行良好,但在wpf中不起作用。下面是代码和说明

 private Process pDocked;
    private IntPtr hWndOriginalParent;
    private IntPtr hWndDocked;

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

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);




    private void panel1_Resize(object sender, EventArgs e)
    {
        //Change the docked windows size to match its parent's size. 
        MoveWindow(hWndDocked, 0, 0, panel1.Width, panel1.Height, true);
    }


    private void dockIt(string utility)
    {
        if (hWndDocked != IntPtr.Zero) //don't do anything if there's already a window docked.
            return;
        //hWndParent = IntPtr.Zero;

        pDocked = Process.Start(utility);
        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, panel1.Handle);

        //Wire up the event to keep the window sized to match the control
        panel1.SizeChanged += new EventHandler(panel1_Resize);
        //Perform an initial call to set the size.
        panel1_Resize(new Object(), new EventArgs());
    }
按一下按钮,我正在传递dockit中的记事本,但它不工作,
如何使其在wpf、任何面板(dockpanel、stackpanel)中工作,但它们不支持调整窗口大小

您可以像这样在wpf窗口中托管记事本:将WindowsFormsHost添加到窗口,将System.Windows.Forms.panel设置为子级,并使用面板的句柄

public partial class MainWindow : Window
{
    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    private Process pDocked;
    private IntPtr hWndOriginalParent;
    private IntPtr hWndDocked;
    public System.Windows.Forms.Panel pannel;

    public MainWindow()
    {
        InitializeComponent();
        pannel = new System.Windows.Forms.Panel();
        host.Child = pannel;
        dockIt("notepad.exe");
    }

    private void dockIt(string utility)
    {
        if (hWndDocked != IntPtr.Zero) //don't do anything if there's already a window docked.
            return;

        pDocked = Process.Start(utility);
        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, pannel.Handle);

        //Wire up the event to keep the window sized to match the control
        SizeChanged += window_SizeChanged;
        //Perform an initial call to set the size.
        AlignToPannel();
    }

    private void AlignToPannel()
    {
        MoveWindow(hWndDocked, 0, 0, pannel.Width, pannel.Height, true);
    }

    void window_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        AlignToPannel();
    }
}
窗口xaml:

<TabControl>
    <TabItem Header="Notepad">
        <WindowsFormsHost x:Name="host" />
    </TabItem>
</TabControl>

但是,一旦您将WindowsFormsHost放在一个选项卡中,就会出现大量WinForms/WPF集成问题。
这是我在本文中找到的关于这个主题的最佳知识来源。祝你好运。

使用windows窗体集成后,它会打开实用程序,但不可见,我想在选项卡项中打开它。你能扩展一下吗?解释您的意图、预期发生的情况以及实际观察到的情况(在win forms和wpf中)。System.Windows.forms.Integration.WindowsFormsHost host=new System.Windows.forms.Integration.WindowsFormsHost();dockIt(“记事本”);host.Child=panel1;this.grid1.Children.Add(host);好的,这是我用来在wpf Grid中打开它的代码段。这工作正常,但已经解释过了,可以使用tab控件还是可以将其作为tabitem打开