///////////////////// (一)项目文件 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;

|