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

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

  • 相关阅读:
    第四章 利用函数实现指定的功能
    5-7 点到原点的距离(多态)
    5-2 宠物的生长(多态)
    5-7 学生cpp成绩统计
    5-6 学生CPP成绩计算
    php将远程图片下载保存到本地
    vs2010 调试快捷键
    vs2010 快捷键大全
    [C#] 使用Application.AddMessageFilter当做Form的热键
    C# 收发和处理自定义的WINDOWS消息
  • 原文地址:https://www.cnblogs.com/kenter/p/2290965.html
Copyright © 2020-2023  润新知