I finally got around to doing something about not allowing the user to run more than one instance of the app at a time. My approach is to iterate processes until I find my exe. Then retrieve the window handle for that ProcessId. Once I have that I use a version of Craig Boyds faq184-4262 code to bring the running process's main window to the foreground and kill the new process. For the most part things work pretty well, still haven't worked out restoring minimized windows yet. But the weird thing is that many times I wind up with a 'box' on the screen that doesn't go away. It's typically in the upper left of the screen and stays on top of everything and anything. It's also whatever happened to be at that spot at the time, desktop, other window, whatever.
Here is roughly the code I'm using:
Any ideas what that box is and how I handle it?
Thanks,
Ralph Kolva
Here is roughly the code I'm using:
Code:
FUNCTION MyShowWindow
LPARAMETERS vhWindowHandle AS Integer, viFlag AS Integer
ASSERT !EMPTY(vhWindowHandle) MESSAGE "No arg passed to ProcessClass.ShowWindow"
*IF !THIS.SuppressDebugs THEN
* DEBUGOUT "ProcessClass.ShowWindow called"
*ENDIF
LOCAL lhForeGroundWindowHandle AS Number, lnForeGroundThreadID AS Number
LOCAL lnTargeWindowThreadID AS Number, llReturn AS Logical
LOCAL SW_NORMAL AS Integer, liFlag AS Integer
DECLARE Long BringWindowToTop In Win32API Long
DECLARE Long ShowWindow In Win32API Long, Long
DECLARE INTEGER GetCurrentThreadId IN kernel32
DECLARE INTEGER GetWindowThreadProcessId IN user32 ;
INTEGER hWnd, INTEGER @ lpdwProcId
DECLARE INTEGER GetCurrentThreadId IN kernel32
DECLARE INTEGER AttachThreadInput IN user32 ;
INTEGER idAttach, INTEGER idAttachTo, ;
INTEGER fAttach
DECLARE INTEGER GetForegroundWindow IN user32
&& Constants for ShowWindow
*SW_HIDE = 0
SW_NORMAL = 1
*SW_SHOWMINIMIZED = 2
*SW_SHOWMAXIMIZED = 3
*SW_SHOWNOACTIVATE = 4
*SW_SHOW = 5
*SW_MINIMIZE = 6
*SW_SHOWMINNOACTIVE = 7
*SW_SHOWNA = 8
*SW_RESTORE = 9
*SW_SHOWDEFAULT = 10
llReturn = .F.
liFlag = IIF(EMPTY(viFlag), SW_NORMAL, CAST( viFlag AS Integer) )
lhForeGroundWindowHandle = GetForegroundWindow()
lnForeGroundThreadID = GetWindowThreadProcessId( lhForeGroundWindowHandle, 0 )
lnTargeWindowThreadID = GetWindowThreadProcessId( vhWindowHandle, 0 )
IF lnForeGroundThreadID == lnTargeWindowThreadID THEN
BringWindowToTop( vhWindowHandle )
ShowWindow( vhWindowHandle, liFlag )
ELSE
AttachThreadInput( lnForeGroundThreadID, lnTargeWindowThreadID, .T. )
BringWindowToTop( vhWindowHandle )
ShowWindow( vhWindowHandle, liFlag )
AttachThreadInput( lnForeGroundThreadID, lnTargeWindowThreadID, .F. )
ENDIF
lhForeGroundWindowHandle = GetForegroundWindow()
IF lhForeGroundWindowHandle == vhWindowHandle THEN
llReturn = .T.
ENDIF
CLEAR DLLS BringWindowToTop, ShowWindow, GetCurrentThreadId, GetWindowThreadProcessId
CLEAR DLLS AttachThreadInput, GetForegroundWindow, GetCurrentThreadId
RETURN llReturn
Any ideas what that box is and how I handle it?
Thanks,
Ralph Kolva