次の手順に従います。
MSDN のウォークスルー ( https://msdn.microsoft.com/en-us/library/ms752055.aspx )
コンソール アプリケーションを wpf でホストすることができました。(注: ホストされるアプリケーションは 2 つ以上あります)
ControlHost.cs 内
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Interop;
namespace Try
{
public class ControlHost : HwndHost
{
private static List<Process> _procList = new List<Process>();
IntPtr hwndControl;
int hostHeight, hostWidth;
string filePath;
internal const int
WS_CHILD = 0x40000000,
GWL_STYLE = -16,
WS_CAPTION = 0x00C00000,
WS_THICKFRAME = 0x00040000;
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
[DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)]
internal static extern bool DestroyWindow(IntPtr hwnd);
public ControlHost(double height, double width, string filePathName)
{
hostHeight = (int)height;
hostWidth = (int)width;
filePath = filePathName;
}
protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
Process _process = new Process();
_process.StartInfo.FileName = filePath;
_process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
_process.Start();
_procList.Add(_process);
// The main window handle may be unavailable for a while, just wait for it
while (_process.MainWindowHandle == IntPtr.Zero)
{
Thread.Yield();
}
hwndControl = _process.MainWindowHandle;
int style = GetWindowLong(hwndControl, GWL_STYLE);
style = style & ~((int)WS_CAPTION) & ~((int)WS_THICKFRAME); // Removes Caption bar and the sizing border
style |= ((int)WS_CHILD); // Must be a child window to be hosted
SetWindowLong(hwndControl, GWL_STYLE, style);
SetParent(hwndControl, hwndParent.Handle);
this.InvalidateVisual();
HandleRef hwnd = new HandleRef(this, hwndControl);
return hwnd;
}
protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
handled = false;
return IntPtr.Zero;
}
protected override void DestroyWindowCore(HandleRef hwnd)
{
DestroyWindow(hwnd.Handle);
if (_procList != null)
{
foreach (Process p in _procList)
{
if (p != null)
{
try
{
while (!p.HasExited)
{
p.Refresh();
p.CloseMainWindow();
p.Kill();
Thread.Sleep(10);
}
}
catch
{
}
}
}
}
}
public void Stop(IntPtr Hwnd)
{
HandleRef hwnd = new HandleRef(this, Hwnd);
DestroyWindow(hwnd.Handle);
if (_procList != null)
{
foreach (Process p in _procList)
{
if (p != null)
{
try
{
while (!p.HasExited)
{
p.Refresh();
p.CloseMainWindow();
p.Kill();
Thread.Sleep(10);
}
}
catch
{
}
}
}
}
}
}
}
およびMainWindow.xaml.csで:
using System;
using System.Collections.Generic;
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;
using Microsoft.Win32;
namespace Try
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string[] appsList;
private List<ControlHost> ctrlHostList = new List<ControlHost>();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ControlHost appsControl;
Border appsBorder;
foreach (string app in appsList)
{
appsBorder = new Border();
appsBorder.Height = double.NaN;
appsBorder.Width = double.NaN;
appsBorder.BorderBrush = Brushes.Silver;
appsBorder.BorderThickness = new Thickness(1);
appsControl = new ControlHost(appsBorder.ActualHeight, appsBorder.ActualWidth, app);
ctrlHostList.Add(appsControl);
appsBorder.Child = appsControl;
WP_Apps.Children.Add(appsBorder);
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog _openFileDlg = new OpenFileDialog();
_openFileDlg.Multiselect = true;
if (_openFileDlg.ShowDialog() == true)
{
appsList = _openFileDlg.FileNames;
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
foreach(ControlHost CH in ctrlHostList)
{
CH.Stop(CH.Handle);
}
}
}
}
最後にMainWindow.xamlで:
<Window x:Class="Try.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1502" Width="1500" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" WindowState="Maximized" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="76*"/>
<ColumnDefinition Width="671*"/>
</Grid.ColumnDefinitions>
<Button Content="Start" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Height="20" Click="Button_Click"/>
<Button Content="GetApps" HorizontalAlignment="Left" Height="21" Margin="10,54,0,0" VerticalAlignment="Top" Width="74" Click="Button_Click_1"/>
<ScrollViewer Grid.Column="1">
<WrapPanel Name="WP_Apps" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" />
</ScrollViewer>
<Button Content="Stop" HorizontalAlignment="Left" Margin="10,99,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
</Grid>
</Window>
まず、[開始] をクリックするよりも [アプリを取得] ボタンをクリックしてアプリを取得します。結果は次のようになります。
ただし、ユーザー入力を受け入れるホストされたアプリケーションは1 つだけです。(この例では、ユーザー入力を持つことができる赤丸で囲まれた最初のアプリケーションです) 他の 2 つはユーザー入力を受け入れません。他の 2 つのアプリケーションをクリックしても、何もトリガーされません。
私は多くの状況を扱っていないことを知っています。しかし、それは私が現在抱えている問題には影響しなかったと思います. これは私が書いた単純なアプリケーション (実際のアプリケーションではありません) であり、誰かが私と同じエラーを再現できることを願っています。
私が間違っていることはありますか?それとも私は何かを逃しましたか?どんな提案でも大歓迎です。前もって感謝します!