www.pudn.com > srmsrc.zip > OneInst.pas


unit OneInst; 
 
interface 
 
uses 
  Windows, SysUtils, Forms; 
 
function CheckInstance:boolean; 
 
implementation 
 
 
var 
  MyAppName, MyClassName: array[0..255] of Char; 
  NumFound: Integer; 
  LastFound, MyPopup: HWND; 
 
function LookAtAllWindows(Handle: HWND; Temp: LongInt): BOOL; stdcall; 
var 
  WindowName, ClassName: Array[0..255] of Char; 
begin 
  if (GetClassName(Handle, ClassName, SizeOf(ClassName)) > 0) and 
     (StrComp(ClassName, MyClassName) = 0) and 
     (GetWindowText(Handle, WindowName, SizeOf(WindowName)) > 0) and 
     (StrComp(WindowName, MyAppName) = 0) then 
  begin 
    Inc(NumFound); 
    if Handle <> Application.Handle then LastFound := Handle; 
  end; 
  Result:= True; 
end; 
 
function CheckInstance:boolean; 
begin 
  Result:=false; 
  NumFound := 0; 
  LastFound := 0; 
  GetWindowText(Application.Handle, MyAppName, SizeOf(MyAppName)); 
  GetClassName(Application.Handle, MyClassName, SizeOf(MyClassName)); 
  EnumWindows(@LookAtAllWindows, 0); 
  if NumFound> 1 then 
  begin 
    MyPopup := GetLastActivePopup(LastFound); 
    BringWindowToTop(LastFound); 
    if IsIconic(MyPopup) 
      then ShowWindow(MyPopup, SW_RESTORE) 
      else SetForegroundWindow(MyPopup); 
    Application.Terminate; 
    Result:=true; 
  end 
end; 
 
end.