在很多情況下,我們需要將當前運行的程序重新運行;此時我們就必須在程序關閉後立即重新運行程序,形成新的進程;
實例代碼:
1、重啟按鈕事件中添加代碼:
g_bIsRunAgain=true;//控制是否重新運行的變量
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();