Detect new app and run it in panel


Detect new app and run it in panel



I am creating application that runs another app inside panel.


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

public Form3() {
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e) {
Process p = Process.Start(@"pathapp.exe");
Thread.Sleep(200); // Allow the process to open it's window
SetParent(p.MainWindowHandle, panel1.Handle);
}



But the problem is, that app.exe sometimes (I know when) creates new window as a new app. I want to add this new window into new panel.


private Process GetProcess() {
//do some magic stuff and find actually running app
return NewAppProcess;
}

private void button2_Click(object sender, EventArgs e) {
Process p = GetProcess();
SetParent(p.MainWindowHandle, panel2.Handle);
}



Thanks for everything that can push me to right way





Watch Win32_ProcessStartTrace.
– Reza Aghaei
Jun 30 at 7:06


Win32_ProcessStartTrace





Making the UI of another process to be a child of your GUI is likely to freeze one or both apps.
– MickyD
Jun 30 at 7:19





Yeah i agree with @MickyD this is asking for trouble
– TheGeneral
Jun 30 at 7:50





@TheGeneral I'm not saying using SetParent is a good idea. But saying it makes the app freeze, is not really true. Take a look at the example which I shared.
– Reza Aghaei
Jun 30 at 8:03



SetParent




1 Answer
1



Using ManagementEventWatcher, you can watch Win32_ProcessStartTrace to receive an event when a new process starts.


ManagementEventWatcher


Win32_ProcessStartTrace



Example



In this example, I shows how you can watch starting of mspaint.exe and adding it as child of a Panel in your form. To so add a reference to System.Management dll to your project and then use the following code.


mspaint.exe


Panel


System.Management



Note 1: The watcher is not super fast and you probably see the the window opens in desktop and then sits in the panel.



Note 2: It's an example and showing hot to do it with mspaint.exe. If you have any problem applying the solution on your real app.exe, you need to specifically ask about the solution for your app.exe.


mspaint.exe


app.exe


app.exe



Note 3: Make sure you run your as administrator.


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

ManagementEventWatcher watcher;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
watcher = new ManagementEventWatcher(
"Select * From Win32_ProcessStartTrace Where ProcessName = 'mspaint.exe'");
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Start();
}
void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
var id = (UInt32)e.NewEvent["ProcessID"];
var process = System.Diagnostics.Process.GetProcessById((int)id);
this.Invoke(new MethodInvoker(() => {
SetParent(process.MainWindowHandle, panel1.Handle);
}));
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
watcher.Stop();
watcher.Dispose();
base.OnFormClosed(e);
}
}



enter image description here





@MickyD It's rendered, you may need to move it to a location that you see it.
– Reza Aghaei
Jun 30 at 7:54





@MickyD See the screenshot.
– Reza Aghaei
Jun 30 at 8:00





Well blow me down. OK I found that I first had to setup Pain to start at the upper-left of my screen (near 0,0) so that when it is "embedded" it doesn't shoot of the right hand side of the WinForms app. Surprisingly I was able to interact with Paint whilst embedded, much like you can with Ole. Still....I think thar be dragons and it remains to be seen whether other apps play nice. ;)
– MickyD
Jun 30 at 8:15






Great soltuion. Works like a charm. Only problem is, that app was in full screen. Now is still in full screen but panel1 is little smaller, so i do not see everyting. But, I think, I can solve it. Many thanks :)
– 110mat110
Jun 30 at 9:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

.

How to generate build files in next js

Delphi Android file open failure with API 26