var main_handle:Hwnd; begin main_handle:=winprocs.findwindow(nil,'xxxx管理系统'); if main_handle>0 then begin showmessage('xxxx管理系统已经打开,请检查!'); //target:find the active form ways exit; end;
unit MultInst;interface const MI_QUERYWINDOWHANDLE=1; MI_RESPONDWINDOWHANDLE=2; MI_ERROR_NONE=0; MI_ERROR_FAILSUBCLASS=1; MI_ERROR_CREATINGMUTEX=2; function GetMiError:Integer;implementationuses Forms,Windows,SysUtils;const UniqueAppStr='DDG.MyProgram';var MessageId:Integer; WProc:TFNWndProc; MutHandle:THandle; MIError:Integer;function GetMIError:Integer; begin Result:=MIError; end;function NewWndProc(Handle:HWND;Msg:Integer;wParam,lParam:Longint):Longint;stdCall; begin Result:=0; if Msg=MessageID then begin case wParam of MI_QUERYWINDOWHANDLE: begin if IsIconic(Application.Handle) then begin Application.MainForm.WindowState:=wsNormal; Application.Restore; end; PostMessage(HWND(lParam),MessageID,MI_RESPONDWINDOWHANDLE,Application.MainForm.Handle); end; MI_RESPONDWINDOWHANDLE: begin SetForegroundWindow(HWND(lParam)); Application.Terminate; end; end; end else Result:=CallWindowProc(WProc,Handle,Msg,wParam,lParam); end;procedure SubClassApplication; begin WProc:=TFNWndProc(SetWindowLong(Application.Handle,GWL_WNDPROC,Longint(@NewWndProc))); if WProc=nil then MIError:=MIError or MI_ERROR_FAILSUBCLASS; end;procedure DoFirstInstance; begin MutHandle:=CreateMutex(nil,False,UniqueAppStr); if MutHandle=0 then MIError:=MIError or MI_ERROR_CREATINGMUTEX; end;procedure BroadcastFocusMessage; var BSMRecipients:DWORD; begin Application.ShowMainForm:=false; BSMRecipients:=BSM_APPLICATIONS; BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @BSMRecipients,MessageID,MI_QUERYWINDOWHANDLE,Application.Handle); end;procedure InitInstance; begin SubClassApplication; MutHandle:=OpenMutex(MUTEX_ALL_ACCESS,False,UniqueAppStr); if MutHandle=0 then begin DoFirstInstance; end else BroadcastFocusMessage; end;initialization MessageID:=RegisterWindowMessage(UniqueAppStr); InitInstance;finalization if WProc<>Nil then SetWindowLong(Application.Handle,GWL_WNDPROC,LongInt(WProc)); if MutHandle<>0 then CloseHandle(MutHandle); end.把这个单元加到你工程里就行了
用findprocess做检查是否存在该进程
这个是好问题了,搜一下以前的问题应该能找到不少 用Windows的互斥对象类似这样{$R *.res}var Handle: THandle;begin Handle := CreateMutex(nil, false, 'My Program'); if GetLastError <> ERROR_ALREADY_EXISTS then begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; ReleaseMutex(Handle); end else begin Handle := FindWindow(nil, 'Form1'); if Handle <> 0 then begin SetForegroundWindow(Handle); SetActiveWindow(Handle); end; end; end;这段代码放在项目文件.dpr中
begin
main_handle:=winprocs.findwindow(nil,'xxxx管理系统');
if main_handle>0 then
begin
showmessage('xxxx管理系统已经打开,请检查!'); //target:find the active form ways exit;
end;
顺便说一下使用findwindow是有缺陷的,至于什么缺陷,该篇文章也说的很清楚
const
MI_QUERYWINDOWHANDLE=1;
MI_RESPONDWINDOWHANDLE=2; MI_ERROR_NONE=0;
MI_ERROR_FAILSUBCLASS=1;
MI_ERROR_CREATINGMUTEX=2; function GetMiError:Integer;implementationuses Forms,Windows,SysUtils;const
UniqueAppStr='DDG.MyProgram';var
MessageId:Integer;
WProc:TFNWndProc;
MutHandle:THandle;
MIError:Integer;function GetMIError:Integer;
begin
Result:=MIError;
end;function NewWndProc(Handle:HWND;Msg:Integer;wParam,lParam:Longint):Longint;stdCall;
begin
Result:=0;
if Msg=MessageID then
begin
case wParam of
MI_QUERYWINDOWHANDLE:
begin
if IsIconic(Application.Handle) then
begin
Application.MainForm.WindowState:=wsNormal;
Application.Restore;
end;
PostMessage(HWND(lParam),MessageID,MI_RESPONDWINDOWHANDLE,Application.MainForm.Handle);
end;
MI_RESPONDWINDOWHANDLE:
begin
SetForegroundWindow(HWND(lParam));
Application.Terminate;
end;
end;
end
else
Result:=CallWindowProc(WProc,Handle,Msg,wParam,lParam);
end;procedure SubClassApplication;
begin
WProc:=TFNWndProc(SetWindowLong(Application.Handle,GWL_WNDPROC,Longint(@NewWndProc)));
if WProc=nil then
MIError:=MIError or MI_ERROR_FAILSUBCLASS;
end;procedure DoFirstInstance;
begin
MutHandle:=CreateMutex(nil,False,UniqueAppStr);
if MutHandle=0 then
MIError:=MIError or MI_ERROR_CREATINGMUTEX;
end;procedure BroadcastFocusMessage;
var
BSMRecipients:DWORD;
begin
Application.ShowMainForm:=false;
BSMRecipients:=BSM_APPLICATIONS;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
@BSMRecipients,MessageID,MI_QUERYWINDOWHANDLE,Application.Handle);
end;procedure InitInstance;
begin
SubClassApplication;
MutHandle:=OpenMutex(MUTEX_ALL_ACCESS,False,UniqueAppStr);
if MutHandle=0 then
begin
DoFirstInstance;
end
else
BroadcastFocusMessage;
end;initialization
MessageID:=RegisterWindowMessage(UniqueAppStr);
InitInstance;finalization
if WProc<>Nil then
SetWindowLong(Application.Handle,GWL_WNDPROC,LongInt(WProc));
if MutHandle<>0 then CloseHandle(MutHandle);
end.把这个单元加到你工程里就行了
用Windows的互斥对象类似这样{$R *.res}var
Handle: THandle;begin
Handle := CreateMutex(nil, false, 'My Program');
if GetLastError <> ERROR_ALREADY_EXISTS then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
ReleaseMutex(Handle);
end
else
begin
Handle := FindWindow(nil, 'Form1');
if Handle <> 0 then
begin
SetForegroundWindow(Handle);
SetActiveWindow(Handle);
end;
end;
end;这段代码放在项目文件.dpr中