您的位置:首页 > 其它

在程序中重启自己的一种方法 (转载)

2005-12-28 16:12 429 查看
作者: ahaozi(老公仔)
在工程.cpp文件(Project1.cpp)中加入:

.....
#include "Unit1.h"
.....
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
.....
.....
if(g_bIsRunAgain)
{
AnsiString strPath;
STARTUPINFO StartInfo;
PROCESS_INFORMATION procStruct;
memset(&StartInfo, 0, sizeof(STARTUPINFO));
StartInfo.cb = sizeof(STARTUPINFO);
strPath = Application->ExeName;
if(!::CreateProcess(
(LPCTSTR) strPath.c_str(),
NULL,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&StartInfo,
&procStruct))
return 0;
}

return 0;
}
主窗口的单元头文件(Unit1.h)中加入:
extern bool g_bIsRunAgain;

主窗口的单元.cpp(Unit1.cpp)中加入:
bool g_bIsRunAgain = false;
//----------------------------------------------------------------------------
// 关闭程序
void __fastcall TForm1::btnCloseClick(TObject *Sender)
{
Close();
}
//----------------------------------------------------------------------------
// 重启应用程序
void __fastcall TForm1::btnReExcuteClick(TObject *Sender)
{
g_bIsRunAgain = true;
Close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: