【问题标题】:how to take handle of a window in wpf如何在wpf中处理窗口
【发布时间】:2017-05-05 16:18:37
【问题描述】:

我想在我的窗口中打开一个应用程序。我的代码在 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());
    }

然后在按钮上单击我将记事本传递到 dokit 但它不起作用, 我如何使它在 wpf、任何面板(dockpanel、stackpanel)中工作,但它们不支持调整窗口大小

【问题讨论】:

  • 使用 windows 窗体集成后,它会打开实用程序,但不可见
  • 我想在一个tabitem中打开它
  • 你能扩展一下吗?解释你的意图、你期望发生的事情以及你实际观察到的事情(无论是获胜形式还是 wpf)。
  • System.Windows.Forms.Integration.WindowsFormsHost 主机 = 新 System.Windows.Forms.Integration.WindowsFormsHost(); dockIt("记事本");主机.孩子=面板1; this.grid1.Children.Add(host);
  • 好的,这是我用来在 wpf 网格中打开它的代码段

标签: c# wpf


【解决方案1】:

您可以像这样在 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 集成问题。 我在这篇文章"Mitigating Airspace Issues In WPF Applications" 中找到了关于这个主题的最佳知识来源。祝你好运。

【讨论】:

  • 这工作正常,但已经解释了是否可以进行选项卡控制,或者您可以将其作为 tabitem 打开
猜你喜欢
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
相关资源
最近更新 更多