一般内嵌程序都是作为子窗口嵌入父窗口的。所以如果我们需要内嵌exe窗口程序,那么我们就需要找到exe的顶层窗口。然后去掉WS_CAPTION | WS_BORDER | WS_THICKFRAME等属性,增加WS_CHILD属性。
//查找该窗口是否存在(如果exe窗口使我们自子设立的,事实上我们可以通过进程间通信来替代findwindow)
HWND hFindWnd = ::FindWindow(_T("xxxclass_name"), _T("xxxxtitle"));
//如果窗口不存在则启动exe
if (!hFindWnd)
::ShellExecute(GetSafeHwnd(), _T("open"), _T("xxx.exe"), _T(""), _T("xxx运行目录"), SW_SHOWMAXIMIZED);
hFindWnd = ::FindWindow(_T("xxxclass_name"), _T("xxxxtitle"));
if (hFindWnd)
{
//修改窗口属性为WS_CHILD
DWORD dwModifuStyle = WS_CAPTION | WS_BORDER | WS_THICKFRAME;
long dwStyle = GetWindowLong(hFindWnd, GWL_STYLE);
dwStyle &= ~dwModifuStyle;
dwStyle |= WS_CHILD;
::SetWindowLong(hFindWnd, GWL_STYLE, dwStyle);
//设置父窗口为容器窗口
::SetParent(hFindWnd, GetSafeHwnd());
RECT rtClient;
GetClientRect(&rtClient);
::MoveWindow(hFindWnd, rtClient.left, rtClient.top, rtClient.right, rtClient.bottom, TRUE);
}
Note:当主程序退出的时候应该同步退出子程序。