Hi Colin

I use the following in the projects source.  Similar to what Luke & Robert
are suggesting, but includes a procedure to bring the previous running
instance of your program to the fore.

cheers Tim


program Project1;

uses
  Forms,
  Windows,
  Dialogs,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

const
  MutexName = 'Project1Mutex';

var
  Project1MutexH: THandle;

procedure RestorePrevInstance;
var
  PrevInst: HWND;
begin
  PrevInst := FindWindow('TForm1', nil);
  if PrevInst <> 0 then
  begin
    if IsIconic(PrevInst) then
      ShowWindow(PrevInst, SW_RESTORE)
    else if IsIconic(GetWindow(PrevInst, GW_OWNER)) then
      ShowWindow(GetWindow(PrevInst, GW_OWNER), SW_RESTORE)
    else
      SetForegroundWindow(PrevInst);
  end;
  Application.Terminate;
end;

begin
  Project1MutexH := CreateMutex(nil, True, MutexName);
  if Project1MutexH = 0 then
  begin
    ShowMessage('Initialization Error');
    Halt;
  end;
  if GetLastError = ERROR_ALREADY_EXISTS then
  begin
    Application.ShowMainForm := False;
    RestorePrevInstance;
  end;

  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;

  CloseHandle(Project1MutexH);
end.



---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to