java实现任务栏闪烁_SWT : 通过JNA调用windows api FlashWindow函数(QQ任务栏 红色闪动)...

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窗口。

4bfb0d53bbd6c1052ed2ae902ed6a5be.png

代码如下:

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值