提前道歉,因为我是新手。
我正在尝试使用带有 java 和 JNA 库的名为 rFactor 的基于 Windows 的游戏。我已经看到使用 c++ 的人想要做什么。
到目前为止,我有这个(我从另一个 stackoverflow 后的Java search for on-screen text field复制了大部分内容):
package au.gov.nsw.lpi.bds.jnatest;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.examples.win32.W32API.HWND;
import com.sun.jna.examples.win32.W32API.LPARAM;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public class IterateChildWindows {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
int SendMessage(HWND hWnd, int msg, int wParam, byte[] lParam);
boolean FindWindowEx(HWND parent, HWND child, String className, String window);
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
boolean EnumChildWindows(HWND parent, WNDENUMPROC callback, LPARAM info);
interface WNDENUMPROC extends StdCallCallback {
boolean callback(HWND name, Pointer arg);
}
int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
int GetClassNameA(HWND in, byte[] lpString, int size);
}
public static void main(String[] args) {
User32.INSTANCE.EnumWindows(new User32.WNDENUMPROC() {
public boolean callback(HWND hWnd, Pointer userData) {
byte[] textBuffer = new byte[512];
User32.INSTANCE.GetWindowTextA(hWnd, textBuffer, 512);
String wText = Native.toString(textBuffer);
if (wText.contains("ISI Dedicated Server")) {
if (User32.INSTANCE.FindWindowEx(hWnd, null, "Static", "Game Name:")) {
System.out.println(new String(textBuffer).trim() + " - " + hWnd);
User32.INSTANCE.EnumChildWindows(hWnd, new User32.WNDENUMPROC() {
int count = 1;
public boolean callback(HWND hWnd, Pointer userData) {
byte[] textBuffer = new byte[512];
User32.INSTANCE.GetClassNameA(hWnd, textBuffer, 512);
if ((new String(textBuffer).trim()).contains("Edit")) {
System.out.println(new String(textBuffer).trim());
System.out.println(hWnd);
// User32.INSTANCE.SendMessage(hWnd,
// WM_SETTEXT, msg.length(), (LPARAM) msg);
}
if ((new String(textBuffer).trim()).contains("Button")) {
if (count == 8) {
System.out.println(new String(textBuffer).trim() + " " + count);
System.out.println(hWnd);
}
count++;
}
return true;
}
}, null);
}
}
return true;
}
}, null);
}
}
这产生
ISI Dedicated Server - native@0x207e0 (com.sun.jna.examples.win32.W32API$HWND@207e0)
Edit
native@0x207ac (com.sun.jna.examples.win32.W32API$HWND@207ac)
Button 8
native@0x207a8 (com.sun.jna.examples.win32.W32API$HWND@207a8)
我认为我在正确的轨道上,因为我已经隔离了我需要的正确的“编辑”和“按钮”。我相信我现在需要将文本放入编辑字段,然后使用按钮
int SendMessage(HWND hWnd, int msg, int wParam, byte[] lParam);
这是来自 c++ 的示例
SendMessage(chatHwnd, WM_SETTEXT, msgSB.Length, msgSB)
我已经尝试过,但没有运气让它工作。任何帮助,将不胜感激。
编辑,我已经试过了(虽然我在黑暗中摸不着头脑)
int SendMessage(HWND hWnd, int msg, int wParam, byte[] lParam);
和这个
public boolean callback(HWND hWnd, Pointer userData) {
byte[] textBuffer = new byte[512];
User32.INSTANCE.GetClassNameA(hWnd, textBuffer, 512);
if ((new String(textBuffer).trim()).contains("Edit")) {
User32.INSTANCE.SendMessage(hWnd,0xC, msg.length,
Native.toByteArray("MessageFromNick"));
}
if ((new String(textBuffer).trim()).contains("Button")) {
if (count == 8) {
User32.INSTANCE.SendMessage(hWnd, 0x0201, 0, 0);
}
count++;
}
return true;
}