星期三, 7月 01, 2009

讓程式不能重覆開啟


//專案檔最前面加上,1.檢查Mutex,若XMonitor已存在,則離開。
if ProgramAlreadyExists() then exit;

function ProgramAlreadyExists():boolean;
var
hRunningForm: Thandle;
begin
//g_hMutex:=CreateMutex(nil, False, 'X-Console');
g_hMutex:=CreateEvent(nil, False, TRUE, 'X-Monitor');
if (g_hMutex=0) OR (GetLastError()=ERROR_ALREADY_EXISTS) then //已存在
begin
hRunningForm := FindWindow(_XMONITOR_CLASS_NAME,nil);
if hRunningForm<>0 then
begin
SetForegroundWindow(hRunningForm);
// PostMessage(hRunningForm,WM_SYSCOMMAND,SC_MAXIMIZE,0);
end;

Result:=true;
end
else
Result:=false;
end;