建立两个MFC Dialog应用程序,分别为 ParentApp.exe 和 ChildApp.exe。通过 ParentApp 调用 ChildApp,传递参数给 ChildApp,让 ChildApp 获取 ParentApp 配置文件信息,代码前提条件是ParentApp.exe 、 ChildApp.exe 和 ParentApp.ini 放同一文件夹下。以下为部分代码:
//ParentAppDlg.cpp
/*
父窗口调用子窗口,让子窗口显示父窗口配置文件信息
*/
void CParentAppDlg::OnBnClickedCallChild()
{
// TODO: 在此添加控件通知处理程序代码
TCHAR szPath[MAX_PATH];
GetModuleFileName(AfxGetApp()->m_hInstance, szPath, MAX_PATH);
//获取子程序路径
CString strExePath(szPath);
int iPos = strExePath.ReverseFind(_T('\\'));
ASSERT(-1 != iPos);
strExePath = strExePath.Mid(0, iPos + 1);
strExePath += _T("ChildApp.exe");
//配置文件的路径加上引号,因为命令行参数以空格区分,有些路径包含空格,
//会被识别为两个或多个参数,加引号可解决此问题
CString strPath(_T("\""));
strPath += szPath;
iPos = strPath.ReverseFind(_T('\\'));
ASSERT(-1 != iPos);
strPath = strPath.Mid(0, iPos + 1);
strPath += _T("ParentApp.ini");
strPath += _T("\"");
//获取 ParentApp 句柄
long lWndHwnd = (long)m_hWnd;
CString strHwndParam;
strHwndParam.Format(_T("%d"), lWndHwnd);
CString strParam;
strParam = CString(_T(" wnd ")) + strHwndParam + CString(_T(" path ")) + strPath;
ShellExecute(m_hWnd, _T("open"), strExePath, strParam, NULL, SW_SHOWNORMAL);
}
BOOL CChildAppApp::InitInstance()
{
// 如果一个运行在 Windows XP 上的应用程序清单指定要
// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
//则需要 InitCommonControlsEx()。否则,将无法创建窗口。
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// 将它设置为包括所有要在应用程序中使用的
// 公共控件类。
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
// 标准初始化
// 如果未使用这些功能并希望减小
// 最终可执行文件的大小,则应移除下列
// 不需要的特定初始化例程
// 更改用于存储设置的注册表项
// TODO: 应适当修改该字符串,
// 例如修改为公司或组织名
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
int iArg;
LPWSTR *szArgList;
szArgList = CommandLineToArgvW(GetCommandLineW(), &iArg);
if(NULL == szArgList)
{
AfxMessageBox(_T("获取命令行参数失败!"));
LocalFree(szArgList);
return FALSE;
}
//实际只传4个参数,却判断参数个数是否为5?因为第一个参数是当前程序路径
else if(5 != iArg)
{
AfxMessageBox(_T("命令行参数个数不正确!"));
LocalFree(szArgList);
return FALSE;
}
else if((0 != _wcsicmp(szArgList[1], L"wnd")) || (0 != _wcsicmp(szArgList[3], L"path")))
{
AfxMessageBox(_T("命令行参数出错!"));
LocalFree(szArgList);
return FALSE;
}
HWND parentHwnd = (HWND)(int)(wcstod(szArgList[2], NULL));
ASSERT(parentHwnd);
CWnd *pWnd = CWnd::FromHandle(parentHwnd);
ASSERT(pWnd);
CString strConfigPath(szArgList[4]);
LocalFree(szArgList);
if(!PathFileExists(strConfigPath))
{
AfxMessageBox(_T("命令行参数指出的配置文件不存在!"));
return FALSE;
}
//通过构造函数,将父窗口 Cwnd * 和配置文件路径传给子窗口
CChildAppDlg dlg(pWnd, strConfigPath);
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此处放置处理何时用“确定”来关闭
// 对话框的代码
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用“取消”来关闭
// 对话框的代码
}
// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
// 而不是启动应用程序的消息泵。
return FALSE;
}
//子窗口在 CChildAppDlg::OnInitDialog() 函数中读取配置文件信息
//ChildAppDlg.cpp
BOOL CChildAppDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//...
// TODO: 在此添加额外的初始化代码
TCHAR szInfo[1000];
if(GetPrivateProfileString(_T("TEST"), _T("INFO"), _T("未找到 INFO 相关信息"), szInfo, 1000, m_strConfigPath))
{
GetDlgItem(IDC_SHOW)->SetWindowText(szInfo);
}
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}