SetFocus question.

HunterEC
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

SetFocus question.

Post 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 ?
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: SetFocus question.

Post by karinha »

Code: Select all

   ACTIVATE DIALOG oDlg ON INIT SetFocus( FindWindow( 0, cCaller ) )
 
Regards.
João Santos - São Paulo - Brasil
HunterEC
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Re: SetFocus question.

Post by HunterEC »

Karinha:

Thank you for the response but does not work. Any clues ?
Gale FORd
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston
Contact:

Re: SetFocus question.

Post 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 ) )
 
HunterEC
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Re: SetFocus question.

Post 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.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: SetFocus question.

Post by Antonio Linares »

Gustavo,

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

Antonio Linares
www.fivetechsoft.com
HunterEC
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Re: SetFocus question.

Post 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

//----------------------------------------------------------------------------//
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: SetFocus question.

Post by Antonio Linares »

Gustavo,

Try to use SetForeGroundWindow( hWnd ) instead of SetFocus( hWnd )
regards, saludos

Antonio Linares
www.fivetechsoft.com
HunterEC
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Re: SetFocus question.

Post by HunterEC »

Antonio:

Thank you for your response but it didn't worked, it stayed in the called program. Any ideas ?
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: SetFocus question.

Post by Antonio Linares »

Gustavo,

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

Antonio Linares
www.fivetechsoft.com
HunterEC
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Re: SetFocus question.

Post by HunterEC »

Yes, console app.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: SetFocus question.

Post 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() :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
HunterEC
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Re: SetFocus question.

Post by HunterEC »

Antonio:

How do I call that function from Harbour ? Thank you very much !
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: SetFocus question.

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
HunterEC
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Re: SetFocus question.

Post 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
 
Post Reply