Java中调用Windows API的方法
要在Java中调用Windows API,最简单的方法就是使用JNative.jar中提供的接口。该库已经对Linux和Windows系统中的API进行了封装,例如对Windows,使用它里面的接口调用就和在Delphi等开发工具中的调用方法是一样的,因为函数的名字和参数都是一样的。下面说明其用法。安装方法:将JNative.jar加到classpath中即可。假如现在要给QQ的窗口发送消息,程序如下:
import org.junit.Test; import org.xvolks.jnative.misc.basicStructures.HWND; import org.xvolks.jnative.misc.basicStructures.LPARAM; import org.xvolks.jnative.misc.basicStructures.UINT; import org.xvolks.jnative.misc.basicStructures.WPARAM; import org.xvolks.jnative.util.User32; public class JNativeLearning { @Test public void sendMessage() throws Exception{ HWND hWnd = User32.FindWindow("TXGuiFoundation", "QQ2010"); if(hWnd.getValue()>0){ System.out.println("window exists"); User32.SendMessage(hWnd, new UINT(0x10), new WPARAM(0), new LPARAM(0)); }else{ System.out.println("window doesn't exists"); } } }