unit MultInst;interfaceconst MI_QUERYWINDOWHANDLE = 1; MI_RESPONDWINDOWHANDLE = 2; MI_ERROR_NONE = 0; MI_ERROR_FAILSUBCLASS = 1; MI_ERROR_CREATINGMUTEX = 2;// Call this function to determine if error occurred in startup. // Value will be one or more of the MI_ERROR_* error flags. function GetMIError: Integer;implementationuses Forms, Windows, SysUtils;const UniqueAppStr = 'DDG.I_am_the_Eggman!';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 this is the registered message... if Msg = MessageID then begin case wParam of MI_QUERYWINDOWHANDLE: // A new instance is asking for main window handle in order // to focus the main window, so normalize app and send back // message with main window handle. 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: // The running instance has returned its main window handle, // so we need to focus it and go away. begin SetForegroundWindow(HWND(lParam)); Application.Terminate; end; end; end // Otherwise, pass message on to old window proc else Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam); end;procedure SubClassApplication; begin // We subclass Application window procedure so that // Application.OnMessage remains available for user. WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(@NewWndProc))); // Set appropriate error flag if error condition occurred if WProc = nil then MIError := MIError or MI_ERROR_FAILSUBCLASS; end;procedure DoFirstInstance; // This is called only for the first instance of the application begin // Create the mutex with the (hopefully) unique string MutHandle := CreateMutex(nil, False, UniqueAppStr); if MutHandle = 0 then MIError := MIError or MI_ERROR_CREATINGMUTEX; end;procedure BroadcastFocusMessage; // This is called when there is already an instance running. var BSMRecipients: DWORD; begin // Prevent main form from flashing Application.ShowMainForm := False; // Post message to try to establish a dialogue with previous instance BSMRecipients := BSM_APPLICATIONS; BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE, Application.Handle); end;procedure InitInstance; begin SubClassApplication; // hook application message loop MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr); if MutHandle = 0 then // Mutex object has not yet been created, meaning that no previous // instance has been created. DoFirstInstance else BroadcastFocusMessage; end;initialization MessageID := RegisterWindowMessage(UniqueAppStr); InitInstance; finalization // Restore old application window procedure if WProc <> Nil then SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc)); if MutHandle <> 0 then CloseHandle(MutHandle); // Free mutex end. 你是否感到好复杂?——放心,这不是我这菜鸟编的,这是Delphi5开发人员指南中的例子,详细请看该书的397页。
我有几个检测当前程序是否已经运行的函数 不用发送消息的 function SystemInit:boolean; var WndHandle:THandle; //MainFormName: String; begin //MainFormName := Application.MainForm.ClassName; Result := True; WndHandle := FindWindow(PChar('TMianForm'),SystemConst_AppExplain); if WndHandle <> 0 then begin //如果系统已启动则激活 SetForegroundWindow(wndHandle); result := False; end; end;function SystemInit(aAppName: String; aTemp: Integer = 0):Boolean; Var hMutex:HWND; Ret:Integer; begin hMutex:=CreateMutex(nil,False,PChar(aAppname)); Ret:=GetLastError; If Ret<>ERROR_ALREADY_EXISTS Then Result := True Else Application.MessageBox('你的程序已经在运行了!','注意',MB_OK); ReleaseMutex(hMutex); End;function SystemInit(S: string; aTemp: String = ''): Boolean; var aHandle: THANDLE; p: array[0..79] of Char; begin Result := True; StrPCopy(P, S); aHandle := FindWindow(Pchar('TApplication'), P); if (aHandle <> 0) then begin // 已经启动过应用程序 激活先前实例! SetForegroundWindow(aHandle); SetActiveWindow(aHandle); if IsIconic(aHandle) then ShowWindow(aHandle, SW_RESTORE); Result := False; end; End;
MI_QUERYWINDOWHANDLE = 1;
MI_RESPONDWINDOWHANDLE = 2; MI_ERROR_NONE = 0;
MI_ERROR_FAILSUBCLASS = 1;
MI_ERROR_CREATINGMUTEX = 2;// Call this function to determine if error occurred in startup.
// Value will be one or more of the MI_ERROR_* error flags.
function GetMIError: Integer;implementationuses Forms, Windows, SysUtils;const
UniqueAppStr = 'DDG.I_am_the_Eggman!';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 this is the registered message...
if Msg = MessageID then
begin
case wParam of
MI_QUERYWINDOWHANDLE:
// A new instance is asking for main window handle in order
// to focus the main window, so normalize app and send back
// message with main window handle.
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:
// The running instance has returned its main window handle,
// so we need to focus it and go away.
begin
SetForegroundWindow(HWND(lParam));
Application.Terminate;
end;
end;
end
// Otherwise, pass message on to old window proc
else
Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
end;procedure SubClassApplication;
begin
// We subclass Application window procedure so that
// Application.OnMessage remains available for user.
WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
Longint(@NewWndProc)));
// Set appropriate error flag if error condition occurred
if WProc = nil then
MIError := MIError or MI_ERROR_FAILSUBCLASS;
end;procedure DoFirstInstance;
// This is called only for the first instance of the application
begin
// Create the mutex with the (hopefully) unique string
MutHandle := CreateMutex(nil, False, UniqueAppStr);
if MutHandle = 0 then
MIError := MIError or MI_ERROR_CREATINGMUTEX;
end;procedure BroadcastFocusMessage;
// This is called when there is already an instance running.
var
BSMRecipients: DWORD;
begin
// Prevent main form from flashing
Application.ShowMainForm := False;
// Post message to try to establish a dialogue with previous instance
BSMRecipients := BSM_APPLICATIONS;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
@BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE,
Application.Handle);
end;procedure InitInstance;
begin
SubClassApplication; // hook application message loop
MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
if MutHandle = 0 then
// Mutex object has not yet been created, meaning that no previous
// instance has been created.
DoFirstInstance
else
BroadcastFocusMessage;
end;initialization
MessageID := RegisterWindowMessage(UniqueAppStr);
InitInstance;
finalization
// Restore old application window procedure
if WProc <> Nil then
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
if MutHandle <> 0 then CloseHandle(MutHandle); // Free mutex
end.
你是否感到好复杂?——放心,这不是我这菜鸟编的,这是Delphi5开发人员指南中的例子,详细请看该书的397页。
如果你的目的是查找某个程序是否在运行,只要简单的用findwindow就可以了
如果你要通过别的应用程序的Message和进行什么操作,如翻译软件等,可以使用Hook技术,在Csdn有很多关于Hook的话题。
如果你要查看的应用程序是你自己的,你可以自定义一个Message,在程序中自己检测一下就可以了。
如果你的目的是查找某个程序是否在运行,只要简单的用findwindow就可以了
如果你要通过别的应用程序的Message和进行什么操作,如翻译软件等,可以使用Hook技术,在Csdn有很多关于Hook的话题。
如果你要查看的应用程序是你自己的,你可以自定义一个Message,在程序中自己检测一下就可以了。
如果你的目的是查找某个程序是否在运行,只要简单的用findwindow就可以了
如果你要通过别的应用程序的Message和进行什么操作,如翻译软件等,可以使用Hook技术,在Csdn有很多关于Hook的话题。
如果你要查看的应用程序是你自己的,你可以自定义一个Message,在程序中自己检测一下就可以了。
不用发送消息的
function SystemInit:boolean;
var
WndHandle:THandle;
//MainFormName: String;
begin
//MainFormName := Application.MainForm.ClassName;
Result := True;
WndHandle := FindWindow(PChar('TMianForm'),SystemConst_AppExplain);
if WndHandle <> 0 then
begin
//如果系统已启动则激活
SetForegroundWindow(wndHandle);
result := False;
end;
end;function SystemInit(aAppName: String; aTemp: Integer = 0):Boolean;
Var
hMutex:HWND;
Ret:Integer;
begin
hMutex:=CreateMutex(nil,False,PChar(aAppname));
Ret:=GetLastError;
If Ret<>ERROR_ALREADY_EXISTS Then
Result := True
Else
Application.MessageBox('你的程序已经在运行了!','注意',MB_OK);
ReleaseMutex(hMutex);
End;function SystemInit(S: string; aTemp: String = ''): Boolean;
var
aHandle: THANDLE;
p: array[0..79] of Char;
begin
Result := True;
StrPCopy(P, S);
aHandle := FindWindow(Pchar('TApplication'), P);
if (aHandle <> 0) then
begin // 已经启动过应用程序 激活先前实例!
SetForegroundWindow(aHandle);
SetActiveWindow(aHandle);
if IsIconic(aHandle) then
ShowWindow(aHandle, SW_RESTORE);
Result := False;
end;
End;
谢谢大家支持