Business Directory
Trade Forum
Vision 2000
Delphi
Domain Rates
Search
Music
Opinion Award
About
FeedBack
Quit


Delphi Tips & Tricks

| Tips & Tricks | Bug List | Cool Delphi Sites |
| Connectivity | Delphi 2.0 Features | Internet |

Detect the previous instance of an applcation

unit PrevInst;

interface

uses 
    WinTypes, WinProcs, SysUtils;

type
    PHWND = ^HWND;
    function EnumFunc(Wnd:HWND; TargetWindow:PHWND): bool; export;
    procedure GotoPreviousInstance;

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 GotoPreviousInstance;
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.

And then make the main block of your *.DPR file look something like this--

if hPrevInst <> 0 then
    GotoPreviousInstance
else
begin
    Application.CreateForm(MyForm, MyForm);
    Application.Run;
end;

| Borland Delphi | About the Authors | Home |

For Queries Mail To Webmaster

Copyright © 1996 Asylum Software Pvt. Ltd. This is an ASPL production.
Produced in conjunction with ASPL DELPHI TEAM.
Last revised November 27, 1996.