別の非 WPF アプリケーション内でホストされているように見せたい WPF アプリケーションがあります。実際には、この非 WPF アプリは Internet Explorer 内の ActiveX ですが、問題を説明するために単純な Windows フォーム アプリを使用します。
私は Windows API 関数 SetParent を使用します。これには既に数十のスレッドがあります。ただし、正確な問題について書かれたものは見つかりません。WPF アプリの右側と下部にある小さな領域が、非 WPF アプリのウィンドウ内に描画されません。
単独で実行される 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 の使用が必ずしも推奨される方法ではないことは承知していますが、この方法でそれを行う方法を知りたいし、見つける必要があるので、教えてください :)