以下是全部代码
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace EmbedDemo
{
public partial class frmMain : Form
{
private const int GWL_STYLE = (-16);
private const int WS_VISIBLE = 0x10000000;
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
//SetParent(IntPtr hWndChild, IntPtr hWndNewParent);这个方法很重要,就是将hWndChild指向开启exe的窗体嵌入到hWndNewParent窗体的某个控件上,或者是窗体本身的容器
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
// MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);这个方法是windows的api,见名知意,是移动hwnd所指的窗体到指定的位置,并且指定是否需要重绘
[DllImport("user32.dll", SetLastError = true)]
public static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("kernel32.dll")]
public static extern long GetLastError();
public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
{
if (IntPtr.Size == 4)
{
return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
}
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
}
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
//以下这段代码是通过命令行方式调起一个exe程序,获取这个程序的句柄然后嵌入主的winform窗体中
//string exePath = System.Configuration.ConfigurationManager.AppSettings["exePath"];
ProcessStartInfo info = new ProcessStartInfo(@"D:\AAA\fxxxmm\Flexem.Box.Client.Desktop.exe");
info.WindowStyle = ProcessWindowStyle.Minimized;
info.UseShellExecute = false;
info.CreateNoWindow = true;
info.RedirectStandardOutput = false;
Process appProcess = System.Diagnostics.Process.Start(info);
appProcess.WaitForInputIdle();
this.Show();
try
{
EmbedProcess();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
///
/// 将指定的程序嵌入指定的控件
///
private void EmbedProcess()
{
string title = System.Configuration.ConfigurationManager.AppSettings["Title"];//要查找的外部应用程序窗口标题
IntPtr P = new IntPtr(0);
while (true)
{
P = FindWindow(null, "FlexManager");//通过标题查找窗口句柄,当然也可以按class查找,如果需要查找子窗口需要FindWindowEx函数
Thread.Sleep(100);
if (P == IntPtr.Zero)
continue;
else
break;
}
try
{
// 将外部应用程序嵌入到本窗口
long ret = SetParent(P, this.Handle);
if (ret == 0)
{
MessageBox.Show("ErrorCode:"+ GetLastError().ToString());
}
// 移除边框样式
SetWindowLong(new HandleRef(this, P), GWL_STYLE, WS_VISIBLE);
//移动窗口
MoveWindow(P, 0, 0, this.Width, this.Height, true);
}
catch (Exception ex1)
{
Console.WriteLine(ex1.Message);
}
}
}
}
就想嵌入的这个软件和嵌入其他软件一样正常显示就行,不要有那半透明边框什么的