-1

You can open Notepad in WinForm with this code:

public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        public Form1()
        {
            InitializeComponent();

            Process p = Process.Start("notepad.exe");
            p.WaitForInputIdle(); // Allow the process to open it's window
            SetParent(p.MainWindowHandle, this.Handle);
        }
    }

In WPF this.handle is not recognized. What is the WPF version of this.Handle?

And how can you open Notepad in full screen without the close button's in a WPF screen?

user7849697
  • 503
  • 1
  • 8
  • 19
  • WPF version is `new System.Windows.Interop.WindowInteropHelper(this).Handle` where `this` is instance of WPF `Window`. – Evk Oct 14 '17 at 14:10
  • @Evk indeed thats it, but hmm guess it wont work in WPF.. notepad opens out of the wpf windows. – user7849697 Oct 14 '17 at 14:12
  • For the appended question, you can refer to [Removing the Title bar of external application using c#](https://stackoverflow.com/questions/2825528/removing-the-title-bar-of-external-application-using-c-sharp). – Iron Oct 14 '17 at 17:45
  • Have you achieve what you want? @user7849697 – Iron Oct 16 '17 at 15:40

1 Answers1

5

You can get the handle of a WPF window using WindowInteropHelper while the widow has Loaded or SourceInitialized. In the constructor method, the handle has not yet been created, it returns a handle of zero. Just have a try:

public MainWindow()
{
    InitializeComponent();

    Loaded += (s, e) =>
    {
        Process p = Process.Start("notepad.exe");
        p.WaitForInputIdle(); // Allow the process to open it's window
        SetParent(p.MainWindowHandle, new System.Windows.Interop.WindowInteropHelper(this).Handle);
    };
}

Alternatively,

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);

    Process p = Process.Start("notepad.exe");
    p.WaitForInputIdle(); // Allow the process to open it's window
    SetParent(p.MainWindowHandle, new System.Windows.Interop.WindowInteropHelper(this).Handle);
}
Iron
  • 926
  • 1
  • 5
  • 16