QQ交流群:185441009
JNA可以让你像调用一般java方法一样直接调用本地方法。就和直接执行本地方法差不多,而且调用本地方法还不用额外的其他处理或者配置什么的,也不需要多余的引用或者编码,使用很方便。
下面,我就将如何用SWT调用user32.dll中的 FlashWindow函数,在这里举例说明。
FlashWindow函数
可以使标题产生闪烁的效果,类似于QQ任务栏红色闪烁
function FlashWindow(const hwnd:Integer;const bInvert:LongBool):LongBool;
FlashWindow(handle,true);
1、下载JNA的jar包。
2、打开eclipse,建立一个SWT项目,并且新建一个application窗口。
代码如下:
User32类,着个类就是你申明的windows 函数。注意,flashwindow函数在user32.dll下面,大家注意大小写。
package com.test;
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface User32 extends Library {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
boolean FlashWindow(int hwnd, boolean bInvert);
}
窗口类:
package com.test;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class MainWindow {
protected Shell shell;
public static void main(String[] args) {
try {
MainWindow window = new MainWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// 注意看这里是怎么调用的
User32 user32 = User32.INSTANCE;
user32.FlashWindow(shell.handle, true);
}
});
btnNewButton.setBounds(177, 136, 80, 27);
btnNewButton.setText("New Button");
}
}
这样一来,C语言能办到的,java也能办到。。
QQ群:185441009