VC++ 内嵌EXE

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();
}

 

例子下载 http://files.cnblogs.com/kenter/MFC_ADD_EXE.zip 

转载于:https://www.cnblogs.com/kenter/archive/2011/12/17/2290965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值