2006. május 26., péntek

How to activate the previous instance of an application


Problem/Question/Abstract:

How to activate the previous instance of an application

Answer:

Place the following at the start of your project file:

begin
  if HPrevInst < > 0 then
  begin
    ActivatePreviousInstance;
    Exit;
  end;
  { ... }

and include the following unit:

unit PrevInst;

interface

uses
  WinProcs, WinTypes, SysUtils;

type
  PHWnd = ^HWnd;

function EnumFunc(Wnd: HWnd; TargetWindow: PHWnd): Bool; export;
procedure ActivatePreviousInstance;

implementation

function EnumFunc(Wnd: HWnd; TargetWindow: PHWnd): Bool;
var
  ClassName: array[0..30] of char;
begin
  Result := True;
  if GetWindowWord(Wnd, GWW_HINSTANCE) = HPrevInst then
  begin
    GetClassName(Wnd, ClassName, 30);
    if StrIComp(ClassName, 'TApplication') = 0 then
    begin
      TargetWindow^ := Wnd;
      Result := False;
    end;
  end;
end;

procedure ActivatePreviousInstance;
var
  PrevInstWnd: HWnd;
begin
  PrevInstWnd := 0;
  EnumWindows(@EnumFunc, Longint(@PrevInstWnd));
  if PrevInstWnd <> 0 then
    if IsIconic(PrevInstWnd) then
      ShowWindow(PrevInstWnd, SW_RESTORE)
    else
      BringWindowToTop(PrevInstWnd);
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése