FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to fill edit box and click button in another application
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
How to fill edit box and click button in another application
Posted: Tue Feb 14, 2023 08:23 PM
Hi,

I can fill edit box, press button in a webpage using TWebView. I also want to fill edit box and press button in another application from mine. Another application is not mine. I have not any source. It is a java application.
Code (fw): Select all Collapse
    hWnd := FindWindow( 0,"Second Application")
    
    ? hWnd    // There is a hWnd value.
    
    BringWindowToTop( hWnd )

        .... I want to press TAB Key 3 times.

        .... I want to paste text "12abc"

        .... I want to click a button.
Is there any suggestion?
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: How to fill edit box and click button in another application
Posted: Wed Feb 15, 2023 08:00 AM

Dear Hakan,

You may try:

PostMessage( hWnd, WM_KEYCHAR, VK_TAB )

etc

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: How to fill edit box and click button in another application
Posted: Wed Feb 15, 2023 09:46 AM
Antonio Linares wrote:Dear Hakan,

You may try:

PostMessage( hWnd, WM_KEYCHAR, VK_TAB )

etc
Thank you Antonio,

What is WM_KEYCHAR? There is not in winapi.ch.
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: How to fill edit box and click button in another application
Posted: Wed Feb 15, 2023 10:55 AM

Dear Hakan,

It is WM_CHAR, not WM_KEYCHAR sorry

define WM_CHAR 0x0102

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: How to fill edit box and click button in another application
Posted: Wed Feb 15, 2023 12:06 PM
Antonio Linares wrote:Dear Hakan,

It is WM_CHAR, not WM_KEYCHAR sorry

#define WM_CHAR 0x0102
Code (fw): Select all Collapse
PostMessage( hWnd, WM_CHAR, VK_TAB )
unfortunately no results.
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: How to fill edit box and click button in another application
Posted: Wed Feb 15, 2023 12:28 PM

Please check if this makes a difference:

PostMessage( hWnd, WM_CHAR, VK_TAB, 0 )

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: How to fill edit box and click button in another application
Posted: Wed Feb 15, 2023 01:13 PM
Antonio Linares wrote:Please check if this makes a difference:

PostMessage( hWnd, WM_CHAR, VK_TAB, 0 )
No change.
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: How to fill edit box and click button in another application
Posted: Wed Feb 15, 2023 02:13 PM

Hi Antonio,

I have sent a mail.

Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: How to fill edit box and click button in another application
Posted: Wed Feb 15, 2023 02:17 PM
hi

i done know JAVA so i can only tell you about Windows Control

to find Handle of Window you need FindWindow() like in your Sample

to find a Control "in" a Window you need FindWindowEx() and "lpszClass" of Control e.g. "Button"
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowexa
Code (fw): Select all Collapse
   hWndChildAfter := 0
   lpszClass := "Button"
   lpszWindow := NIL     // -> Title / Caption

   nTo := FindWindowEx(hWnd, hWndChildAfter, lpszClass+Chr(0), lpszWindow)
   IF !EMPTY(nTo)
      SendMessage(nTo, BM_CLICK, 0, 0)
   ENDIF
for TEXT you have to look for "Edit" and use WM_TEXT with SendMessage()

---

when have multiple of same lpszClass you can use lpszWindow (Title / Caption) to identify
you also can use API EnumWindows() to find Control you want to "talk to"
greeting,

Jimmy
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: How to fill edit box and click button in another application
Posted: Sun Mar 12, 2023 04:30 PM
Hi,


this sample app works very well. testbtnz.prg is fwh application.

If I use java application instead of testbtnz.prg, The movemove also works. But, Click button does not work. Is there anyone that use this method with java application?

Can you help me please.

Thanks.

Code (fw): Select all Collapse
FUNCTION Test(cText)
LOCAL hWnd, oWnd, nTry:=0, cMyFile, cLink, cText_Esas

    cText_Esas :="Test Button on Dialog"
    cMyFile:="C:\fwh\samples\testbtnz.exe"
    
    SHELLEXECUTE( 0, "open", cMyFile, 0, 0, 1 )
    
    do while nTry<10 .AND. EMPTY(hWnd)
        SysWait(1)
        hWnd := FindWindow( 0, cText_Esas)
        nTry++
    Enddo
    IF EMPTY(hWnd)
        MsgAlert("Cannot find hWnd","Error")
        RETURN
    ENDIF
    
    ? hWnd, nTry

    BringWindowToTop( hWnd )
    SysWait(1)
    
    Ob_MouseMove(hWnd, 100, 100)
    Ob_LButtonDown(hWnd, 100, 100)
    SysWait(.1) 
    Ob_LButtonUp(hWnd, 100, 100)    

RETURN

FUNCTION Ob_MouseMove(hWnd, nYY, nXX)
LOCAL nX, nY, hCtrl, aPos 
  nX = nXX + GetWndRect( hWnd )[ 2 ]
  nY = nYY + GetWndRect( hWnd )[ 1 ]
  SetCursorPos( nX, nY )
  hCtrl = WindowFromPoint( nX, nY )
  LogFile( "TestJava.log", { "MM", nX, nY, hCtrl, GetClassName( hCtrl ) } )
  aPos = ScreenToClient( hCtrl, { nX, nY } )
  PostMessage( hCtrl, WM_MOUSEMOVE, aPos[ 1 ], aPos[ 2 ] )
RETURN

FUNCTION Ob_LButtonDown(hWnd, nYY, nXX)
LOCAL nX, nY, hCtrl, aPos 
  nX = nXX + GetWndRect( hWnd )[ 2 ]
  nY = nYY + GetWndRect( hWnd )[ 1 ]
  SetCursorPos( nX, nY )
  hCtrl = WindowFromPoint( nX, nY )
  LogFile( "TestJava.log", { "MD", nX, nY, hCtrl, GetClassName( hCtrl ) } )
  aPos = ScreenToClient( hCtrl, { nX, nY } )
  PostMessage( hCtrl, WM_LBUTTONDOWN, aPos[ 1 ], aPos[ 2 ] )
RETURN

FUNCTION Ob_LButtonUp(hWnd, nYY, nXX)
LOCAL nX, nY, hCtrl, aPos 
  nX = nXX + GetWndRect( hWnd )[ 2 ]
  nY = nYY + GetWndRect( hWnd )[ 1 ]
  SetCursorPos( nX, nY )
  hCtrl = WindowFromPoint( nX, nY )
  LogFile( "TestJava.log", { "MU", nX, nY, hCtrl, GetClassName( hCtrl ) } )
  aPos = ScreenToClient( hCtrl, { nX, nY } )
  PostMessage( hCtrl, WM_LBUTTONUP, aPos[ 1 ], aPos[ 2 ] )
RETURN
testbtnz.prg
Code (fw): Select all Collapse
#include 'FIVEWIN.CH'

PROCEDURE Main()
   local oFont, oDlg
   define font oFont name 'MS Sans Serif' size 6,15

   define dialog oDlg from 0,0 to 12,35 font oFont title 'Test Button on Dialog'
   @ 1,3 BUTTON "&Button Test on Dialog" size 100,50 font oFont OF oDlg ACTION MsgInfo("Button Test on Dialog")
   activate dialog oDlg centered

return
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: How to fill edit box and click button in another application
Posted: Sun Mar 12, 2023 05:28 PM
Dear Hakan, I just asked chatGPT to port it to java. Not sure if this may help.

not sure if it compiles/works/etc
Code (fw): Select all Collapse
import javax.swing.*;

public class Main {

    public static void main(String[] args) {
        // Create font
        Font font = new Font("MS Sans Serif", Font.PLAIN, 15);

        // Create dialog box
        JDialog dialog = new JDialog();
        dialog.setTitle("Test Button on Dialog");
        dialog.setSize(300, 200);
        dialog.setLocationRelativeTo(null);
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        // Create button
        JButton button = new JButton("&Button Test on Dialog");
        button.setFont(font);
        button.setSize(100, 50);
        button.addActionListener(e -> JOptionPane.showMessageDialog(dialog, "Button Test on Dialog"));

        // Add button to dialog box
        dialog.add(button);

        // Display dialog box
        dialog.setVisible(true);
    }
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: How to fill edit box and click button in another application
Posted: Sun Mar 12, 2023 06:07 PM
Thank you ver much Antonio,

I do not want to write java application. I just want to click the button in a java program written by someone else from within the fivewin application.

Antonio Linares wrote:Dear Hakan, I just asked chatGPT to port it to java. Not sure if this may help.

not sure if it compiles/works/etc
Code (fw): Select all Collapse
import javax.swing.*;

public class Main {

    public static void main(String[] args) {
        // Create font
        Font font = new Font("MS Sans Serif", Font.PLAIN, 15);

        // Create dialog box
        JDialog dialog = new JDialog();
        dialog.setTitle("Test Button on Dialog");
        dialog.setSize(300, 200);
        dialog.setLocationRelativeTo(null);
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        // Create button
        JButton button = new JButton("&Button Test on Dialog");
        button.setFont(font);
        button.setSize(100, 50);
        button.addActionListener(e -> JOptionPane.showMessageDialog(dialog, "Button Test on Dialog"));

        // Add button to dialog box
        dialog.add(button);

        // Display dialog box
        dialog.setVisible(true);
    }
}
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: How to fill edit box and click button in another application
Posted: Sun Mar 12, 2023 06:40 PM

Have you tried to increase this value SysWait(.1) ?

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: How to fill edit box and click button in another application
Posted: Sun Mar 12, 2023 07:05 PM
Antonio Linares wrote:Have you tried to increase this value SysWait(.1) ?
Yes, I tried from .1 to 10. There is no change.
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06

Continue the discussion