Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Mustapha Bihmouten
  Where is Mustapha Bihmouten?
 Casablanca
 Morocco
 Mustapha Bihmouten
 To: Ayaz Ahmad
  Where is Ayaz Ahmad?
 Peshawar
 Pakistan
 Ayaz Ahmad
 Tags
Subject: RE: do form
Thread ID: 248903 Message ID: 248943 # Views: 1 # Ratings: 0
Version: Visual FoxPro 9 Category: General VFP Topics
Date: Friday, December 25, 2009 12:22:56 PM         
   


> hello.
> i want to run form only one time without windowstype = 1 or do form likend


Hello Ayaz

Here is the code, a prg file, i use to check a form
Call it from the load event of a form :

If CheckIfLoaded( Thisform.Name)
	*--- if form active : exit now
	Return .F.
Endif

*------------------------------------------
* CheckIfloaded : test if a form is active
*------------------------------------------
Lparameters tcFenetre As String

Local liNumInstance As Integer, lcWindName As String, llRetVal As Boolean

*--- liNumInstance contains the number of instances of the form.
m.liNumInstance = 0
*--- lcWindName contains the name of the form you're looking at.
*--- wchild() : "" must be a NULL or empty string; 0 to search the first instance
m.lcWindName = Wchild("",0)

*--- Loop through all open forms, and see if we have any with the same name as the one you want to open.
*--- (It exists in INIT() but is not yet * available.)
Do While Not Empty( m.lcWindName)

    *--- check if exist a window with this name
    m.liNumInstance = m.liNumInstance + Iif( m.lcWindName = Upper( m.tcFenetre), 1, 0)

    *--- exit from the loop if one instance exist
    If m.liNumInstance > 1
        m.llRetVal = .T.
        Exit
    Else
        m.llRetVal = .F.
        *--- Wchild() use param "1" to search the second instance
        m.lcWindName = Wchild( "", 1)
    Endif
Enddo

*--- free memory
Release lcWindName, liNumInstance

*--- "llRetval" returns .T. from INIT() if the form is already visible.
*--- Returning .T. from INIT() causes the object to not be created.
Return m.llRetVal


Another code to prevent the use of the EXE

From the foxite example section, a code provided by Eric Den Doop (do you know him ?)
it use win32api call and work well
Notes : It prevent the use of the EXE, not the form

*-------------------------------------------------------------------------
* The Foxite Visual FoxPro FAQ's, Help,Tips and Good Practice Notes
*-------------------------------------------------------------------------
* Aspect: EXE's & APPS  Option: Controlling access  Ref No: 301015
*
* Prevent exe from starting twice
*
* Question:	"If we have a desktop shortcut to start a compiled VFP exe,
*			how can the user be prevented from starting it again,
*			if by mistake they double click again on the icon? "
*
* Answer: 	"The Win32API provides us with some interesting functions
*			that can take care of such actions.
*			Use this code in the main prg file as one of the first lines."
*-------------------------------------------------------------------------
If Not AppMutex()
	Quit
Endif

*----------------
Function AppMutex
Declare Integer CreateMutex In WIN32API Integer, Integer, String @
Declare Integer CloseHandle In WIN32API Integer
Declare Integer GetLastError In WIN32API
Declare Integer SetProp In WIN32API Integer, String @, Integer
Declare Integer GetProp In WIN32API Integer, String @
Declare Integer RemoveProp In WIN32API As RetireProp Integer, String @
Declare Integer IsIconic In WIN32API Integer
Declare Integer SetForegroundWindow In WIN32API Integer
Declare Integer GetWindow In WIN32API Integer, Integer
Declare Integer ShowWindow In WIN32API As AffWindow Integer, Integer
Declare Integer GetDesktopWindow In WIN32API
Declare Long FindWindow In WIN32API Long, String

#Define SW_RESTORE 				9
#Define ERROR_ALREADY_EXISTS 	183
#Define GW_HWNDNEXT 			2
#Define GW_CHILD 				5

Local llRetVal As boolean, lcExeFlag As String, lnExeHwnd As Integer, lnHwnd As Integer

m.lcExeFlag = Strtran( _Screen.Caption, " ", "") + Chr(0)
m.lnExeHwnd = CreateMutex( 0, 1, @lcExeFlag)

If GetLastError() = ERROR_ALREADY_EXISTS

	m.lnHwnd = GetWindow( GetDesktopWindow(), GW_CHILD)

	Do While m.lnHwnd > 0
		If GetProp( m.lnHwnd, @lcExeFlag) = 1
			If IsIconic( m.lnHwnd) > 0
				AffWindow( m.lnHwnd, SW_RESTORE)
			Endif
			SetForegroundWindow( lnHwnd)
			Exit
		Endif
		lnHwnd = GetWindow( lnHwnd, GW_HWNDNEXT)
	Enddo
	CloseHandle( m.lnExeHwnd)
	m.llRetVal = .F.
Else
	SetProp( FindWindow( 0, _Screen.Caption), @lcExeFlag, 1)
	m.llRetVal = .T.
Endif

#Undef SW_RESTORE
#Undef ERROR_ALREADY_EXISTS
#Undef GW_HWNDNEXT
#Undef GW_CHILD

*--- free memory
Clear Dlls "CreateMutex"
Clear Dlls "CloseHandle"
Clear Dlls "GetLastError"
Clear Dlls "SetProp"
Clear Dlls "GetProp"
Clear Dlls "RetireProp"
Clear Dlls "IsIconic"
Clear Dlls "SetForegroundWindow"
Clear Dlls "GetWindow"
Clear Dlls "AffWindow"
Clear Dlls "GetDesktopWindow"
Clear Dlls "FindWindow"

Return m.llRetVal
Endfunc


Regards
Mustapha Bihmouten

ENTIRE THREAD

do form Posted by ayaz ahmad @ 12/24/2009 3:07:51 PM
RE: do form Posted by ho me @ 12/24/2009 3:41:23 PM
RE: do form Posted by ayaz ahmad @ 12/24/2009 5:21:51 PM
RE: do form Posted by ho me @ 12/24/2009 7:49:43 PM
RE: do form Posted by M. Akram Bhatti @ 12/24/2009 6:25:24 PM
RE: do form Posted by Mustapha Bihmouten @ 12/25/2009 12:22:56 PM