一个应用程序多次点击时,如何只让它只运行一个

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

///////////////////// (一)项目文件 test.dpr //////////////////////

program SerialGet;

uses

Forms,

UMain in 'UMain.pas' {frmMain},

ULogin in 'UForm2.pas' {Form2},

UDataModule in 'UDataModule.pas' {DataModule1: TDataModule},

{$R *.res}

begin

Application.Initialize;

if CreateMutex then //创建句柄,判断此应用程序是否在运行

begin

Application.CreateForm(TfrmMain, frmMain);

Application.CreateForm(Tform2, form2);

Application.CreateForm(TDataModule1, DataModule1);

Application.Run;

end else

begin

DestroyMutex; //释放句柄

end;

end.

//////////////// (二)登陆窗体 UMain.pas UMain.dfm //////////////////

unit UMain;

interface

uses ......

type

... ... ...

private

public

end;

var

frmMain: TfrmMain;

function CreateMutex: Boolean; // 全项目公用函数

procedure DestroyMutex; // 全项目公用函数

implementation

uses UDataModule; //引用数据模块

var Mutex: hWnd;

{$R *.dfm}

procedure DestroyMutex;

begin

if Mutex <> 0 then CloseHandle(Mutex);

end;

function CreateMutex: Boolean;

var

PrevInstHandle: THandle;

AppTitle: PChar;

begin

AppTitle := StrAlloc(100);

StrPCopy(AppTitle, Application.Title);

Result := True;

Mutex := Windows.CreateMutex(nil, False, AppTitle);

if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin

Result := False;

SetWindowText(Application.Handle, '');

PrevInstHandle := FindWindow(nil, AppTitle);

if PrevInstHandle <> 0 then begin

if IsIconic(PrevInstHandle) then

ShowWindow(PrevInstHandle, SW_RESTORE)

else

BringWindowToTop(PrevInstHandle);

SetForegroundWindow(PrevInstHandle);

end;

if Mutex <> 0 then Mutex := 0;

end;

StrDispose(AppTitle);

end;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航