How to fill edit box and click button in another application

How to fill edit box and click button in another application

Postby Horizon » Tue Feb 14, 2023 8: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: Select all  Expand view
    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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: How to fill edit box and click button in another application

Postby Antonio Linares » Wed Feb 15, 2023 8:00 am

Dear Hakan,

You may try:

PostMessage( hWnd, WM_KEYCHAR, VK_TAB )

etc
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: How to fill edit box and click button in another application

Postby Horizon » Wed Feb 15, 2023 9: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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: How to fill edit box and click button in another application

Postby Antonio Linares » 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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: How to fill edit box and click button in another application

Postby Horizon » 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: Select all  Expand view
PostMessage( hWnd, WM_CHAR, VK_TAB )


unfortunately no results.
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: How to fill edit box and click button in another application

Postby Antonio Linares » 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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: How to fill edit box and click button in another application

Postby Horizon » Wed Feb 15, 2023 1: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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: How to fill edit box and click button in another application

Postby Horizon » Wed Feb 15, 2023 2:13 pm

Hi Antonio,

I have sent a mail.
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: How to fill edit box and click button in another application

Postby Jimmy » Wed Feb 15, 2023 2: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: Select all  Expand view
  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
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: How to fill edit box and click button in another application

Postby Horizon » Sun Mar 12, 2023 4: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: Select all  Expand view

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: Select all  Expand view
#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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: How to fill edit box and click button in another application

Postby Antonio Linares » Sun Mar 12, 2023 5: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: Select all  Expand view
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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: How to fill edit box and click button in another application

Postby Horizon » Sun Mar 12, 2023 6: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: Select all  Expand view
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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: How to fill edit box and click button in another application

Postby Antonio Linares » Sun Mar 12, 2023 6:40 pm

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: How to fill edit box and click button in another application

Postby Horizon » Sun Mar 12, 2023 7: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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 42 guests