Ver Mensaje Individual
  #3  
Antiguo 21-01-2009
Avatar de The_Duke
The_Duke The_Duke is offline
Miembro
 
Registrado: abr 2006
Ubicación: Republica Dominicana
Posts: 48
Reputación: 0
The_Duke Va por buen camino
Gracias por la ayuda Dec, e modificado el codigo para lo que necesito.
y aqui lo dejo para os que os puedan necesitar, y si hacen alguna mejora, ya que encuentro que es ago ento no mucho pero se nota.

Código Delphi [-]
unit UIApp;

interface

implementation

uses
  Windows, SysUtils, Forms, Dialogs, Messages, Unit1;

const
  { Cadenas para registrar el mutex y el mensaje }
  sMutex   = '10D73234-C9F7-4C2D-BC7E-39B5820AF456';
  sActivar = '3F154732-CCDE-4BC7-9439-AFCD3BCFA84D';

var
  mActivar    : Cardinal; { Mensaje para activar la instancia anterior }
  Mutex       : Cardinal; { Mutex                                      }
  PrevWndProc : TFarProc; { Procedimiento de ventana original          }


function AppWndProc(Handle: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LongInt; stdcall;
var
  FgThreadId  : DWORD; { Hilo de la app. que tenga el foco }
  AppThreadId : DWORD; { Hilo de nuestra aplicación        }

begin

  if Msg = mActivar then
  begin

    Form1.close;

  end
  else
    { Dejar que el procedimiento original se encargue de los otros mensajes }
    Result := CallWindowProc(PrevWndProc, Handle, Msg, wParam, lParam);


end;

procedure Activar;
begin
  { Mandamos el mensaje a todas las ventanas }

   SendMessage(HWND_BROADCAST, mActivar, 0, 0);
  end;

procedure Registrar;
begin
  mActivar := RegisterWindowMessage(sActivar);
  Mutex    := CreateMutex(nil, true, sMutex);

  { Si ya existe el mutex lanzamos una excepción silenciosa }
  if GetLastError = ERROR_ALREADY_EXISTS then
  begin
        Activar;
        PrevWndProc := TFarProc(GetWindowLong(Application.Handle, GWL_WNDPROC));
  SetWindowLong(Application.Handle, GWL_WNDPROC, LongWord(@AppWndProc));
  end
  else
  begin
    { Sustituimos el procedimiento de ventana }
    PrevWndProc := TFarProc(GetWindowLong(Application.Handle, GWL_WNDPROC));
    SetWindowLong(Application.Handle, GWL_WNDPROC, LongWord(@AppWndProc));
  end;
end;

initialization
  try
    Registrar;
  except
    Halt;
  end;

finalization
  if Mutex <> 0 then ReleaseMutex(Mutex);
end.

Última edición por The_Duke fecha: 22-01-2009 a las 00:23:28.
Responder Con Cita