Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Minimize App Started With CreateObject

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I have an app that starts another program with CreateObject. I would like to start it in minimized mode. Can this be done with CreateObject? I think I could use a Windows API to do this but not sure how. Also need to determine if the other app is already running. Can someone point me in the right direction? The program is now in VFP6 but will me moved to VFP9 soon.

Auguy
Sylvania, Ohio
 
You can always set the
objForm.Visible=.f.
objForm.WindowState=1
when you create it at design time

Change the properties to
objForm.Visible=.T.
objForm.WindowState=0
at run time when needed

As far as determining if a application is running, There are several ways. I like using the "Window Name" here are 3 examples of how to do it.
Code:
*/***************************************************************************
*# is a window open to Window Operation system
*/Program   : App_running
*/System    : FoxPro Library
*/Purpose   : Checks the Windows Operation System to see if a program is running by the Windows Name.
*/Syntax    : logical = App_Running("WindowName",[version])
*/Returns   : logical
*/Parameter : WindowName - String - All or part of the window name to look for
*/Defaults  : none
*/Requires  :
*/Changes   :
*/Calls     :
*/Version   : 3.0
*/Dated     : <<DATE>>
*/Written By: David W. Grewe
*/***************************************************************************
*&Application HAndling
*/***************************************************************************
*/ Record Of Change
*/ 1.0 Added Win32API call 
*/ 2.0 Added bring Window to top of screen
*/ 3.0 Expanded change 2 so it was nore reliable
*/***************************************************************************
parameter pcWindName, pnVersion
*******************************************************************************
IF 	PARAMETERS()<1 OR VARTYPE(pcWindName)<>"C" OR EMPTY(pcWindName)
	RETURN .f.
ENDIF 
IF 	PARAMETERS()<2 OR VARTYPE(pnVersion)<>"N" OR EMPTY(pnVersion)
	pnVersion = 1
ENDIF 
*
DO CASE 
CASE pnVersion = 1
	SET LIBRARY TO SYS(2004)+"foxtools.fll" ADDITIVE 
	* FindWindow() takes two parameters and returns the handle of the window, HWND, if it was found.
	GetWind = RegFn("FindWindow", "CC", "I")
	*Set the first parameter of the call to getwind to 0, null.
	RETURN IIF(CallFn(GetWind , 0 , pcWindName)> 0 , .t. , .f.)
*
CASE pnVersion = 2
	declare integer BringWindowToTop in Win32API integer lnWnd
	declare integer GetActiveWindow  in Win32API
	declare integer GetWindow        in Win32API integer lnWnd, integer lnType
	declare integer GetWindowText    in Win32API integer lnWnd, string @lcText, integer lnType
	lnNext = GetActiveWindow()                       && current app's window
	DO WHILE lnNext <> 0
		lcText = REPLICATE(CHR(0) , 80)
		= GetWindowText(lnNext , @lcText , 80)         && get window title
		IF	UPPER(ALLTRIM(pcWindName)) $ UPPER(lcText)
			= BringWindowToTop(lnNext)
			RETURN .t.
		ENDIF 
		lnNext = GetWindow(lnNext , 2)                 && next window
	ENDDO 
	RETURN .f.
*
CASE pnVersion = 3
	IF  Single_Instance_App_By_Mutex('pcWindName')
		QUIT 
	ENDIF 
	RETURN .f.
*
OTHERWISE
	DECLARE INTEGER FindWindow IN Win32API String @wClass, String @WinName
	RETURN IIF(FindWindow("pcWindName") > 0 , .T. , .F.)
*	
ENDCASE
*
*
*
****************************************************************
FUNCTION Single_Instance_App_By_Mutex
****************************************************************
*Descrip:        This function only allows one instance of an app to be run at a time
*Returns:        (T/F)Whether another instance of the app is running

LPARAMETERS tcMutexName
*parameter listing
*tcMutexName    -    Name of Apps assigned mutex
*API Functions
declare integer IsIconic            in win32api integer
declare integer SetForegroundWindow in win32api integer
declare integer GetForegroundWindow in win32api
declare integer SetProp             in win32api integer,string @,integer
declare integer showwindow          in win32api integer,integer

LOCAL lhAppWnd
LOCAL llQuit

*get the apps window handle
lhAppWnd = Find_App_By_Mutex(tcMutexName)
IF 	lhAppWnd <> -1
	IF 	lhAppWnd <> GetForegroundWindow()  && necessary for testing only
		llQuit=.t.
	ENDIF 
	IF 	IsIconic(lhAppWnd)>0
		showwindow(lhAppWnd,3)    && activate the window and show it
	ENDIF 
	SetForegroundWindow(lhAppWnd)
ELSE 
* this assumes our application window is currently active
  lhAppWnd=GetForegroundWindow()

* To help identify the application window we are adding
* a property to it - which we can later search for in Find_App_By_Mutex
  SetProp(lhAppWnd,@tcMutexName,1)
ENDIF  && lhAppWnd <> -1

RETURN llQuit
ENDFUNC 
*
*
*
****************************************************************
FUNCTION Find_App_By_Mutex
****************************************************************
LPARAMETERS tcMutexName
*Descrip:    This function locates the app window based on the mutex
*Returns:    The apps window handle, -1 if not found
*paramter listing
*tcMutexName    -    Name of apps assigned mutex

*API Function
declare integer GetWindow        in win32api integer,integer
declare integer GetDesktopWindow in win32api
declare integer GetProp          in win32api integer,string @

*variable declaration
LOCAL lhWnd,lhFoundWnd

lhFoundWnd = -1

IF	Set_Mutex(tcMutexName) = -2
* Mutex Already Exist - locate the original application
	lhWnd=GetDesktopWindow() && handle to the desktop
	lhWnd=GetWindow(lhWnd,5) && get the first child of the desktop

	DO WHILE lhWnd>0 AND lhFoundWnd = -1 && loop until we find the window or run out of windows
		IF	GetProp(lhWnd,@tcMutexName)==1
			lhFoundWnd = lhWnd    && set the flag
		ENDIF 
		lhWnd=GetWindow(lhWnd,2) && get handle to the next child of the desktop
	ENDDO 
ENDIF 

RETURN lhFoundWnd
ENDFUNC 
*
*
*
****************************************************************
FUNCTION Set_Mutex
****************************************************************
*Descrip:    This function attempts to set the mutex for the current app
*Returns:    -2 if mutex already exists
*                    -1 if set_mutex fails
*                     0 if successful
*
LPARAMETERS tcMutexName
*parameter listing
*tcMutexName    -    Name of apps assigned mutex
*
*API Functions
declare integer CreateMutex  in win32api integer,integer,string @
declare integer GetLastError in win32api
declare integer CloseHandle  in win32api integer
*
tcMutexName = tcMutexName+chr(0) && must be null terminated
lhMutex=CreateMutex(0,1,@tcMutexName)
*
DO CASE 
CASE lhMutex = .null.
* CreateMutex failed
  lhMutex = -1
CASE GetLastError()=183
* Mutex Already Exist
  lhMutex = -2
  CloseHandle(lhMutex) && close the new handle
OTHERWISE 
* Mutex was created
  lhMutex = 0
ENDCASE 
RETURN lhMutex
ENDFUNC


David W. Grewe Dave
 
Dave, Thanks for the great info. I thought I tried changing the windowstate and got an error. Will try it again to see if I can make it work.

Auguy
Sylvania, Ohio
 
Auguy,

Excel.application and such also have a WindowState property, but not all COM server apps. The universal method to set a window state to minimized is ShowWindow API call. Since it's a vfp reserved word, define it this way:

Code:
#define SW_HIDE             0
#define SW_SHOWNORMAL       1
#define SW_NORMAL           1
#define SW_SHOWMINIMIZED    2
#define SW_SHOWMAXIMIZED    3
#define SW_MAXIMIZE         3
#define SW_SHOWNOACTIVATE   4
#define SW_SHOW             5
#define SW_MINIMIZE         6
#define SW_SHOWMINNOACTIVE  7
#define SW_SHOWNA           8
#define SW_RESTORE          9
#define SW_SHOWDEFAULT      10
#define SW_FORCEMINIMIZE    11


DECLARE INTEGER ShowWindow IN user32 AS ShowWindowA;
        INTEGER hWindow, INTEGER nCmdShow

The SW_ constants are possible nCmdShow values, also see for a description. So try SW_SHOWMINIMIZED if the app is not visible. If it is use SW_MINIMIZE or SW_FORCEMINIMIZE. hWindow is the .hwnd property, if that is not available as property of the object you get from CreateObject() you can use the FindWindow API call to get the hwnd by the windows caption.

Bye, Olaf.
 
Thanks Olaf, I think I can get my desired results using the options you and David provided.

Auguy
Sylvania, Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top