Embed external application inside WPF Application

A friend of mine wanted to embed a third party application within her WPF application. While at first I was slightly confused, a bit of googling made life easy.

Center idea revolve around launching the third party application using the Process class and use the Win32 API methods to embed it within the client application.

var process = Process.Start("Notepad.exe");
process.WaitForInputIdle();

The above code needs to be executed after the view has been completely loaded. In the above steps the application (for the sake of example, we will use Notepad.exe ) and use the WaitForInputIdle() method to wait untill the associated process is in idle state.

The next step involve changing the parent Window of new Notepad process to the current application’s Window Handle.

var win = view as Window;
var winHandle = new WindowInteropHelper(win).Handle;
SetParent(process.MainWindowHandle, winHandle);
SetWindowLong(process.MainWindowHandle, GWL_STYLE, WS_VISIBLE);
MoveWindow(process.MainWindowHandle, 0, 0, (int)win.ActualWidth, (int)win.ActualHeight, true);

This would be followed by setting the visibility of the Window Style, and then, the most important step – changing the position of the newly invoked process to the desired location in the parent WPF application.

That’s all you would need to embed the application within the WPF application. I have wrote a small application, implementing the idea, which can be accessed from my Github here.

Advertisement

One thought on “Embed external application inside WPF Application

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s