软件已安装好,EXE所在路径为C:\Program Files\Company\Workstation\Software.exe
文中涉及到的具体名字已隐藏
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Width="500" Height="350" Loaded="Window_Loaded">
<Grid>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//启动
var process = new Process();
ProcessStartInfo info = new ProcessStartInfo("C:\\Program Files\\Company\\Workstation\\Software.exe");
info.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(info);
var m_WindowName = "Software_name";
IntPtr HWND = IntPtr.Zero;
int times = 60 * 5;//给5分钟的启动时间
while (times > 0)
{
times--;
HWND = Class1.FindWindow(null, m_WindowName);
if (HWND == IntPtr.Zero) //稍等一会,不然可能找不到
{
Thread.Sleep(1000);
continue;
}
break;
}
//当前窗口句柄
IntPtr parent = new WindowInteropHelper(Application.Current.MainWindow).Handle;
var hand = Class1.FindWindow(null, "MainWindow");
//改变样式(去掉一圈)
Class1.ShowWindow(HWND, Class1.SW_SHOW);
Class1.EnableWindow(HWND, true);
int style = Class1.GetWindowLong(HWND, Class1.GWL_STYLE);
style = style & ~((int)Class1.WS_CAPTION) & ~((int)Class1.WS_THICKFRAME);
style |= ((int)Class1.WS_CHILD);
Class1.SetWindowLong(HWND, Class1.GWL_STYLE, style);
//放进去
Class1.SetParent(HWND, parent);
}
}
}
效果如图
没有全部填充到WP的窗口中,只显示一部分,不是想要的效果,使用
Rect rect = new Rect();
Class1.GetWindowRect(parent, out rect);
显示的位置也不正确,找不到有效的解决办法,换了一种思路
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Width="500" Height="350" Loaded="Window_Loaded">
<Grid>
<Border x:Name="WndHost"></Border>
</Grid>
</Window>
namespace WpfApplication2
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
EmbeddedApp ea = new EmbeddedApp(WndHost, 100, 100);
WndHost.Child = ea;
}
}
}
class EmbeddedApp : HwndHost, IKeyboardInputSink
{
private Border WndHoster;
private double screenW, screenH;
public EmbeddedApp(Border b, double sW, double sH)
{
WndHoster = b;
screenW = sW;
screenH = sH;
}
protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
//启动
var process = new Process();
ProcessStartInfo info = new ProcessStartInfo("C:\\Program Files\\Company\\Workstation\\Software.exe");
info.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(info);
var m_WindowName = "Software_name";
IntPtr HWND = IntPtr.Zero;
int times = 60 * 5;//给5分钟的启动时间
while (times > 0)
{
times--;
HWND = Class1.FindWindow(null, m_WindowName);
if (HWND == IntPtr.Zero) //稍等一会,不然可能找不到
{
Thread.Sleep(1000);
continue;
}
break;
}
//改变样式
Class1.ShowWindow(HWND, Class1.SW_SHOW);
Class1.EnableWindow(HWND, true);
int style = Class1.GetWindowLong(HWND, Class1.GWL_STYLE);
style = style & ~((int)Class1.WS_CAPTION) & ~((int)Class1.WS_THICKFRAME);
style |= ((int)Class1.WS_CHILD);
Class1.SetWindowLong(HWND, Class1.GWL_STYLE, style);
//嵌入进去
Class1.SetParent(HWND, hwndParent.Handle); ;
return new HandleRef(this, HWND);
}
protected override void DestroyWindowCore(HandleRef hwnd)
{
Console.WriteLine("11");
}
}
结果exe打开的窗口可以完全嵌入到WPF打开的窗口中,完成想要的效果。