zoukankan      html  css  js  c++  java
  • 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 

  • 相关阅读:
    CodeForces 681D Gifts by the List (树上DFS)
    UVa 12342 Tax Calculator (水题,纳税)
    CodeForces 681C Heap Operations (模拟题,优先队列)
    CodeForces 682C Alyona and the Tree (树上DFS)
    CodeForces 682B Alyona and Mex (题意水题)
    CodeForces 682A Alyona and Numbers (水题,数学)
    Virtualizing memory type
    页面跳转
    PHP Misc. 函数
    PHP 5 Math 函数
  • 原文地址:https://www.cnblogs.com/kenter/p/2290965.html
Copyright © 2011-2022 走看看