uses
Forms,
windows,
Unit1 in 'Unit1.pas' {Fmmain},
login in 'login.pas' {Fmlogin},
fm in 'fm.pas' {Formfm};
{$R *.res}
begin
CreateMutex(nil, True, 'appone');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '汽配管理管理系统已经运行了', '错误!' ,MB_ICONERROR);
Halt;
end;
begin
Application.Initialize;
application.Title:='欢迎使用汽配管理系统';
formfm:=tformfm.Create(application);
formfm.Show;
formfm.Update;
while formfm.Timer1.Enabled do
application.ProcessMessages;
formfm.Hide;
formfm.Free;
application.Title:='用户登陆';
fmlogin:=tfmlogin.Create(application);
fmlogin.ShowModal;
if fmlogin.Edit1.Text='' then
application.Terminate
else
begin
Application.Title := '汽配管理系统';
Application.CreateForm(TFmmain, Fmmain);
Application.Run;
end;
end;
end.
这个应该比较简单。
begin
Hwnd:=FindWindow('TForm1','MP3Player'); //查找是否已有窗体MainForm
if Hwnd<>0 then
begin
SetForegroundWindow(Hwnd); //激活已運行的程序實例
Application.Terminate; //中止本次實例
end
else
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;//執行本次實例
end;
end.
procedure TMainForm.RestoreRequest(var message: TMessage);
begin
if IsIconic(Application.Handle) then //判断程序是否最小化
Application.Restore //如果是则恢复窗体
else
Application.BringToFront; //否则移至屏幕最前端
end;
uses
Forms,
windows,
SysUtils,
Dialogs,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
var
hMutex:HWND;
Error:integer;
begin
Application.Initialize;
hMutex:=CreateMutex(nil,False,'Only One Instance');
Error:=GetLastError;
if Error<>ERROR_ALREADY_EXISTS Then
begin
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
begin
MessageDlg('已经有一个程序的实例在运行!',mtInformation,[mbOK],0);
ReleaseMutex(hMutex);
end;
end.
var
MessageID:Integer;
Wproc:TFNWndProc;
MutHandle:Thandle;
Form1: TForm1;
implementation
{$R *.DFM}
//新的窗口处理过程
function NewWndProc(Handle:HWND;Msg:Integer;wParam,lParam:longint):
longint;stdcall;
begin
Result:=0;
if Msg=MessageID then
begin
//在这里您可以做其他事情,我在这里只是简单的显示一条消息
showmessage('我已经运行了,不要再运行我!');
end
else
//其他的消息则调用旧的窗口处理过程
Result:=CallWindowProc(WProc,Handle,Msg,wParam,lParam);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
BSMRecipients:DWORD;
begin
BSMRecipients:=BSM_APPLICATIONS;
//注册一个消息,并创建一个唯一的消息标识符
MessageID:=RegisterWindowMessage(UniqueAppstr);
//子类化窗口,消息由新的窗口处理程序处理
WProc:=TFNWndProc(SetWindowLong(Application.Handle,GWL_WNDPROC,Longint(@NewWndProc)));
if MutHandle=0 then //若不存在,表明第一次运行,则创建一互斥对象
MutHandle:=CreateMutex(nil,False,UniqueAppstr)
else
begin
//若存在,表明已经有实例运行,则向广播消息给所有的顶级窗口,并退出程序
Application.ShowMainForm:=False;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,@BSMRecipients,MessageID,0,0);
Application.Terminate();
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SetWindowLong(Application.Handle,GWL_WNDPROC,Longint(WProc));
CloseHandle(MutHandle);
end;
uses
Forms,
Windows,
Messages,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas';
{$R *.res}
var
hMutex : Thandle;
WaitResult : word;
BroadcastList : DWORD;
begin
MessageID := RegisterWindowMessage('Check For Choice Previous Inst');
// register a message to use later on
hMutex := createMutex(nil,false,pchar('App_Choice'));
// grab a mutex handle
WaitResult := WaitForSingleObject(hMutex,10); // wait to see if we can have exclusive use of the mutex
if ( waitResult = WAIT_TIMEOUT ) then
// if we can't then broadcast the message to make the owner of the mutex respond
{ request that the running application takes focus }
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastSystemMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0);
//32 bit - broadcast the message to all apps - only a prev inst will hear it.
end
else
begin
{ do the normal stuff}
Application.Title := 'Choice Organics Purchase & Sales System';
Application.CreateForm(TForm1, Form1);
Application.Run;
ReleaseMutex(hMutex);
// release the mutex as a politeness
end;
CloseHandle(hMutex);
// close the mutex handle
begin
Application.Initialize;
Application.Title := 'XXXX';
{hMutex:=CreateMutex(nil,False,'XXXX');
Ret:=GetLastError;
If Ret=ERROR_ALREADY_EXISTS Then
ReleaseMutex(hMutex)
else
begin
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end;
end.
var
hMutex : Thandle;
WaitResult : word;
BroadcastList : DWORD;
begin
MessageID := RegisterWindowMessage('Check For Choice Previous Inst');
// register a message to use later on
hMutex := createMutex(nil,false,pchar('App_Choice')); // grab a mutex
handle
WaitResult := WaitForSingleObject(hMutex,10); // wait to see
if we can have exclusive use of the mutex
if ( waitResult = WAIT_TIMEOUT ) then // if we can't then broadcast
the message to make the owner of the mutex respond
{ request that the running application takes focus }
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastSystemMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0); //32 bit - broadcast the
message to all apps - only a prev inst will hear it.
end
else
begin
{ do the normal stuff}
Application.Title := 'Choice Organics Purchase & Sales System';
Application.CreateForm(TMainForm, MainForm);
Application.Run;
ReleaseMutex(hMutex); // release the mutex as a politeness
end;
CloseHandle(hMutex); // close the mutex handle
end.
This goes in the MainForm
procedure Tmainform.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);
begin
{ If it's the special message then focus on this window}
if Msg.Message = MessageID then // if we get the broadcast message from an
another instance of this app that is trying to start up
begin
show;
WindowState := wsMaximized;
BringToFront;
SetFocus;
Handled := true;