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"); } } } 
2
  • See this.. stackoverflow.com/questions/2610924/… Commented Jul 14, 2015 at 7: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? Commented Jul 14, 2015 at 8:05

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

is it legal to have a cross-process parent/child or owner/owned relationship: "... 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"
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 what?
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, this was the only solution ive seen so far :/ got any ideas?
|
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.