我有一个 WPF 应用程序,我希望它看起来像是托管在另一个 - 非 WPF - 应用程序中。在现实生活中,这个非 WPF 应用程序是 Internet Explorer 中的 ActiveX,但为了说明问题,我使用了一个简单的 Windows 窗体应用程序。
我使用 Windows API 函数 SetParent,它已经有几十个线程。但是,我找不到任何关于我的确切问题的内容:WPF 应用程序右侧和底部的一个小区域未绘制在非 WPF 应用程序的 window 内。
自行运行的 WPF 窗口:
以 WinForm 应用程序的窗口为父窗口的 WPF 窗口:
如果将 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 的这种使用不一定是推荐的做法,但我想并且需要了解如何这样做,所以请让我 :)