0

I am using Visual Studio 2015 RC (WPF application C#) and I want my program to open any of my apps (with the click of a button) into my app window, the code below is unfinished.

The problem happens only when I click the button, notepad opens randomly in it's own window, and not in my app's canvas.

Main.Window.xaml:

<Window x:Class="stackoverflowquestion1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:stackoverflowquestion1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Menu x:Name="menu" HorizontalAlignment="Left" Height="22" Margin="2,10,0,0" VerticalAlignment="Top" Width="497" Grid.Column="1">
        <Button x:Name="button" Click="button_Click" Width="187">Click here to open Notepad Below</Button>
    </Menu>
    <Canvas Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="257" Margin="10,52,0,0" VerticalAlignment="Top" Width="489" Background="Black"/>

</Grid></Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace stackoverflowquestion1
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }


    private void button_Click(object sender, RoutedEventArgs e)
    {
        Process.Start("notepad.exe");
    }
  }
}
SeniorJD
  • 6,946
  • 4
  • 36
  • 53
Adan
  • 453
  • 1
  • 10
  • 27
  • See this.. http://stackoverflow.com/questions/2610924/wpf-opening-up-exe-program-within-wpf-window – Pankaj Jul 14 '15 at 07:47
  • thx, this almost worked, managed to make my button launch a new window, perhaps i can make notepad move to the windows location and resize to the size of the window instead of opening the program in the window? – Adan Jul 14 '15 at 08:05

2 Answers2

0

found the answer, here is the code:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Process p = Process.Start("notepad.exe");
        p.WaitForInputIdle(); // Allow the process to open it's window
        SetParent(p.MainWindowHandle, this.Handle);
    }

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
}
}

final notes, i used a windows forms application c#, NOT windows WPF application, phew im so glad i found the answer haha gl everyone

Adan
  • 453
  • 1
  • 10
  • 27
  • [is it legal to have a cross-process parent/child or owner/owned relationship](http://blogs.msdn.com/b/oldnewthing/archive/2013/04/12/10410454.aspx): "... it is technically legal. It is also technically legal to juggle chainsaws ... they become near-impossible to manage if one or both of the windows involved is unaware that it is participating in a cross-process window tree" – Damien_The_Unbeliever Jul 14 '15 at 09:17
  • Oh, and one further quote: "things will *definitely* stop working if you change that other window from a top-level window to a child window" - which is exactly what you've done here. – Damien_The_Unbeliever Jul 14 '15 at 09:19
  • @Damien_The_Unbeliever what? – Adan Jul 14 '15 at 09:19
  • You're doing something that is *dangerous* and *wrong*. If a program is written expecting that it has a top-level window, it will *not* have been written to expect your program to come along and change its window into a child window. For something as simple as notepad, it may *appear* to work correctly but you shouldn't assume that it *will* work correctly. – Damien_The_Unbeliever Jul 14 '15 at 09:20
  • @Damien_The_Unbeliever, this was the only solution ive seen so far :/ got any ideas? – Adan Jul 16 '15 at 05:01
  • There isn't a "solution" because the problem you're trying to solve isn't one that's envisaged within the Windows system - programs, generally, expect to control their own windows, not to have them embedded within another program's windows. The blog post I linked to explains the various reasons *why* its a bad idea. If you're don't understand *what* that blog post is talking about, that's an indication that you need to read up more, to then understand why what you're trying to do is a bad idea. – Damien_The_Unbeliever Jul 16 '15 at 09:05
  • @Damien_The_Unbeliever, you are right, only notepad works with this, but if you try another program, it breaks, and it's letting me know that it isnt meant to work. – Adan Jul 18 '15 at 18:22
0

If you are really using WPF, You could replace the this.Handle for Process.GetCurrentProcess().MainWindowHandle

CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56