我有一个WPF应用程序,我想看起来像是在另一个非WPF-应用程序中托管的。在现实生活中,这个非WPF应用程序是Internet Explorer内部的ActiveX,但是为了说明问题,我使用了一个简单的Windows forms应用程序。

我使用Windows API函数SetParent,已经有数十个线程。但是,我找不到关于我的确切问题写的任何内容: WPF应用程序右侧和底部的一个小区域没有在非WPF应用程序的窗口内绘制.

WPF窗口自行运行:
alt text

WPF窗口带有Winform App的窗口作为父:
alt text

如果将WPF应用程序与Winforms应用程序或普通Win32应用程序(如记事本)交换,我不会遇到问题。

Winform代码看起来像这样:

private void Form1_Load(object sender, EventArgs e)
    {
        // Start process
        var psi = new ProcessStartInfo("c:\\wpfapp\\wpfapp\\bin\\Debug\\wpfapp.exe");
        psi.WindowStyle = ProcessWindowStyle.Normal;
        _process = Process.Start(psi);

        // Sleep until new process is ready
        Thread.Sleep(1000);

        // Set new process's parent to this window
        SetParent(_process.MainWindowHandle, this.Handle);

        // Remove WS_POPUP and add WS_CHILD window style to child window
        const int GWL_STYLE = -16;
        const long WS_POPUP = 0x80000000;
        const long WS_CHILD = 0x40000000;
        long style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
        style = (style & ~(WS_POPUP)) | WS_CHILD;
        SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);

        // Move and resize child window to fit into parent's
        MoveWindow(_process.MainWindowHandle, 0, 0, this.Width, this.Height, true);
    }

注意:我知道这种使用setparent不一定是推荐的练习,但是我想并且需要找出这种方式,所以请让我:)

有帮助吗?

解决方案

我找到了一个相当不错的解决方法:在SetParent之前致电MoveWindow:

private void Form1_Load(object sender, EventArgs e)
{
     // Start process            
     var psi = new ProcessStartInfo("C:\\WpfApp\\WpfApp.exe");
     psi.WindowStyle = ProcessWindowStyle.Normal;
     _process = Process.Start(psi);

     // Sleep until new process is ready
     Thread.Sleep(3000);

     // Move and resize child window to fit into parent's
     Rectangle rect;
     GetClientRect(this.Handle, out rect);
     MoveWindow(_process.MainWindowHandle, 0, 0, rect.Right - rect.Left, rect.Bottom - rect.Top, true);

     // Set new process's parent to this window
     SetParent(_process.MainWindowHandle, this.Handle);

     // Remove WS_POPUP and add WS_CHILD window style to child window
     long style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
     style = (style & ~(WS_POPUP) & ~(WS_CAPTION)) & ~(WS_THICKFRAME) | WS_CHILD;
     SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);
}           

在Sizechanged活动处理程序中,我将孩子带出父母,致电MoveWindow并将其移回父母。为了减少闪烁,我在执行这些操作时隐藏了窗口:

private void Form1_SizeChanged(object sender, EventArgs e)
{
     ShowWindow(_process.MainWindowHandle, SW_HIDE);

     var ptr = new IntPtr();
     SetParent(_process.MainWindowHandle, ptr);

     Rectangle rect;
     GetClientRect(this.Handle, out rect);

     MoveWindow(_process.MainWindowHandle, 0, 0, rect.Right - rect.Left, rect.Bottom - rect.Top, true);            
     SetParent(_process.MainWindowHandle, this.Handle);
     ShowWindow(_process.MainWindowHandle, SW_SHOW);
}

它不能解释为什么MoveWindow在SetParent之后不起作用,但是它可以解决我的问题,因此除非有更好的出现,否则我会将其标记为答案。

其他提示

您可能应该调整WPF窗口的大小,以使其适合 客户区域 它的新父窗口:

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);

// Move and resize child window to fit into parent's client area.
RECT rc = new RECT();
GetClientRect(this.Handle, out rc);
MoveWindow(_process.MainWindowHandle, 0, 0, rc.Right - rc.Left,
    rc.Bottom - rc.Top, true)

我用面板使用了表格 / USERCONTROL。然后,我将面板用作新父母,并将带有setWindowPos的面板的新位置和大小设置为子窗口。

注意:MS建议在使用setWindowlong(或setWindowlongptr)之后使用setWindowpos来激活新样式。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top