5

The LA Fox Developer Newsletter
March 1998
Checking for Instantiated
Windows
by Charlie Parker

Here is a handy function that can be used in Visual FoxPro to
determine if your application is already running or if a specific
window has already been instantiated. It checks for the exist-
ence of a window with a specified caption and returns a logical
value indicating it existence or non-existence. It also optionally
wilt minimize, normalize or maximize the window if it finds it.

*
APPRUNNING This function will check to see
*
if an instance of this application is already
*
running. If so, then it will activate that
*
instance and return a .T. condition. Otherwise,
*
it will just return a .F. condition. When
*
activating the window, the second parameter
*
tells how to show the window (e.g. normalized,
*
minimized or maximized).
FUNCTION APPRUNNING (tcAppName, tnWindowSize)
*__ Parameters:
*__ 1) tcAppName:
The caption in the window to check for.
2)
tnWindowSize:
0
(or omitted) Don’t show the window,
I = Show the window Normalized,
2
= Show the window Minimized,
3
= Show the window Maximize
*__ If the second parameter is not passeu, then
assume we don’t want to show the window,
*__ we just want to know if it exists.
IF TYPE(”tnWindowSize”) <> “N”
tnWindowSize = 0
ENDIF

LOCAL lcNull, InAppHandle, IlReturnValue
IcNuII = .NULL.
IlReturnValue = .F.

*__ Define the Windows API routines.
DECLARE INTEGER FlndWindow IN W1N32API
STRING IcNulI, STRING tcAppName

DECLARE INTEGER ShowWindow IN WIN32API;
INTEGER InAppHandle, INTEGER tnWindowSize

*__ Get the handle of the specified window. This
will return zero if no such window exists.
InAppHandle = FindWindow(IcNulI, tcAppName)

IF lnAppHandle <> 0
IlReturnValue = .T.

*__ If the window size parameter is zero,
*__
then don’t show the window, just report
back that It is there.
IF tnWindowSize > 0
ShowWindow(InAppHandle, tnWindowSlze)
ENDIF
ENDIF

RETURN IlReturnValue
Checkina To See If Your Application Has Already Been
I Launched

In order for this function to work, your application must have a
main window with a unique caption. This can be a top-level form
or the FoxPro window. Since the function checks for windows
with a specific caption, then if you dynamically Change the
caption the function may not return the expected value.

To use the function, put in code similar to the following at the
beginning of your main program:

IF APPRUNNING(”My Application’s Caption”, 3)
RETURN
ENDIF


This will call the APPRUNNING function that will check to see if
there is a window with the caption “My Application’s Caption”. If
so, then it will maximize that window and return a .T. condition
causing the second instance to terminate.

Checkina For Activated Windows.

If your application restricts specific windows to one instance of
thatwindow, then the APPRUNNING function can be used for
this too. I recently developed an application that keeps track of
different development projects. The main screen is just a grid
that contains the name of the development project and the path
to where the data is for that project. The control source for the•
table called that "Projects.dbf" with two fields -
ProjName (Project Name) and Proj DBC (full path to where the
DBC is for the project).

To display the status of a project, the user first clicks on the row
in the grid containing the project, then clicks on the “Display
Project” button. This then opens the DBC (specified in the
Projects.ProjDBC field) and displays the project information
form (called ProjTrak).

The caption of the ProjTrak form is set to the project name (from
the Projects.ProjName field). Thus, each instance of the project
information form has a unique caption.

If the ProjTrak form has already been instantiated for a given
project, then I don’t want to open that DBC again and try to
instantiate another instance of that project. I used the
APPRUNNING function to check for this. The click event for the
“Display Project” button looks something like this:

IF NOT APPRUNNING(ALLTRIM(Projects.ProjName), 1)

LOCAL IcDBC
IcDBC = Projects.Pr0jDBC

OPEN DATABASE &IcDBC

DO FORM ProjTrak WITH alltrim(Projects.ProjName)

ENDIF


(Con't page 6)
Page 5

5