我正在尝试从类似Win32 ListView的控件中检索项目文本。我正在使用JNA和SendMessageW()将LVM_GETITEMTEXTW发送到控件。我已成功检索项目计数(通过LVM_GETITEMCOUNT),但此时我很难过。我的User32类设置如下:
public interface MyUser32 extends User32 {
MyUser32 INSTANCE = (MyUser32)Native.loadLibrary("user32", MyUser32.class);
LRESULT SendMessageW(HWND hWnd, int msg, WPARAM wParam, LVITEM lParam);
}
我的LVITEM类设置如下:
public class LVITEM extends Structure{
public LVITEM() {
pszText = new Memory(MEMSIZE);
cchTextMax = MEMSIZE;
}
private static final int MEMSIZE = 256;
public UINT mask;
public int iItem;
public int iSubItem;
public UINT state;
public UINT stateMask;
public Pointer pszText;
public int cchTextMax;
public int iImage;
public LPARAM lParam;
public int iIndent;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "mask", "iItem", "iSubItem", "state", "stateMask", "pszText", "cchTextMax", "iImage", "lParam", "iIndent"});
}
}
调用它的代码就是这样:
MyUser32 u32 = MyUser32.INSTANCE;
LVITEM lvItem = new LVITEM();
WPARAM wPar = new WPARAM(1);
...
lvItem.iSubItem = 0;
res = u32.SendMessageW(handle, LVM_GETITEMTEXTW, wPar, lvItem);
System.out.println(res.intValue());
s = lvItem.pszText.getString(0);
System.out.println(s);
我遗漏了一些代码,但我相信这些是重要的部分。我的问题是,当我打印出res.intValue()时,它总是0(表示没有返回文本),当我打印出pszText的字符串值时,它总是一些垃圾字符。我对这一点感到非常难过,所以非常感谢任何建议。感谢。