您的位置:首页 > 编程语言 > Java开发

在java程序中如何能获取到另一个程序的windows句柄,又如何使用这个句柄向这个窗口发送消息

2013-04-20 00:38 1616 查看
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;

public class TryWithHWND {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
}

public static void main(String[] args) {
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WNDENUMPROC() {
int count = 0;
@Override
public boolean callback(HWND hWnd, Pointer arg1) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText);

// get rid of this if block if you want all windows regardless of whether
// or not they have text
if (wText.isEmpty()) {
return true;
}

System.out.println("Found window with text " + hWnd + ", total " + ++count
+ " Text: " + wText);
return true;
}
}, null);
}
}

原文:http://stackoverflow.com/questions/8717999/how-to-get-list-of-all-window-handles-in-java-using-jna

源代码:http://pan.baidu.com/share/link?shareid=411802&uk=3878681452

How to get list of all window handles in Java (Using JNA)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐