jna发送编辑控制消息

时间:2013-09-02 05:08:17

标签: java jna

事先道歉,因为我是新手。

我正在尝试使用带有java和JNA库的基于Windows的游戏rFactor。我已经看到了人们使用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;
}

2 个答案:

答案 0 :(得分:0)

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

0x000C是指WM_SETTEXT

答案 1 :(得分: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);
}