blah blah blah is here! blah blah » Close

up0down
link

how can i open an exe file in a C# window?
i see an example in other site which open all open exe files in diffrent tabs.
but i want open my exe file in a panel ... how can i do this?

last answered one year ago

2 answers

up0down
link

These sort of tricks are generally done by calling the API function SetParent.

Here's some sample code, assuming the exe is suitable for embedding:

using System.Diagnostics;
using System.Runtime.InteropServices;

// in your form class

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

[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

private Process proc;

// embed exe on a button click say
private void button1_Click(object sender, EventArgs e)
{
if (proc == null || proc.HasExited)
{
proc = Process.Start("winword.exe"); // or whatever
proc.WaitForInputIdle();
SetParent(proc.MainWindowHandle, panel1.Handle);
MoveWindow(proc.MainWindowHandle, 0, 0, panel1.Width, panel1.Height, false);
}
}

// when form is closing, kill process if it's not been closed already
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!proc.HasExited) proc.Kill();
}


EDIT

It's preferable to use proc.CloseMainWindow() to close a GUI process in an orderly fashion. However, sometimes (as with Word) this doesn't close the whole process (you can check this in Task Manager) so you'll then need to use proc.Kill() instead.

I've also added some code to position the top left hand corner of the exe's window at the top left hand corner of the panel and to make their dimensions the same using another API function, MoveWindow().

up0down
link

thank u my friend
i use this code and it doesnt work properly.
i mean when i normally run it, it open exe file like a seprate process and in its own window.
and when i put trace poit at this line:

proc.WaitForInputIdle();

program stops at this line when i click on continue button ,now exe file move to panel!
why this happen?
how can i open exe file in panel at start.
i mean exe file doesnt run saprately and from the first it open in panel.

vulpes
17279

The problem is that you need the handle of the exe's main window in order to call SetParent(). Now this handle isn't created when the process starts but sometime before the main window is displayed. It therefore recommends in the documentation (see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx) that you should call the WaitForInputIdle() method to be sure that the handle has been created before trying to use it. An alternative to using this method would be to call Thread.Sleep for an interval of one or two seconds. However, this method is a bit 'hit or miss' because different applications take different times to start up and you can't even assume that a given application will always take the same time to start up. Also, even if you could get the handle before the main window is shown, MoveWindow won't work properly until it is. I therefore think that using WaitForInputIdle() is the best approach even if there is a noticeable flash as the application starts and is moved to the panel.

Feedback