借化纤。。<br>function DoIExist(WndTitle : String) : Boolean;<br>var<br> hSem : THandle;<br> hWndMe,<br> hWndPrev : HWnd;<br> semNm,<br> wTtl : Array[0..256] of Char;<br>begin<br><br> Result := False;<br><br> //Initialize arrays<br> StrPCopy(semNm, 'SemaphoreName');<br> StrPCopy(wTtl, WndTitle);<br><br> //Create a Semaphore in memory - If this is the first instance, then<br> //it should be 0.<br> hSem := CreateSemaphore(nil, 0, 1, semNm);<br><br> //Now, check to see if the semaphore exists<br> if ((hSem <> 0) AND (GetLastError() = ERROR_ALREADY_EXISTS)) then begin<br> CloseHandle(hSem);<br><br> //We'll first get the currently executing window's handle then change its title<br> //so we can look for the other instance<br> hWndMe := FindWindow(nil, wTtl);<br> SetWindowText(hWndMe, 'zzzzzzz');<br><br> //What we want to do now is search for the other instance of this window<br> //then bring it to the top of the Z-order stack.<br> hWndMe := FindWindow(nil, wTtl);<br> if (hWndMe <> 0) then begin<br> if IsIconic(hWndMe) then<br> ShowWindow(hWndMe, SW_SHOWNORMAL)<br> else<br> SetForegroundWindow(hWndMe);<br> end;<br><br> Result := True;<br><br> //Could put the Halt here, instead of in the FormCreate method,<br> //unless you want to do some extra processing.<br><br> //Halt;<br> end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> if DoIExist(Self.Caption) then<br> Halt;<br>end;<br>