onceonly.pas

来自「PatientRunner 20 Source」· PAS 代码 · 共 65 行

PAS
65
字号
{ *************************************************************************** }
{                                                                             }
{ PatientRunner                                                               }
{                                                                             }
{ Copyright (c) 2002-2003 IgD Software, LLC                                   }
{                                                                             }
{ This file may be distributed and/or modified under the terms of the GNU     }
{ General Public License (GPL) version 2 as published by the Free Software    }
{ Foundation and appearing at http://www.gnu.org/licenses/gpl.html.           }
{                                                                             }
{ *************************************************************************** }

unit OnceOnly;

interface

function AlreadyRunning( const AppTitle: String ): Boolean;

implementation

uses Messages, Windows, Sysutils;

var mutex: THandle = 0;

{ *************************************************************************** }
{                                                                             }
{ function AlreadyRunning                                                     }
{                                                                             }
{ This function tests to see if another instance of the application is        }
{ already running and returns a boolean.  If another instance is detected,    }
{ the function will try to bring it to the front.                             }
{                                                                             }
{ Adapted fromPeter Below's public domain code from Borland newsgroups        }
{ *************************************************************************** }

function AlreadyRunning(const AppTitle: String): Boolean;
var wnd: HWND;
begin { AlreadyRunning }
  mutex := CreateMutex( nil, false,
                        PChar( StringReplace(AppTitle, ' ', '_',
                                            [rfReplaceAll] )));
  if mutex = 0 then
    Result := false
  else
    Result := GetLastError = ERROR_ALREADY_EXISTS;

  if Result then
  begin
    wnd:= FindWindow( 'TApplication', PChar( AppTitle ));
    if wnd <> 0 Then
    begin
      if IsIconic(wnd) then SendMessage( wnd, WM_SYSCOMMAND, SC_RESTORE, 0 );
      SetForegroundWindow( wnd );
    End; { If }
  End; { If }
End; { AlreadyRunning }

initialization

finalization
  if mutex <> 0 Then CloseHandle( mutex );

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?