MFC将其他EXE镶入对话框中的问题·高手请解答

qq_21961039 2017-11-18 11:24:00
HANDLE hProcess;
HWND apphWnd = NULL;


int CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD pID;
DWORD tpID = GetWindowThreadProcessId(hwnd,&pID);
if(tpID == (DWORD)lParam)
{
apphWnd = hwnd;
return false;
}
return true;
}




HANDLE StartProcess(LPCTSTR program, LPCTSTR args)
{
HANDLE hPro = NULL;
PROCESS_INFORMATION processInfo;
STARTUPINFO startupInfo;
::ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
//设置进程创建时不显示窗口
//startupInfo.dwFlags = /*STARTF_USEPOSITION*/STARTF_USESHOWWINDOW;
startupInfo.dwFlags = STARTF_USEPOSITION;//STARTF_USESHOWWINDOW;
startupInfo.dwX = 600;
startupInfo.dwY = 900;
startupInfo.wShowWindow = SW_HIDE;
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))
{
//延迟0.5s,等待进程创建成功
Sleep(500);
while(true)
{
::EnumWindows(&EnumWindowsProc, processInfo.dwThreadId);//Iterate all windows
if(NULL != apphWnd)
break;
}
hPro = processInfo.hProcess;

}
return hPro;
}







if(apphWnd != NULL)
{
OnQuit();
}

CRect rect;
GetClientRect(&rect);//get our dialog size into rect
hProcess=StartProcess("我的工具.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, WS_VISIBLE);//eraze title of ms paint window.
//Positioning ms paint.
::MoveWindow(apphWnd, rect.left, rect.top,rect.right, rect.bottom, true);
//窗口重绘,(因创建exe时,设置为SW_HIDE,导致exe窗口会被父窗口覆盖一部分)
Invalidate();
::UpdateWindow(apphWnd);
::ShowWindow(apphWnd,SW_SHOW);
}
else
MessageBox("Cannot find Window");






OnQuit()
{
TerminateProcess(hProcess,0);
apphWnd = NULL;
}



运行后第一个画面没有问题 已经镶入进去 如下图



但程序正式启动后 就变了,位置不对

跑到最下面去了

请问怎么解决
...全文
271 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
shen_wei 2017-11-22
  • 打赏
  • 举报
回复
嵌入的程序是最难的搞定的,还是写个类似的功能算了。。。
赵4老师 2017-11-21
  • 打赏
  • 举报
回复
搜“后台调用外部程序的完美实现(使用CreateDesktop建立隐藏桌面)”
schlafenhamster 2017-11-19
  • 打赏
  • 举报
回复

BOOL CMsPaintInMyDlgDlg::GoMsPaint(char *path,BOOL bWait)
{
	char pathexe[MAX_PATH];
	strcpy(pathexe,"mspaint.exe");	
	strcat(pathexe,path);
	STARTUPINFO sinfo;
	PROCESS_INFORMATION pinfo;
	memset (&sinfo,0,sizeof(STARTUPINFO));
	sinfo.cb=sizeof(STARTUPINFO);
	sinfo.dwFlags|=STARTF_USESHOWWINDOW;
	sinfo.wShowWindow=SW_HIDE;
//BOOL fsuccess=0;
	BOOL fsuccess=CreateProcess(NULL,//lpApplicationName
				  pathexe,			//lpCommanderLine
				  NULL,				//lpProcessAttributes
				  NULL,				//lpThreadAttributes
				  FALSE,			//bInheritHandles
				  NORMAL_PRIORITY_CLASS,//dwCreationFlags
				  NULL,				//lpEnvironment
				  NULL,				//lpCurrentDirectory
				  &sinfo,			//lpStartupInfo
				  &pinfo);			//lpProcessInformation
//
	if(fsuccess)
	{
		Sleep(500);
		CRect rect;
		GetClientRect(&rect);//get our dialog size into rect
		m_hPaint=::FindWindow("MSPaintApp",0);
        ::SetParent(m_hPaint,m_hWnd);//set parent of ms paint to our dialog. 
		::ShowWindow(m_hPaint,SW_SHOW);
        ::MoveWindow(m_hPaint, 0, 0, rect.Width(), rect.Height(), true);
		::SetFocus(m_hPaint);
	}
	return(fsuccess);
}
schlafenhamster 2017-11-19
  • 打赏
  • 举报
回复
你还是 用 mspaint 把程序 搞通,再改 “别人写的一个程序”的 路径,要 全路径。 除非你把 “别人写的一个程序“ 放 system32 目录下 另外 要用 spy++ (vc工具下就有)找到 “别人写的一个程序” 的 类名 ! 实在不行 还用 你的 EnumWindows
qq_21961039 2017-11-19
  • 打赏
  • 举报
回复
引用 12 楼 schlafenhamster 的回复:
1 你的 "编辑器.exe" 是什么 是微软的 NotePad 吗? 2 m_hPaint=::FindWindow("编辑器.exe",0);// 这样是 找不到的, 要 类名,如 画图 mspaint 的 类名 是 "MSPaintApp" 记事本 是 "NotePad"; 可以用 spy++ 找到 这个 名。
不是微软的 而是 别人写的一个程序
schlafenhamster 2017-11-19
  • 打赏
  • 举报
回复
1 你的 "编辑器.exe" 是什么 是微软的 NotePad 吗? 2 m_hPaint=::FindWindow("编辑器.exe",0);// 这样是 找不到的, 要 类名,如 画图 mspaint 的 类名 是 "MSPaintApp" 记事本 是 "NotePad"; 可以用 spy++ 找到 这个 名。
qq_21961039 2017-11-19
  • 打赏
  • 举报
回复
引用 9 楼 schlafenhamster 的回复:
按钮没反映呀 哪里的 按钮 ? 我是放 对话框 的 初始化 中的 “我的程序.exe“ 必须使用 全路径, mspaint.exe 在 system32 目录下,所以不要 全路径 你 先 用 mspaint.exe 试试
我邮箱77067720@qq.com 你能发套代码 到我邮箱里 叫我借鉴一下吗?
qq_21961039 2017-11-19
  • 打赏
  • 举报
回复
引用 9 楼 schlafenhamster 的回复:
按钮没反映呀 哪里的 按钮 ? 我是放 对话框 的 初始化 中的 “我的程序.exe“ 必须使用 全路径, mspaint.exe 在 system32 目录下,所以不要 全路径 你 先 用 mspaint.exe 试试


HWND apphWnd = NULL;
HWND  m_hPaint;
/////////////////////////////////////////////////////////////////////////////
// CXY2CK dialog

int CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    DWORD pID;
    DWORD tpID = GetWindowThreadProcessId(hwnd,&pID);
    if(tpID == (DWORD)lParam)
    {
        apphWnd = hwnd;
        return false;
    }
    return true;
}
BOOL CXY2CK::GoMsPaint(char *path,BOOL bWait)
{
    char pathexe[MAX_PATH];
    strcpy(pathexe,"编辑器.exe");   
    strcat(pathexe,path);
    STARTUPINFO sinfo;
    PROCESS_INFORMATION pinfo;
    memset (&sinfo,0,sizeof(STARTUPINFO));
    sinfo.cb=sizeof(STARTUPINFO);
    sinfo.dwFlags|=STARTF_USESHOWWINDOW;
    sinfo.wShowWindow=SW_HIDE;
//BOOL fsuccess=0;
    BOOL fsuccess=CreateProcess(NULL,//lpApplicationName
                  pathexe,            //lpCommanderLine
                  NULL,                //lpProcessAttributes
                  NULL,                //lpThreadAttributes
                  FALSE,            //bInheritHandles
                  NORMAL_PRIORITY_CLASS,//dwCreationFlags
                  NULL,                //lpEnvironment
                  NULL,                //lpCurrentDirectory
                  &sinfo,            //lpStartupInfo
                  &pinfo);            //lpProcessInformation
//        ::SetParent(m_hPaint,m_hWnd);//set parent of ms paint to our dialog. 

    if(fsuccess)
    {
        Sleep(500);
        CRect rect;
        GetClientRect(&rect);//get our dialog size into rect
        m_hPaint=::FindWindow("编辑器.exe",0);
        ::SetParent(m_hPaint,m_hWnd);//set parent of ms paint to our dialog. 
        ::ShowWindow(m_hPaint,SW_SHOW);
        ::MoveWindow(m_hPaint, 0, 0, rect.Width(), rect.Height(), true);
        ::SetFocus(m_hPaint);
    }
    return(fsuccess);
}
 
CXY2CK::CXY2CK(CWnd* pParent /*=NULL*/)
	: CDialog(CXY2CK::IDD, pParent)
{
	//{{AFX_DATA_INIT(CXY2CK)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CXY2CK::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);


	//{{AFX_DATA_MAP(CXY2CK)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP*/
}


BEGIN_MESSAGE_MAP(CXY2CK, CDialog)
	//{{AFX_MSG_MAP(CXY2CK)
		// NOTE: the ClassWizard will add message map macros here
		ON_BN_CLICKED(IDOK, OnOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CXY2CK message handlers

void CXY2CK::OnOk() 
{
	
	GoMsPaint("xy2.exe",FALSE);
    
	
}

麻烦你再看下,有没有错误
schlafenhamster 2017-11-19
  • 打赏
  • 举报
回复
按钮没反映呀 哪里的 按钮 ? 我是放 对话框 的 初始化 中的 “我的程序.exe“ 必须使用 全路径, mspaint.exe 在 system32 目录下,所以不要 全路径 你 先 用 mspaint.exe 试试
qq_21961039 2017-11-19
  • 打赏
  • 举报
回复
引用 6 楼 schlafenhamster 的回复:
没有 qq ,什么问题 ?
但是这样 组建起来后 按钮没反映呀
qq_21961039 2017-11-19
  • 打赏
  • 举报
回复
引用 6 楼 schlafenhamster 的回复:
没有 qq ,什么问题 ?

char pathexe[MAX_PATH];
    strcpy(pathexe,"编辑器.exe");   
    strcat(pathexe,path);
    STARTUPINFO sinfo;
    PROCESS_INFORMATION pinfo;
    memset (&sinfo,0,sizeof(STARTUPINFO));
    sinfo.cb=sizeof(STARTUPINFO);
    sinfo.dwFlags|=STARTF_USESHOWWINDOW;
    sinfo.wShowWindow=SW_HIDE;
//BOOL fsuccess=0;
    BOOL fsuccess=CreateProcess(NULL,//lpApplicationName
                  pathexe,            //lpCommanderLine
                  NULL,                //lpProcessAttributes
                  NULL,                //lpThreadAttributes
                  FALSE,            //bInheritHandles
                  NORMAL_PRIORITY_CLASS,//dwCreationFlags
                  NULL,                //lpEnvironment
                  NULL,                //lpCurrentDirectory
                  &sinfo,            //lpStartupInfo
                  &pinfo);            //lpProcessInformation
//        ::SetParent(m_hPaint,m_hWnd);//set parent of ms paint to our dialog. 

    if(fsuccess)
    {
        Sleep(500);
        CRect rect;
        GetClientRect(&rect);//get our dialog size into rect
        m_hPaint=::FindWindow("xy2.exe",0);
        ::SetParent(m_hPaint,m_hWnd);//set parent of ms paint to our dialog. 
        ::ShowWindow(m_hPaint,SW_SHOW);
        ::MoveWindow(m_hPaint, 0, 0, rect.Width(), rect.Height(), true);
        ::SetFocus(m_hPaint);
    }
    return(fsuccess);
按钮调用 是这样写吗?
GoMsPaint("我的程序.exe",FALSE);
schlafenhamster 2017-11-19
  • 打赏
  • 举报
回复
没有 qq ,什么问题 ?
qq_21961039 2017-11-19
  • 打赏
  • 举报
回复
引用 4 楼 schlafenhamster 的回复:
HWND m_hPaint; msPaint 的 窗口 m_hPaint=::FindWindow("MSPaintApp",0);// mspaint.exe 的 类名 调用 GoMsPaint("",FALSE);
有QQ吗? 还是有问题 能详细问你下不
schlafenhamster 2017-11-19
  • 打赏
  • 举报
回复
HWND m_hPaint; msPaint 的 窗口 m_hPaint=::FindWindow("MSPaintApp",0);// mspaint.exe 的 类名 调用 GoMsPaint("",FALSE);
qq_21961039 2017-11-19
  • 打赏
  • 举报
回复
引用 2 楼 schlafenhamster 的回复:

BOOL CMsPaintInMyDlgDlg::GoMsPaint(char *path,BOOL bWait)
{
	char pathexe[MAX_PATH];
	strcpy(pathexe,"mspaint.exe");	
	strcat(pathexe,path);
	STARTUPINFO sinfo;
	PROCESS_INFORMATION pinfo;
	memset (&sinfo,0,sizeof(STARTUPINFO));
	sinfo.cb=sizeof(STARTUPINFO);
	sinfo.dwFlags|=STARTF_USESHOWWINDOW;
	sinfo.wShowWindow=SW_HIDE;
//BOOL fsuccess=0;
	BOOL fsuccess=CreateProcess(NULL,//lpApplicationName
				  pathexe,			//lpCommanderLine
				  NULL,				//lpProcessAttributes
				  NULL,				//lpThreadAttributes
				  FALSE,			//bInheritHandles
				  NORMAL_PRIORITY_CLASS,//dwCreationFlags
				  NULL,				//lpEnvironment
				  NULL,				//lpCurrentDirectory
				  &sinfo,			//lpStartupInfo
				  &pinfo);			//lpProcessInformation
//
	if(fsuccess)
	{
		Sleep(500);
		CRect rect;
		GetClientRect(&rect);//get our dialog size into rect
		m_hPaint=::FindWindow("MSPaintApp",0);
        ::SetParent(m_hPaint,m_hWnd);//set parent of ms paint to our dialog. 
		::ShowWindow(m_hPaint,SW_SHOW);
        ::MoveWindow(m_hPaint, 0, 0, rect.Width(), rect.Height(), true);
		::SetFocus(m_hPaint);
	}
	return(fsuccess);
}
请问 m_hPaint 是什么控件?
qq_21961039 2017-11-19
  • 打赏
  • 举报
回复
坐等有人帮解决!

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧