未在 wpf 的托管进程中调用 WndProc
WndProc not called in hosted process in wpf
按照以下说明进行操作:
How to run an application inside wpf application?
以及 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>
首先,我将通过单击 "Get Apps" 按钮而不是单击 "Start" 来获取应用程序
结果将如下所示:
但只有 一个 托管应用程序接受用户输入。 (在这个例子中,它是第一个应用程序,用红色圈起来的可以有用户输入)
另外 2 个不接受任何用户输入。单击其他 2 个应用程序时不会触发任何内容。
我知道我没有处理过很多情况。但这并没有影响我目前遇到的问题,我想。这是我写的一个简单的应用程序(不是真正的应用程序),我希望有人能够重现我遇到的相同错误。
我做错了什么吗?或者我错过了什么?
任何建议将不胜感激。提前致谢!
我找到了解决这个问题的方法,
即使用WindowsFormsHost
来托管控制台应用程序
example。
我创建了一个 System.Windows.Forms.Panel
并将其设置为 WindowsFormsHost
的 Child
并再次将其添加到 Wrap Panel
.[=20= 的子项中]
不知何故,所有托管应用程序都能够接收用户输入。
至于为什么,使用HwndHost
和Border
来托管控制台应用程序,不接收用户输入,我仍然不知道为什么。
但我认为是因为:
- 我将托管应用程序的 window 样式设置为
Child
,这使其无法接收用户输入。 (我知道我在哪里看过,但忘了在哪里)
WndProc
仅接收表单消息,它无权访问 to/catch 进入控制台的消息。
这是我能想到的导致托管应用程序无法接收用户输入的 2 个原因。
如果我错了请纠正我。
此致,
凯
按照以下说明进行操作:
How to run an application inside wpf application?
以及 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>
首先,我将通过单击 "Get Apps" 按钮而不是单击 "Start" 来获取应用程序 结果将如下所示:
但只有 一个 托管应用程序接受用户输入。 (在这个例子中,它是第一个应用程序,用红色圈起来的可以有用户输入) 另外 2 个不接受任何用户输入。单击其他 2 个应用程序时不会触发任何内容。
我知道我没有处理过很多情况。但这并没有影响我目前遇到的问题,我想。这是我写的一个简单的应用程序(不是真正的应用程序),我希望有人能够重现我遇到的相同错误。
我做错了什么吗?或者我错过了什么? 任何建议将不胜感激。提前致谢!
我找到了解决这个问题的方法,
即使用WindowsFormsHost
来托管控制台应用程序
example。
我创建了一个 System.Windows.Forms.Panel
并将其设置为 WindowsFormsHost
的 Child
并再次将其添加到 Wrap Panel
.[=20= 的子项中]
不知何故,所有托管应用程序都能够接收用户输入。
至于为什么,使用HwndHost
和Border
来托管控制台应用程序,不接收用户输入,我仍然不知道为什么。
但我认为是因为:
- 我将托管应用程序的 window 样式设置为
Child
,这使其无法接收用户输入。 (我知道我在哪里看过,但忘了在哪里) WndProc
仅接收表单消息,它无权访问 to/catch 进入控制台的消息。
这是我能想到的导致托管应用程序无法接收用户输入的 2 个原因。
如果我错了请纠正我。
此致,
凯