FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour SetFocus question.
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
SetFocus question.
Posted: Mon Sep 12, 2016 08:50 AM
Guys:

I'm calling a program from another. The called program executes this command:
Code (fw): Select all Collapse
         ACTIVATE DIALOG oDlg ON PAINT SetFocus( FindWindow( 0, cCaller ) )


This other command:
Code (fw): Select all Collapse
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 ?
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: SetFocus question.
Posted: Mon Sep 12, 2016 12:49 PM
Code (fw): Select all Collapse
   ACTIVATE DIALOG oDlg ON INIT SetFocus( FindWindow( 0, cCaller ) )


Regards.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
Re: SetFocus question.
Posted: Mon Sep 12, 2016 09:32 PM

Karinha:

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

Posts: 663
Joined: Mon Dec 05, 2005 11:22 PM
Re: SetFocus question.
Posted: Mon Sep 12, 2016 10:33 PM
What Windows version are you using?
You might try a combination of
Code (fw): Select all Collapse
nCaller := FindWindow( 0, cCaller )
ACTIVATE DIALOG oDlg ON INIT ( SysRefresh(), SetFocus( nCaller ), SetForeGroundWindow( nCaller ) )
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
Re: SetFocus question.
Posted: Tue Sep 13, 2016 05:16 AM

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.

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: SetFocus question.
Posted: Tue Sep 13, 2016 07:34 AM

Gustavo,

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

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
Re: SetFocus question.
Posted: Tue Sep 13, 2016 10:45 PM
Antonio:

Caller Program (Harbour console program)
Code (fw): Select all Collapse
                  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 (fw): Select all Collapse
#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 (fw): Select all Collapse
#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

//----------------------------------------------------------------------------//
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: SetFocus question.
Posted: Wed Sep 14, 2016 08:02 AM

Gustavo,

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

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
Re: SetFocus question.
Posted: Mon Sep 19, 2016 06:44 PM

Antonio:

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

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: SetFocus question.
Posted: Tue Sep 20, 2016 09:37 AM

Gustavo,

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

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
Re: SetFocus question.
Posted: Wed Oct 12, 2016 02:13 AM

Yes, console app.

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: SetFocus question.
Posted: Wed Oct 12, 2016 10:03 AM
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
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
Re: SetFocus question.
Posted: Fri Oct 14, 2016 06:11 AM

Antonio:

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

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: SetFocus question.
Posted: Fri Oct 14, 2016 07:27 AM
Gustavo,

Copia este código al final de tu PRG principal

Code (fw): Select all Collapse
#pragma BEGINDUMP

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

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

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
Re: SetFocus question.
Posted: Mon Feb 20, 2017 03:02 PM
Having this at the bottom of my main FW .PRG gives me 2 compiler errors, eventhough it links and creates the executable (EXE).

Code (fw): Select all Collapse
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 (fw): Select all Collapse
#pragma BEGINDUMP

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

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

#pragma ENDDUMP