S
S
SergMC2018-08-05 18:13:58
Java
SergMC, 2018-08-05 18:13:58

How to get the coordinates (x,y) of an element (button) of an open exe program using Java?

Hello. I am making a program in Java. I want to implement an imitation of pressing the button of an open program.
5b671331bf5c9765083894.jpeg
The screenshot shows the program itself. Task: programmatically click on the Login button. Found a crutch:

package ru.first;

import java.awt.event.*;
import java.awt.Robot;
public class test {
 public static void main(String args[]) {
  Robot bot = null;
  try {
   bot = new Robot();
  } catch (Exception failed) {
   System.err.println("Failed instantiating Robot: " + failed);
  }
  int mask = InputEvent.BUTTON1_MASK;
  bot.mouseMove(x, y);
  bot.mousePress(mask);
  bot.mouseRelease(mask);
 }
}

The essence of the code is to click on the mouse button at the given coordinates. All you need is to get the coordinates of the element of the open exe program. Element - Login button (in the screenshot). How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Satangelus, 2018-08-06
@Satangelus

The coordinates of the button IMHO can be defined as follows:
1. The easiest way to count in pixels from the edge of the window. And the coordinates of the window, get through winepi, getting the hwnd of the desired process, and from it call the function for determining the coordinates of the window.
2. Difficult through thorns to the stars. First, we get the hwnd of the process's active window. And then the magic. All visible window elements in the system register a "window class". That is, through the hwnd window, you can get a list of child "windows" and use this list to figure out where the button we need is in the list.
Press the button.
1. It's easier to move the mouse in client coordinates and click.
2. If the button can appear in different places of the window, we simulate the transition through the elements of the window by sending TAB and then sending ENTER.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question