7

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?

SwiftLion
  • 163
  • 3
  • 9

3 Answers3

6

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.

Ray Burns
  • 62,163
  • 12
  • 140
  • 141
  • WaitForInputIdle waits for any thread to become idle, not just the GUI thread. – Sheng Jiang 蒋晟 Apr 11 '10 at 15:43
  • @Sheng: Good point. The above works for a simple application like Notepad, but for a more complex application it may be necessary to replace step 4 with code that periodically uses EnumWindows to search for a window with a matching value returned from GetWindowThreadProcessId that is visible and has no parent. In any case it is still possible using XAML/C#. – Ray Burns Apr 12 '10 at 17:28
2

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/

dontbyteme
  • 1,221
  • 1
  • 11
  • 23
SwiftLion
  • 163
  • 3
  • 9
  • Glad this worked for you. It does have two limitations you should be aware of: 1. The title bar, close button, etc, do not display so it doesn't look like a window, and 2. It causes application failures in applications that rely on NC_ events, that paint in the nonclient area, or that scan up their window tree to find the root window. The other technique is less invasive and more universal and also provides the ability to hide the nonclient area if desired. – Ray Burns Apr 12 '10 at 17:35
-1

You can't do that.

The reason you can view a web page is because IE was designed using an architecture that allows it - the rendering engine isn't actually part of the actual IE executable, but a DLL which exposes a component.

Generic EXEs do not work that way, and you cannot embed them inside your app.

Michael Madsen
  • 54,231
  • 8
  • 72
  • 83