我开始尝试使用JNA从Windows访问每个工具提示。为此,我将TTM_GETTOOLINFO消息连续发送到窗口。以下是我的代码......
public class Test {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
HWND GetForegroundWindow(); // add this
int SendMessageA(HWND hwnd, int msg, int num1, TOOLINFO f );
// int SendMessageW(HWND hwnd, int msg, int num1, TOOLINFO f );
}
public class TOOLINFO extends Structure
{
public int cbSize;
public int uFlags;
public HWND hwnd;
public UINT_PTR uId;
public RECT rect;
public HINSTANCE hinst;
public char [] lpszText;
TOOLINFO()
{
lpszText = new char[512];
}
}
public static void main(String[] args)
{
new Test().go();
}
public void go()
{
TOOLINFO tt = new TOOLINFO();
int WM_USER = 0x0400;
HWND hwnd ;
int i=0;
while(true)
{
hwnd= User32.INSTANCE.GetForegroundWindow();
try
{
i=User32.INSTANCE.SendMessageA(hwnd,WM_USER+8, 0, tt);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(i!=0)
System.out.println("Tooltip :"+tt.lpszText);
}
}
}
但它不起作用。我正在使用窗口8.我在网上找到了两个版本的commctrl.h。一个显示TTM_GETTOOLINFOA = WM_USER + 8 TTM_GETTOOLINFOW = WM_USER + 53,其他显示TTM_GETTOOLINFOA = WM_USER + 9 TTM_GETTOOLINFOW = WM_USER + 54。然而,我尝试与SendMessageA和W的每一个组合。所以我认为有一些基本的错误。所以任何人都可以帮助我。从JNA访问工具提示。
public class JnaTest2 {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
HWND GetForegroundWindow(); // add this
int SendMessageW(HWND hwnd,int msg ,WPARAM w,TOOLINFOW lparam);
}
public class TOOLINFOW extends Structure
{
public int cbSize;
public int uFlags;
public HWND hwnd;
public UINT_PTR uId;
public RECT rect;
public HINSTANCE hinst;
public Pointer pszText = new Memory(512);
LPARAM l;
Pointer lpReserved=null;
}
public static void main(String[] args)
{
new JnaTest2().go();
}
public void go()
{
int WM_USER = 0x0400;
TOOLINFOW tt = new TOOLINFOW();
tt.cbSize=tt.size();
System.out.println(tt.size());
HWND hwnd ;
int j=0;
while(true)
{
hwnd=User32.INSTANCE.GetForegroundWindow();
tt.hwnd=hwnd;
try
{
j=User32.INSTANCE.SendMessageW(hwnd,WM_USER+53,new WPARAM(0),tt);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(j!=0)
System.out.println("Tooltip :"+tt.pszText.getString(0));
}
}
}
答案 0 :(得分:1)
TTM_GETTOOLINFOW
。SendMessageW
。TOOLINFO
的hWnd
和cbSize
字段。
lpszText
必须是指针类型,而不是内联数组。您可以使用Memory
,字段类型为Pointer
。确保它足够大。lParam
(类型LPARAM
)和lpReserved
(类型Pointer
)字段。编辑您的问题,以显示您如何实施这些内容以及相应的结果。
修改强>
检查Native.getLastError()
以查看方法调用是否导致错误。它不会抛出异常。