HANDLE handle;//process handle
HWND apphwnd = NULL;//window handle
BOOL find = FALSE;
/*************Global functions for hosting******************/
//Function to enumerate all windows.
int CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param)
{
DWORD pID;
DWORD TpID = GetWindowThreadProcessId(hwnd, &pID);//get process id
if (TpID == (DWORD)param)
{
apphwnd = hwnd;//hwnd is the window handle
return false;
}
return true;
}
//Functio to start a orocess and return the process handle
HANDLE StartProcess(LPCTSTR program, LPCTSTR args)
{
HANDLE hProcess = NULL;
PROCESS_INFORMATION processInfo;
STARTUPINFO startupInfo;
::ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
if(::CreateProcess(program, (LPTSTR)args,
NULL, // process security
NULL, // thread security
FALSE, // no inheritance
0, // no startup flags
NULL, // no special environment
NULL, // default startup directory
&startupInfo,
&processInfo))
{ /* success */
for (int i = 0; i < 150; i++)
{
Sleep(30);
//WaitForInputIdle(processInfo.hProcess, 10000);
::EnumWindows(&EnumWindowsProc, processInfo.dwThreadId);//Iterate all windows
hProcess = processInfo.hProcess;
if (apphwnd) //找到相应的窗口
break;
}
} /* success */
return hProcess;
}
/**********************************************************/
//handle for host menu
void CHostMSPaintDlg::OnActionHostmspaint()
{
CRect rect, rectWin, rectApp;
GetClientRect(&rect);//get our dialog size into rect
handle=StartProcess("C:\\WINDOWS\\system32\\mspaint.exe","");//Start ms paint
if(apphwnd!=NULL)//check for window handle
{
::SetParent(apphwnd,m_hWnd);//set parent of ms paint to our dialog.
SetWindowLong(apphwnd, GWL_STYLE, GetWindowLong(apphwnd, GWL_STYLE) & ~WS_CAPTION);
::GetWindowRect(apphwnd, rectApp);
GetWindowRect(rectWin);
MoveWindow(rectWin.left, rectWin.top, rectWin.Width() + rectApp.Width() - rect.Width(),
rectWin.Height() + rectApp.Height() - rect.Height(), true); //调整自身窗口大小迎合外部程序的大小
GetClientRect(&rect);//get our dialog size into rect
::MoveWindow(apphwnd, rect.left, rect.top,rect.right, rect.bottom, true); //将外部程序移到自自身窗口里
}
else//no window for our process
MessageBox("没有找到相应的窗口,程序没有正确启动");
}
//handle for kill process menu.
void CHostMSPaintDlg::OnActionKillprocess()
{
TerminateProcess(handle,0); //kill the process using handle
//::SetParent(apphwnd, NULL);
//SetWindowLong(apphwnd, GWL_STYLE, GetWindowLong(apphwnd, GWL_STYLE) | WS_CAPTION);
apphwnd = NULL;
}
BOOL CHostMSPaintDlg::DestroyWindow()
{
OnActionKillprocess();
return CDialog::DestroyWindow();
}
HWND apphwnd = NULL;//window handle
BOOL find = FALSE;
/*************Global functions for hosting******************/
//Function to enumerate all windows.
int CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param)
{
DWORD pID;
DWORD TpID = GetWindowThreadProcessId(hwnd, &pID);//get process id
if (TpID == (DWORD)param)
{
apphwnd = hwnd;//hwnd is the window handle
return false;
}
return true;
}
//Functio to start a orocess and return the process handle
HANDLE StartProcess(LPCTSTR program, LPCTSTR args)
{
HANDLE hProcess = NULL;
PROCESS_INFORMATION processInfo;
STARTUPINFO startupInfo;
::ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
if(::CreateProcess(program, (LPTSTR)args,
NULL, // process security
NULL, // thread security
FALSE, // no inheritance
0, // no startup flags
NULL, // no special environment
NULL, // default startup directory
&startupInfo,
&processInfo))
{ /* success */
for (int i = 0; i < 150; i++)
{
Sleep(30);
//WaitForInputIdle(processInfo.hProcess, 10000);
::EnumWindows(&EnumWindowsProc, processInfo.dwThreadId);//Iterate all windows
hProcess = processInfo.hProcess;
if (apphwnd) //找到相应的窗口
break;
}
} /* success */
return hProcess;
}
/**********************************************************/
//handle for host menu
void CHostMSPaintDlg::OnActionHostmspaint()
{
CRect rect, rectWin, rectApp;
GetClientRect(&rect);//get our dialog size into rect
handle=StartProcess("C:\\WINDOWS\\system32\\mspaint.exe","");//Start ms paint
if(apphwnd!=NULL)//check for window handle
{
::SetParent(apphwnd,m_hWnd);//set parent of ms paint to our dialog.
SetWindowLong(apphwnd, GWL_STYLE, GetWindowLong(apphwnd, GWL_STYLE) & ~WS_CAPTION);
::GetWindowRect(apphwnd, rectApp);
GetWindowRect(rectWin);
MoveWindow(rectWin.left, rectWin.top, rectWin.Width() + rectApp.Width() - rect.Width(),
rectWin.Height() + rectApp.Height() - rect.Height(), true); //调整自身窗口大小迎合外部程序的大小
GetClientRect(&rect);//get our dialog size into rect
::MoveWindow(apphwnd, rect.left, rect.top,rect.right, rect.bottom, true); //将外部程序移到自自身窗口里
}
else//no window for our process
MessageBox("没有找到相应的窗口,程序没有正确启动");
}
//handle for kill process menu.
void CHostMSPaintDlg::OnActionKillprocess()
{
TerminateProcess(handle,0); //kill the process using handle
//::SetParent(apphwnd, NULL);
//SetWindowLong(apphwnd, GWL_STYLE, GetWindowLong(apphwnd, GWL_STYLE) | WS_CAPTION);
apphwnd = NULL;
}
BOOL CHostMSPaintDlg::DestroyWindow()
{
OnActionKillprocess();
return CDialog::DestroyWindow();
}
例子下载 https://files.cnblogs.com/kenter/MFC_ADD_EXE.zip