I have been hosting external application ( through a Process) in a wpf window using a
WindowsFormsHost host = new WindowsFormsHost();
.
This is been done successfully with multiple personal apps and system apps (e.g: "write.exe" ). The applications are launched inside the window correctly.
My problem is I have one particular app which has a log file and another coordinates file ( both text files). The second one is used for set the screen x,y locations where this app must be displayed.
My problem is if I try to host this application XXX.exe in a WPF window ,when I start the process the window host a text editor and the real UI of the app its just open being opened somewhere else in the main screen of main pc (random location , not even care about the coordinates of the text field).
I suspect this is because of the text files used by this app XXX.exe
, my wpf window hosts the text file depending by this app ( for that opens a text editor) . Any idea how can I solved this?
What I have tried:
I leave the code of how my wpf window hosts apps:
private Process _aisProcess = null;
private static readonly string APP_NAME = "xxx.exe";
private static string _processPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
public static readonly string appLocation = _processPath + "\\" + APP_NAME;
public MyWindow()
{
InitializeComponent();
_aisProcess = CreateFormsProcess(appLocation);
Window parent = Application.Current.Windows.Cast<Window>().SingleOrDefault(x => x.IsActive);
ConfigureWindowLocation(this, parent , 300,250);
CreateWindowProcess(_aisProcess, AIS_APP_WIDTH ,AIS_APP_HEIGHT);
}
public static void ConfigureWindowLocation(Window window, Window parent, int xcoord = 0, int ycoord = 0)
{
Window parent = Application.Current.Windows.Cast<Window>().SingleOrDefault(x => x.IsActive);
if (parent != null)
{
System.Windows.Forms.Screen screen = System.Windows.Forms.Screen.FromRectangle(
new System.Drawing.Rectangle(
(int)parent.Left, (int)parent.Top,
(int)parent.Width, (int)parent.Height));
window.SizeToContent = SizeToContent.WidthAndHeight;
System.Drawing.Rectangle bounds = System.Drawing.Rectangle.Empty;
bounds = screen.WorkingArea;
window.Left = bounds.X + xcoord;
window.Top = bounds.Y + ycoord;
}
}
public static Process CreateFormsProcess(string path)
{
Process process = Process.Start(path);
process.StartInfo.UseShellExecute = false;
process.WaitForInputIdle();
return process;
}
public void CreateWindowProcess(Process p, int width = 0, int height = 0)
{
System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
WindowsFormsHost host = new WindowsFormsHost();
IntPtr processHandle = p.MainWindowHandle;
SetParent(processHandle, panel.Handle);
SetWindowLong(processHandle, GWL_STYLE, WS_VISIBLE + WS_MAXIMIZE);
panel.Size = new System.Drawing.Size(width, height);
MoveWindow(processHandle, 0, 0, panel.Width, panel.Height, true);
host.Height = panel.Height;
host.Width = panel.Width;
host.Child = panel;
this.Content = host;
}