unit UIApp;
interface
implementation
uses
Windows, SysUtils, Forms;
const
sMutex = '5EF83655-5902-48D0-AC23-BF3C3B0610F9';
sActivar = '95C30256-F47E-4E23-87AC-9B9C67C8D0C5';
var
mActivar : Cardinal;
Mutex : Cardinal;
PrevWndProc : TFarProc;
function AppWndProc(Handle: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LongInt; stdcall;
var
FgThreadId : DWORD;
AppThreadId : DWORD;
begin
if Msg = mActivar then
begin
if IsIconic(Handle) then
ShowWindow(Handle, SW_RESTORE)
else
begin
FgThreadId := GetWindowThreadProcessId(GetForegroundWindow, nil);
AppThreadId := GetWindowThreadProcessId(Handle, nil);
AttachThreadInput(AppThreadId, FgThreadId, true);
SetForegroundWindow(Handle);
AttachThreadInput(AppThreadId, FgThreadId, false);
end;
Result := 0;
end
else
Result := CallWindowProc(PrevWndProc, Handle, Msg, wParam, lParam);
end;
procedure Activar;
begin
SendMessage(HWND_BROADCAST, mActivar, 0, 0);
end;
procedure Registrar;
begin
mActivar := RegisterWindowMessage(sActivar);
Mutex := CreateMutex(nil, true, sMutex);
if GetLastError = ERROR_ALREADY_EXISTS then
begin
Mutex := 0;
abort;
end
else
begin
PrevWndProc := TFarProc(GetWindowLong(Application.Handle, GWL_WNDPROC));
SetWindowLong(Application.Handle, GWL_WNDPROC, LongWord(@AppWndProc));
end;
end;
initialization
try
Registrar;
except
Activar;
Halt;
end;
finalization
if Mutex <> 0 then ReleaseMutex(Mutex);
end.