因为代码在项目中,不好直接发。我就文字加API来回复了。
1、自定义个控件,继承于PANEL,这个就是你子exe所在的容器了
2、你这个控件里面逻辑代码实现,拉起子进程,获取其主窗体句柄,然后将子进程的主窗体边框去掉,强制MOVE到你控件中,大小就设置成控件的大小
3、重写OnResize,这里需要根据控件大小重设子进程主窗体的大小(很好理解)
下面解决你的通信问题:
1、这个方法有些太多了,namepipe、socket甚至serialport(脑残会用http://forum.china.unity3d.com/static/image/smiley/default/lol.gif3d 官方论坛)" />),我用的是remoting
WIN32的API如下:
#region Win32
API
private const
int WM_KEYDOWN = 0x0100;
[DllImport("User32.dll",
EntryPoint = "http://forum.china.unity3d.com/static/image/smiley/default/titter.gif3d 官方论坛)" />ostMessage")]
private
static extern int PostMessage(
IntPtr
hWnd, //
handle
to
destination
window
int
Msg, //
message
IntPtr
wParam, //
first
message
parameter
IntPtr
lParam //
second
message
parameter
);
[DllImport("USER32",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling =
true, CallingConvention =
CallingConvention.Winapi)]
private
static extern IntPtr GetSystemMenu(IntPtr WindowHandle, int
bReset);
[DllImport("User32.dll")]
internal
static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("USER32",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling =
true, CallingConvention =
CallingConvention.Winapi)]
private
static extern int AppendMenuW(IntPtr MenuHandle, int Flags, int
NewID, String Item);
[DllImport("USER32",
SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling =
true, CallingConvention =
CallingConvention.Winapi)]
private
static extern int InsertMenuW(IntPtr hMenu, int Position, int
Flags, int NewId, String Item);
[DllImport("User32.dll")]
internal
static extern bool EnableMenuItem(IntPtr hMenu, Int32
uIDEnableItem, Int32 uEnable);
[DllImport("user32.dll")]
internal
static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint
uFlags);
[DllImport("User32.dll")]
internal
static extern int DrawMenuBar(IntPtr hWnd);
internal
const UInt32 MF_ENABLED = 0x00000000;
internal
const UInt32 MF_GRAYED = 0x00000001;
internal
const UInt32 MF_DISABLED = 0x00000002;
internal
const UInt32 MF_BYCOMMAND = 0x00000000;
internal
const UInt32 MF_BYPOSITION = 0x00000400;
[DllImport("user32.dll")]
static extern
IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll",
CharSet = CharSet.Auto, ExactSpelling = true)]
public static
extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static
extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll",
EntryPoint = "GetWindowThreadProcessId", SetLastError =
true,
CharSet =
CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private
static extern long GetWindowThreadProcessId(long hWnd, long
lpdwProcessId);
[DllImport("user32.dll",
SetLastError = true)]
private
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);
[DllImport("user32.dll",
SetLastError = true)]
private
static extern long SetParent(IntPtr hWndChild, IntPtr
hWndNewParent);
[DllImport("user32.dll",
EntryPoint = "GetWindowLongA", SetLastError =
true)]
private
static extern long GetWindowLong(IntPtr hwnd, int
nIndex);
[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 SetWindowPos(IntPtr hwnd, long hWndInsertAfter,
long x, long y, long cx, long cy, long wFlags);
[DllImport("user32.dll",
SetLastError = true)]
private
static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx,
int cy, bool repaint);
[DllImport("user32.dll",
EntryPoint = "http://forum.china.unity3d.com/static/image/smiley/default/titter.gif3d 官方论坛)" />ostMessageA",
SetLastError = true)]
private
static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam,
uint lParam);
[DllImport("user32.dll",
SetLastError = true)]
private
static extern IntPtr GetParent(IntPtr hwnd);
[DllImport("user32.dll",
EntryPoint = "ShowWindow", SetLastError = true)]
private
static extern bool ShowWindow(IntPtr hWnd, int
nCmdShow);
[DllImport("User32.dll",
EntryPoint = "SendMessage")]
private
static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam,
string lParam);
[DllImport("user32.dll",
SetLastError = true)]
private
static extern void SwitchToThisWindow(IntPtr hWnd, bool
fAltTab);
private const
int SWP_NOOWNERZORDER = 0x200;
private const
int SWP_NOREDRAW = 0x8;
private const
int SWP_NOZORDER = 0x4;
private const
int SWP_SHOWWINDOW = 0x0040;
private const
int WS_EX_MDICHILD = 0x40;
private const
int SWP_FRAMECHANGED = 0x20;
private const
int SWP_NOACTIVATE = 0x10;
private const
int SWP_ASYNCWINDOWPOS = 0x4000;
private const
int SWP_NOMOVE = 0x2;
private const
int SWP_NOSIZE = 0x1;
private const
int GWL_STYLE = (-16);
private const
int WS_VISIBLE = 0x10000000;
private const
int WS_MAXIMIZE = 0x01000000;
private const
int WS_BORDER = 0x00800000;
private const
int WM_CLOSE = 0x10;
private const
int WS_CHILD = 0x40000000;
private const
int WS_POPUP = -2147483648;
private const
int WS_CLIPSIBLINGS = 0x04000000;
private const
int SW_HIDE
= 0; //{隐藏,
并且任务栏也没有最小化图标}
private const
int SW_SHOWNORMAL
= 1;
//{用最近的大小和位置显示, 激活}
private const
int SW_NORMAL
= 1; //{同
SW_SHOWNORMAL}
private const
int SW_SHOWMINIMIZED
= 2; //{最小化, 激活}
private const
int SW_SHOWMAXIMIZED
= 3; //{最大化, 激活}
private const
int SW_MAXIMIZE
= 3; //{同
SW_SHOWMAXIMIZED}
private const
int SW_SHOWNOACTIVATE =
4; //{用最近的大小和位置显示, 不激活}
private const
int SW_SHOW
= 5; //{同
SW_SHOWNORMAL}
private const
int SW_MINIMIZE
= 6; //{最小化,
不激活}
private const
int SW_SHOWMINNOACTIVE = 7; //{同
SW_MINIMIZE}
private const
int SW_SHOWNA
= 8; //{同
SW_SHOWNOACTIVATE}
private const
int SW_RESTORE
= 9; //{同
SW_SHOWNORMAL}
private const
int SW_SHOWDEFAULT
= 10; //{同
SW_SHOWNORMAL}
private const
int SW_MAX
= 10; //{同
SW_SHOWNORMAL}
const int
WM_SETTEXT = 0x000C;
///
///
设置窗体属性
///
///
///
///
///
private
static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int
dwNewLong)
{
if
(IntPtr.Size == 4)
{
return
SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
}
return
SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
}
//static
KeyboardHook hook;
///
///
嵌入程序
///
///
应用程序
///
容器控件
private
static void EmbedProcess(Process p_application, ShineExcuContainer
p_appContainer)
{
SetParent(p_application.MainWindowHandle,
p_appContainer.Handle);
MoveWindow(p_appContainer._AppProcess.MainWindowHandle,
0, 0, p_appContainer.Width, p_appContainer.Height,
true);
int style =
(int)GetWindowLong(p_application.MainWindowHandle,
GWL_STYLE);
SetWindowLong(new HandleRef(p_appContainer,
p_appContainer._AppProcess.MainWindowHandle), GWL_STYLE,
(WS_VISIBLE | WS_CHILD | WS_MAXIMIZE) &
style);
p_appContainer.DeleteSystemMenuItems();
}
|