Page 1 of 2

SetFocus question.

Posted: Mon Sep 12, 2016 8:50 am
by HunterEC
Guys:

I'm calling a program from another. The called program executes this command:

Code: Select all

         ACTIVATE DIALOG oDlg ON PAINT SetFocus( FindWindow( 0, cCaller ) )
 
This other command:

Code: Select all

MsgInfo( "Find Window: " + STR( FindWindow( 0, cCaller ), 12 ) )
Displays the window handle (numeric).

But control does not return to the caller program (first one). Any clues ?

Re: SetFocus question.

Posted: Mon Sep 12, 2016 12:49 pm
by karinha

Code: Select all

   ACTIVATE DIALOG oDlg ON INIT SetFocus( FindWindow( 0, cCaller ) )
 
Regards.

Re: SetFocus question.

Posted: Mon Sep 12, 2016 9:32 pm
by HunterEC
Karinha:

Thank you for the response but does not work. Any clues ?

Re: SetFocus question.

Posted: Mon Sep 12, 2016 10:33 pm
by Gale FORd
What Windows version are you using?
You might try a combination of

Code: Select all

nCaller := FindWindow( 0, cCaller )
ACTIVATE DIALOG oDlg ON INIT ( SysRefresh(), SetFocus( nCaller ), SetForeGroundWindow( nCaller ) )
 

Re: SetFocus question.

Posted: Tue Sep 13, 2016 5:16 am
by HunterEC
Gale:

Thank you for your response but it did not worked. I'm using Windows 10 Pro x86 & x64.

I think Cristobal, Rao, Uwe or Antonio or any other great people here at the forum (which I'm missing the names) can help us out.

Re: SetFocus question.

Posted: Tue Sep 13, 2016 7:34 am
by Antonio Linares
Gustavo,

Please provide a small and self contained example to test here, thanks

Re: SetFocus question.

Posted: Tue Sep 13, 2016 10:45 pm
by HunterEC
Antonio:

Caller Program (Harbour console program)

Code: Select all

                  cOldCap := GetConsoleTitle()
                  cCommand := "EXTCMD.EXE " + "IMAGE IMAGES\" + ALLTRIM(Img_filenm) + ".JPG" + " " + ["] + Img_name + ["] + " " + ["] + cOldCap + ["]
                  cCaption := ALLTRIM( Img_name ) + " - Software (C) Ver. 1.0"
                  RUN ( "&cCommand" )
EXTCMD.EXE (used to avoid caller program to be tied to called one until it closes. This way caller program can continue without called one being closed)

Code: Select all

#include "Common.ch"


PROCEDURE Main ( cCommand, cArg1, cArg2, cArg3, cArg4 )
   MEMVAR cCmd, cParms

   PRIVATE cCmd, cParms := ""

   DEFAULT cCommand TO ""
   DEFAULT cArg1    TO ""
   DEFAULT cArg2    TO ""
   DEFAULT cArg3    TO ""
   DEFAULT cArg4    TO ""

   cCmd  := cCommand

   IF VALTYPE( cArg1 ) == "C" .AND. VALTYPE( cArg2 ) == "C" .AND. ;
      VALTYPE( cArg3 ) == "C"

      cParms := ["] + cArg1 + ["] + " " + ["] + cArg2 + ["] + " " + ["] + cArg3 + ["]
   ELSE
      cParms := cArg1
   ENDIF

   IF VALTYPE( cCmd ) == "C"
      IF cParms != NIL .AND. LEFT( cParms, 1 ) != CHR( 34 )
         cParms := CHR( 34 ) + cParms
      ENDIF
      IF cParms != NIL .AND. RIGHT( cParms, 1 ) != CHR( 34 )
         cParms := cParms + CHR( 34 )
      ENDIF

      DEFAULT cParms TO ""

      RUN START /I /B &cCmd &cParms
   ENDIF
RETURN
// EOP: Main

Called program (image.exe):

Code: Select all

#define NO_OF_COLS   INT( GetSysMetrics( 0 ) / 7.933884297520661157 )
#define NO_OF_ROWS   INT( GetSysMetrics( 1 ) / 15.18987341772151898 )

// #define NO_OF_COLS   242
// #define NO_OF_ROWS   77


#include "Common.ch"
#include "Fivewin.ch"
#include "Image.ch"


//----------------------------------------------------------------------------//

function Main ( cFile, cImgName, cCaller )
   LOCAL oDlg, oSay1, oImage, nCaller

   DEFINE DIALOG oDlg ;
      TITLE ALLTRIM( cImgName ) + " - Software (C) Ver. 1.0"
   oDlg:nTop    := GetSysMetrics( 1 ) - 250 - 65
   oDlg:nLeft   := GetSysMetrics( 0 ) - 377 - 10
   oDlg:nBottom := GetSysMetrics( 1 ) - 65
   oDlg:nRight  := GetSysMetrics( 0 ) - 10

   @ 00,00 SAY oSay1 VAR cImgName OF oDlg CENTERED PIXEL SIZE 145,10;
          COLOR nRGB(255,255,255), nRGB(0,0,255)
   @ 10,00 IMAGE oImage SIZE 150, 140 OF oDlg SCROLL PIXEL // ADJUST

   @ 10,155 BUTTON "&Imprimir" SIZE 30,10 OF oDlg PIXEL ACTION PrintImage( oImage )

   @ 55,155 BUTTON "&Terminar" SIZE 30,10 OF oDlg PIXEL ACTION oDlg:End()

   @ 110,155 CHECKBOX oImage:lStretch PROMPT "&Agrandar" SIZE 75, 10 OF oDlg ;
             PIXEL ON CHANGE ( oImage:ScrollAdjust(), oImage:Refresh() )

   IF OpenFile ( @oImage, @cFile )
         nCaller := FindWindow( 0, cCaller )
         ACTIVATE DIALOG oDlg ON INIT ( SysRefresh(), SetFocus( nCaller ), SetForeGroundWindow( nCaller ) )
//         ACTIVATE DIALOG oDlg ON INIT SetFocus( FindWindow( 0, cCaller ) )
   ENDIF
return nil
// EOF: Main

//----------------------------------------------------------------------------//

FUNCTION OpenFile (oImage, cFile)
   LOCAL lPass

   IF ! (lPass := FILE(cFile))
      MsgInfo("Esta Imagen No Existe: " + cFile)
   ELSE
      oImage:LoadBmp(cFile)
   ENDIF
RETURN (lPass)
* EOF: OpenFile


FUNCTION PrintImage( oImage )

   LOCAL oPrn

   PRINT oPrn NAME "Image Printing" PREVIEW
      PAGE
         oPrn:SayImage( 0, 0, oImage )
      ENDPAGE
   ENDPRINT

RETURN NIL
* EOF: PrintImage

//----------------------------------------------------------------------------//

Re: SetFocus question.

Posted: Wed Sep 14, 2016 8:02 am
by Antonio Linares
Gustavo,

Try to use SetForeGroundWindow( hWnd ) instead of SetFocus( hWnd )

Re: SetFocus question.

Posted: Mon Sep 19, 2016 6:44 pm
by HunterEC
Antonio:

Thank you for your response but it didn't worked, it stayed in the called program. Any ideas ?

Re: SetFocus question.

Posted: Tue Sep 20, 2016 9:37 am
by Antonio Linares
Gustavo,

The initial caller app is a console app (text mode), right ?

Re: SetFocus question.

Posted: Wed Oct 12, 2016 2:13 am
by HunterEC
Yes, console app.

Re: SetFocus question.

Posted: Wed Oct 12, 2016 10:03 am
by Antonio Linares
It seems to be the function to use:
GetConsoleWindow function

Retrieves the window handle used by the console associated with the calling process.
https://msdn.microsoft.com/en-us/library/ms683175.aspx

once we have such handle, we can call SetFocus() :-)

Re: SetFocus question.

Posted: Fri Oct 14, 2016 6:11 am
by HunterEC
Antonio:

How do I call that function from Harbour ? Thank you very much !

Re: SetFocus question.

Posted: Fri Oct 14, 2016 7:27 am
by Antonio Linares
Gustavo,

Copia este código al final de tu PRG principal

Code: Select all

#pragma BEGINDUMP

#include <windows.h>
#include <hbapi.h>

HB_FUNC( GETCONSOLEWINDOW )
{
   hb_retnll( GetConsoleWindow() );
}

#pragma ENDDUMP

Re: SetFocus question.

Posted: Mon Feb 20, 2017 3:02 pm
by HunterEC
Having this at the bottom of my main FW .PRG gives me 2 compiler errors, eventhough it links and creates the executable (EXE).

Code: Select all

Embarcadero C++ 7.00 for Win32 Copyright (c) 1993-2015 Embarcadero Technologies, Inc.
test.c:
Error E2451 test.prg 304: Undefined symbol 'buffer' in function HB_FUN_GETCONSOLEWINDOW
Error E2451 test.prg 304: Undefined symbol 'dwSize' in function HB_FUN_GETCONSOLEWINDOW
*** 2 errors in Compile ***
Borland Resource Compiler  Version 5.40
Copyright (c) 1990, 1999 Inprise Corporation.  All rights reserved.
Turbo Incremental Link 6.70 Copyright (c) 1997-2014 Embarcadero Technologies, Inc.
* Application successfully built *

Code: Select all

#pragma BEGINDUMP

#include <windows.h>
#include <hbapi.h>

HB_FUNC( GETCONSOLEWINDOW )
{
   hb_retclen( buffer, dwSize );
}

#pragma ENDDUMP