将这段代码--unit文件<br>加到你的程序中去,OK<br>unit MultInst;<br><br>interface<br><br>uses Forms,windows,Dialogs,SysUtils;<br><br>{function BroadcastSystemmessage(Flags: DWORD;recipients
DWORD;uiMessage: Unit;wParam:<br>WParam;lParam:LParam):Longint;stdcall;external'user32.dll';<br> }<br>const<br> MI_NO_ERROR =0;<br> MI_FAIL_SUBCLASS =1;<br> MI_FAIL_CREATE_MUTEX =2;<br><br>function GetMiError: integer;<br><br>implementation<br>const UniqueAppStr: Pchar='I am the Eggman!';<br>var<br> messageId: integer;<br> WProc:TFNWndProc = nil;<br> MutHandle: THandle = 0;<br> MIError: integer = 0;<br><br>function GetMIError: integer;<br>begin<br> Result := MIError;<br>end;<br><br>function NewWndProc(Handle:HWND;MSG: Integer;WParam,LParam: LongInt):longint;stdcall;<br>begin<br> {如果是注册的消息}<br> if Msg = MessageID then<br> begin<br> {如果主窗口已最小化,就恢复成原先的大小}<br> if IsIconic(application.handle) then<br> begin<br> application.MainForm.WindowState := wsNormal;<br> application.Restore;<br> end;<br> SetForegroundWindow(application.MainForm.Handle);<br> end<br> else<br> Result :=CallWindowProc(WProc,Handle,Msg,Wparam,LParam);<br>end;<br><br>procedure SubClassApplication;<br>begin<br> {替换应用对象的窗口程序以使applicationONMessage对用户仍然可用}<br> WProc:=TFNWndProc(SetWindowLong(application.handle,GWL_WNDPROC,<br> longint(@NewWndProc)));<br> if WProc = nil then<br> MIError := MIError or MI_FAIL_SUBCLASS;<br>end;<br><br>procedure DOFirstInstance;<br>begin<br> SubClassApplication;<br> MutHandle := CreateMutex(Nil,false,UniqueAppStr);<br> if MutHandle = 0 then<br> MIError := MIERROR or MI_FAIL_CREATE_MUTEX;<br>end;<br><br>procedure BroadcastFocusMessage;<br>{如果已经有实例存在,就会调用这个过程}<br>var<br> BSMRecipients
WORD;<br>begin<br> {暂时隐去主form}<br> Application.ShowMainForm := False;<br> {广播一个消息通知其他实例激活自己}<br> BSMRecipients := BSM_Applications;<br> BroadCastSystemMEssage(BSF_IGNORECURRENTTASk OR BSF_POSTMESSAGE,@BSMRecipients,MEssageID,0,0);<br> application.Terminate;<br>end;<br><br>procedure InitInstance;<br>begin<br> MutHandle := OpenMutex(MUTEX_ALL_ACCESS,False,UniqueAppStr);<br> if MutHandle = 0 then<br> {互斥对象还没有创建,说明没有实例存在}<br> DoFirstInstance<br> else<br> BroadcastFocusMEssage;<br>end;<br><br>initialization<br> MessageID := RegisterWindowMessage(UniqueAppStr);<br> InitInstance;<br>finalization<br> if WProc <> nil then<br> {恢复原来的窗口过程}<br> SetWindowLong(Application.handle,GWL_WNDPROC,LongInt(WProc));<br>end.