Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF opening up exe program within WPF window

Tags:

c#

windows

wpf

Not sure if this is possible but is there a way to open up another program like notepad within the container of a WPF window? similiar to that of being able to open a web page using the webbrowser control?

Basically I would like to open notepad or other exe but keep it constrained within the WPF window container using xaml/c# code? not sure if possible?

like image 977
SwiftLion Avatar asked Apr 09 '10 21:04

SwiftLion


People also ask

How do I navigate between Windows in WPF?

NavigationService is for browser navigation within WPF. What you are trying to do is change to a different window TrainingFrm . To go to a different window, you should do this: private void conditioningBtn_Click(object sender, RoutedEventArgs e) { var newForm = new TrainingFrm(); //create your new form.

What is the difference between WPF window and WPF page?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.


2 Answers

Yes it is possible.

All you have to do is:

  1. Create a WindowsFormsHost and add it to a panel in your UI
  2. Start the process (such as Notepad) using Process.Start
  3. Call process.WaitForInputIdle
  4. Use process.MainWindowHandle to get the window handle
  5. Call SetWindowPos to set the process's window to the coordinates and Z Order of the HwndHost window
  6. Hook both the HwndHost and the process.MainWindowHandle to detect size changes and repeat step 5.

It is quite straightforward to do all of this. The effect is the entire Notepad window (or whatever application you started) appears and behaves exactly as if it were part of your WPF application.

like image 114
Ray Burns Avatar answered Sep 24 '22 07:09

Ray Burns


managed to do this using the SetParent method

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hwc, IntPtr hwp);

so by getting the handles for wpf window and then the exe window, I was able to set the wpf window as parent so it gave a simulation of being embedded (also closed the title bar etc)

got help from here: http://channel9.msdn.com/forums/TechOff/250417-Hide-window-caption-in-c/

like image 40
SwiftLion Avatar answered Sep 26 '22 07:09

SwiftLion