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. 呵呵,给分吧
Application.? 忘了.
在工程文件(dpr文件)中加入:
Var
prvHandle: THandle;
ds: TCopyDataStruct;
hd: THandle;
Begin
prvHandle := CreateMutex(Nil, false, 'form1');
If GetLastError = ERROR_ALREADY_EXISTS Then
Begin
//传送消息
ds.cbData := 10;
GetMem(ds.lpData, ds.cbData); //为传递的数据区分配内存
StrCopy(ds.lpData, 'Show');
Hd := FindWindow(Nil, Version); // 获得接受窗口的句柄
If Hd <> 0 Then
SendMessage(Hd, WM_COPYDATA, Application.Handle, Cardinal(@ds)); // 发送WM_COPYDATA消息
// else
// ShowMessage('目标窗口没找到!');
System.FreeMem(ds.lpData); //释放资源
CloseHandle(prvHandle);
Application.Terminate;
Exit;
End;
Application.Initialize;
Application.Title := 'form1';
Application.CreateForm(TFrm_Main, Frm_Main);
Application.Run;
End.在主窗体中加入:
private
{ Private declarations }
Procedure Mymessage(Var t: TWmCopyData); message WM_COPYDATA;
Procedure TFrm_Main.Mymessage(Var t: TWmCopyData);
Begin
If StrPas(t.CopyDataStruct.lpData) = 'Show' Then
Begin
Self.show;
End;End;
你的方法好像是可以(从流程方法上),可是有时候可以有时候不可以!?
而且是调试时可以,独立测试时不可以!?
我哪里理解不够吗?
请指教!
prvHandle := CreateMutex(Nil, false, 'form1');
Application.Title := 'form1';还有Mymessage中的'Show'与发送时的" StrCopy(ds.lpData, 'Show');"是否一致。如果还不行,你可以把程序贴出来,我看一下
Hd := FindWindow(Nil, Version); // 获得接受窗口的句柄
这句话的意义是什么?Version?是个常量!?
我调试时把Version改成'Project1',要不Hd=0!
xzhifei(飞) 怎么和你联系?我的QQ是1597820,谢谢!
大伙再给点新思路!学习学习:)
Version为要找的窗体Caption
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.
呵呵,给分吧
Thanks!
:)
初测成功!
有时间讲解一下好吗?
QQ联系,方便吗?
我的:1597820