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 MainFormprocedure 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; end; end;//And this goes in the TMainForm.FormCreate ;-Application.OnMessage:= OnAppMessage;
if findwindow('主窗口的名称',nil)>0 then 程序已运行 sele 程序未运行;你试试行不行,我只在本程序中用过!
主窗口的名称是什么东西? 是name 还是caption?
就是你的form1.caption 注意一个字也不要写错
在工程文件里面: var //... Mutex:THandle; begin Mutex:=CreateMutex(NIL,True,'HIS'); if GetLastError<>ERROR_ALREADY_EXISTS then //如果不存在另一实例 begin Application.Initialize; Application.CreateForm(TMain, Main); //... //... end; ReleaseMutex(Mutex);
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 MainFormprocedure 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; end;
end;//And this goes in the TMainForm.FormCreate ;-Application.OnMessage:= OnAppMessage;
sele 程序未运行;你试试行不行,我只在本程序中用过!
是name 还是caption?
注意一个字也不要写错
var
//...
Mutex:THandle;
begin
Mutex:=CreateMutex(NIL,True,'HIS');
if GetLastError<>ERROR_ALREADY_EXISTS then //如果不存在另一实例
begin
Application.Initialize;
Application.CreateForm(TMain, Main);
//...
//...
end;
ReleaseMutex(Mutex);