在很多情况下,我们需要将当前运行的程序重新运行;此时我们就必须在程序关闭后立即重新运行程序,形成新的进程;
实例代码:
1、重启按钮事件中添加代码:
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)g_bIsRunAgain=true;//控制是否重新运行的变量
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)this-SendMessage(WM_CLOSE);
2、在OnClose()消息处理中加入代码:
if(g_bIsRunAgain)
{
char pBuf[MAX_PATH]; //存放路径的变量
GetCurrentDirectory(MAX_PATH,pBuf); //获取程序的当前目录
strcat(pBuf,"");
strcat(pBuf,AfxGetApp()-m_pszExeName);
strcat(pBuf,".exe");
CString strPath= (CString) pBuf;
STARTUPINFO StartInfo;
PROCESS_INFORMATION procStruct;
memset(&StartInfo, 0, sizeof(STARTUPINFO));
StartInfo.cb = sizeof(STARTUPINFO);
::CreateProcess(
(LPCTSTR) strPath,
NULL,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&StartInfo,
&procStruct);
}
CDialog::OnClose();