IF IsExeRunning(cFileName(HB_ARGV(0)))
MsgAlert("Your application is already running !","Attention")
Quit
ENDIF
// Author: "hua" <tomyam At rocketmail Dot com>
function main()
if AlreadyRunning()
return
endif
.
.
return
/**************************************************************************
If an exe is already running, bring window to front and maximize it.
NOTE: Though the exe is the same, but if it's being run from a different
path, they're considered different exe when using this routine.
*********/
#include "fivewin.ch"
#define GW_HWNDFIRST 0
#define GW_HWNDLAST 1
#define GW_HWNDNEXT 2
#define GW_HWNDPREV 3
#define GW_OWNER 4
#define GW_CHILD 5
#define SW_SHOWMAXIMIZED 3
function AlreadyRunning()
local lRet := .f., cMod := hb_Argv( 0 ), hWnd
if isExeRunning( cFileName( cMod ) )
hWnd := findWindowByModuleFilename( cMod )
setForeGroundWindow(hWnd)
showWindow( hWnd, SW_SHOWMAXIMIZED)
lRet := .t.
endif
return lRet
//----------------------------------------------------------------------
static function FINDWINDOWBYMODULEFILENAME( cModuleFileName )
LOCAL hWnd := getForegroundWindow(), cFile, hRet
while hWnd != 0
cFile := getWindowModuleFilename( hWnd )
cFile := left(cFile, len(cFile)-1) // remove null-termination char
IF cFile == cModuleFileName
hRet := hWnd
exit
ENDIF
hWnd := getWindow( hWnd, GW_HWNDNEXT )
enddo
return hRet
//--------------------------------------------------------------------------
DLL32 FUNCTION GETFOREGROUNDWINDOW() AS LONG;
PASCAL FROM "GetForegroundWindow" LIB "user32.dll"
//--------------------------------------------------------------------------
#pragma BEGINDUMP
#include "windows.h"
#include "hbapi.h"
HB_FUNC( GETWINDOWMODULEFILENAME ) // ( hWnd ) --> cFileName
{
char szFileName[ 256 ];
UINT wChar = GetWindowModuleFileName( ( HWND ) hb_parnl( 1 ),
szFileName, 256 );
hb_retclen( szFileName, wChar );
}
#pragma ENDDUMP
cFile := left(cFile, len(cFile)-1) // remove null-termination char
if right( cFile, 1) == chr(0)
cFile := left(cFile, len(cFile)-1) // remove null-termination char
endif
#pragma BEGINDUMP
#ifndef ASFW_ANY
#define ASFW_ANY ((DWORD)-1)
#endif
HB_FUNC( ALLOWSETFOREGROUNDWINDOW )
{
HINSTANCE h = LoadLibraryEx( "user32.dll", NULL, 0 );
BOOL bASFWRet = (BOOL) FALSE ;
DWORD dwProcessId = ISNIL( 1 ) ? ASFW_ANY : (DWORD) hb_parnl( 1 );
if( h )
{
typedef BOOL (WINAPI *xbAllowSetForegroundWindow)( DWORD dwProcessId );
xbAllowSetForegroundWindow pfnASFW = (xbAllowSetForegroundWindow)
GetProcAddress( h, "AllowSetForegroundWindow") ;
if( pfnASFW )
{
bASFWRet = (BOOL) pfnASFW( dwProcessId ) ;
}
FreeLibrary( h );
}
hb_retl( bASFWRet );
}
#pragma ENDDUMP
Otto wrote:What do you pass as cFileName - the name of the EXE or the Title of the window? Does it work if the exe is minimized?
Regards,
Otto
#include "Fivewin.ch"
FUNCTION MAIN()
LOCAL oWnd
LOCAL cTitle := "This is a test"
IF ISEXERUNNING( CFILENAME( HB_ARGV( 0 ) ) )
SHOWWINDOW( FINDWINDOW( 0, cTitle ), 9 )
SETFOREGROUNDWINDOW( FINDWINDOW( 0, cTitle ) )
RETURN NIL
ENDIF
DEFINE WINDOW oWnd;
TITLE cTitle
ACTIVATE WINDOW oWnd
RETURN NIL
#include "Fivewin.ch"
FUNCTION MAIN()
LOCAL oWnd
LOCAL cTitle := "This is a test"
IF ISEXERUNNING( CFILENAME( HB_ARGV( 0 ) ) )
SHOWWINDOW( FINDWND( cTitle ), 9 )
SETFOREGROUNDWINDOW( FINDWND( cTitle ) )
RETURN NIL
ENDIF
DEFINE WINDOW oWnd;
TITLE "Prefix - " + cTitle + " - Postfix"
ACTIVATE WINDOW oWnd
RETURN NIL
#define GW_HWNDFIRST 0
#define GW_HWNDLAST 1
#define GW_HWNDNEXT 2
#define GW_HWNDPREV 3
#define GW_OWNER 4
#define GW_CHILD 5
FUNCTION FINDWND( cTitle )
LOCAL hWnd := GETWINDOW( GETDESKTOPWINDOW(), GW_CHILD )
WHILE hWnd != 0
IF UPPER( cTitle ) $ UPPER( GETWINDOWTEXT( hWnd ) )
RETURN hWnd
ENDIF
hWnd = GETWINDOW( hWnd, GW_HWNDNEXT )
ENDDO
RETURN NIL
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: Google [Bot] and 102 guests