一种更有优势的办法:
工程文件中:
winapi winmain(hinstance, hinstance, lpstr, int)
{
try
{
handle mutex=openmutex(mutex_all_access,false,"chenicehonlyone");
if (mutex==null)
mutex=createmutex(null,true,"chenicehonlyone");
else
{
sendmessage(hwnd_broadcast,registerwindowmessage("cheniconlyoneeh"),0,0);
return 0;
}
application->initialize();
application->title = "考场英豪";
application->createform(__classid(tform1), &form1);
application->createform(__classid(tfrmlogin), &frmlogin);
application->run();
releasemutex(mutex);
}
catch (exception &exception)
{
application->showexception(&exception);
}
return 0;
}
主窗体中:
void __fastcall tform1::formcreate(tobject *sender)
{
msgrestore=registerwindowmessage("cheniconlyoneeh");
application->hookmainwindow(messagehandler);
}
//---------------------------------------------------------------------------
bool __fastcall tform1::messagehandler(tmessage &message)
{
if (message.msg==msgrestore)
{
application->restore();
application->bringtofront();
return true;
}
else return false;
}
//---------------------------------------------------------------------------
void __fastcall tform1::formdestroy(tobject *sender)
{
application->unhookmainwindow(messagehandler);
}
我在这里做的只是简单的将应用程序提到前台,你大可以举一反三做一个提示或记录重复启动次数之类的事,也可以根据这个方法给程序注册一些消息,在程序间传递控制指令,实现多个程序间的互动。