{*******************************************************}
{
}
{ HTMLCoolEdit
}
{
}
{ Copyright (c) 1999-2000 PROFOX
}
{
}
{*******************************************************}
unit
multinst;
interface
uses Forms, Windows, Dialogs,
SysUtils;
const
MI_NO_ERROR = 0;
MI_FAIL_SUBCLASS = 1;
MI_FAIL_CREATE_MUTEX = 2;
function GetMIError:
Integer;
function InitInstance :
Boolean;
implementation
uses RegWork,
FileWork;
var
UniqueAppStr : PChar;
MessageId: Integer;
WProc: TFNWndProc = Nil;
MutHandle: THandle = 0;
MIError: Integer = 0;
function GetMIError:
Integer;
begin
Result := MIError;
end;
function NewWndProc(Handle: HWND; Msg:
Integer; wParam, lParam: Longint): Longint; StdCall;
begin
Result := 1;
if Msg = MessageID then begin
if IsIconic(Application.Handle) then
OpenIcon(Application.Handle)
else SetForegroundWindow(Application.Handle);
FileWork.LoadFileName(RegWork.RWGetParamStr1);
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_FAIL_SUBCLASS;
end;
procedure
DoFirstInstance;
begin
SubClassApplication;
MutHandle := CreateMutex(Nil, False, UniqueAppStr);
if MutHandle = 0 then
MIError := MIError or MI_FAIL_CREATE_MUTEX;
end;
procedure
BroadcastFocusMessage;
begin
Application.ShowMainForm := False;
PostMessage(HWND_BROADCAST, MessageId,0,0);
end;
function InitInstance :
Boolean;
begin
MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False,
UniqueAppStr);
if MutHandle = 0 then
begin
ShowWindow(Application.Handle, SW_ShowNormal);
Application.ShowMainForm:=True;
DoFirstInstance;
result := True;
end else begin
RegWork.RWSetParamStr1;
BroadcastFocusMessage;
result := False;
end;
end;
initialization
begin
UniqueAppStr := PChar(Application.ExeName);
MessageID := RegisterWindowMessage(UniqueAppStr);
ShowWindow(Application.Handle, SW_Hide);
Application.ShowMainForm:=FALSE;
end;
finalization
begin
if WProc <> Nil then
SetWindowLong(Application.Handle, GWL_WNDPROC,
LongInt(WProc));
end;
end.