1

提前道歉,因为我是新手。

我正在尝试使用带有 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;
}
4

2 回答 2

0

User32.INSTANCE.SendMessageA(hWnd, 0x000C, 0, Native.toByteArray("TextMessage"));

0x000C 指 WM_SETTEXT

于 2013-09-04T05:33:40.770 回答
0

界面:

static Map UNICODE_OPTIONS = new HashMap() {
    {
        put("type-mapper", com.sun.jna.win32.W32APITypeMapper.UNICODE);
        put("function-mapper", com.sun.jna.win32.W32APIFunctionMapper.UNICODE);
    }
};

public interface UUser32 extends StdCallLibrary {
    UUser32 INSTANCE = (UUser32)(UUser32)Native.load("user32", UUser32.class, UNICODE_OPTIONS);

    int SendMessage(com.sun.jna.platform.win32.WinDef.HWND hWnd, int msg, int wParam, char[] lParam);
}

称呼:

static final int WM_SETTEX = 0x000C;//from winuser.h
static public void setEditText(com.sun.jna.platform.win32.WinDef.HWND windowH, com.sun.jna.platform.win32.WinDef.HWND editH, String text){
    user32.SetForegroundWindow(windowH);//user32 is User32.INSTANCE defined in com.sun.jna.platform.win32.User32;
    char[] ctext = Native.toCharArray(text);
    UUser32.INSTANCE.SendMessage(editH, WM_SETTEX, 0, ctext);
}
于 2020-05-08T11:54:29.573 回答