既然 sdtm1016 兄 给出新的建议,那我们就继续完善下 SnapShoot
按 sdtm1016 兄的需求,这次再增加三个功能:
1. 修改文件选择窗口的默认目录为系统桌面,且给定默认文件名。
2. 增加托盘功能,且程序运行时,不自动显示软件窗口。
3. 增加系统热键功能,即:不管程序当前有没有获得焦点,均可以保持键盘快捷键的监听,实现快捷功能。
功能一: 修改文件选择窗口的默认目录为系统桌面,且给定默认文件名。
对于在当前用户的系统桌面目录的取得,Java API 已经有提供了:
//得到当前用户的桌面目录
File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
那么我们设定文件选择窗口的默认选中文件为 用户桌面目录下的 save.png :
JFileChooser chooser = new JFileChooser();
File selectedFile = new File(FileSystemView.getFileSystemView().getHomeDirectory(), "save.png");
//设置默认选中文件
chooser.setSelectedFile(selectedFile);
功能一完成。
功能二:增加托盘功能,且程序运行时,不自动显示软件窗口。
在 JDK6.0 中,也提供了对系统托盘的操作。 本例关于加入系统托盘的代码:
/**
* 加入系统托盘
*/
private void addSystemTray() {
//修改窗口关闭和最小化事件
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
SnapShoot.this.setVisible(false);
}
public void windowIconified(WindowEvent e) {
SnapShoot.this.setVisible(false);
}
});
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
// 为这个托盘加一个弹出菜单
final PopupMenu popup = new PopupMenu();
MenuItem item = new MenuItem("open ctrl + shift + o");
MenuItem exit = new MenuItem("exit");
popup.add(item);
popup.add(exit);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SnapShoot.this.setVisible(true);
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//清除系统热键
JIntellitype.getInstance().cleanUp();
System.exit(1);
}
});
// 为这个托盘加一个提示信息
Image scaleLogo = ((BufferedImage)logo).getScaledInstance(16, 16, Image.SCALE_FAST);
TrayIcon trayIcon = new TrayIcon(scaleLogo, "屏幕截图小软件: SnapShoot\n作者:pengranxiang", popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("无法向这个托盘添加新项: " + e);
}
} else {
System.err.println("无法使用系统托盘!");
}
}
运行后的效果:
运行时不自动显示软件窗口只需修改 在窗口初始化时 注释掉 this.setVisible(true); 即可:
public SnapShoot() {
initUI();
initLayout();
createAction();
addSystemTray();
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setIconImage(logo);
this.setSize(730, 500);
this.setTitle("SnapShoot");
this.setFocusable(true);
this.setLocationRelativeTo(null); //居中
// this.setVisible(true);
}
功能三: 增加系统热键功能,即:不管程序当前有没有获得焦点,均可以保持键盘快捷键的监听,实现快捷功能
这个就真的比较难搞啦。 Java AWT & Swing 体系的键盘监听,前提条件是,被监听的组件必须先获得焦点。
比如:窗口最小化后,由于此时窗口失去焦点,这时也就监听不到键盘事件,不能响应操作了。
要让程序在失去焦点的时候也响应事件操作,能想到的就是 跟QQ一样跟操作系统绑定一个系统热键了。
这个功能用 纯Java 是无法实现的。 需要用到 JNI 调用本地方法去绑定系统热键。
对于 C++ 我是很久没搞了。 忘差不多了。幸运的是,我在网上找到了一个 绑定系统热键的 开源项目:JIntellitype
项目地址: http://melloware.com/products/jintellitype/index.html
该项目对 JNI 操作进行的底层封装。 使用也非常简单, 直接看 她的 Quick Start 的例子就学的差不多了。
本例相关代码:
private void addSystemHotKey() {
JIntellitype.getInstance();
//检查是否已经有该程序在运行中
if(JIntellitype.checkInstanceAlreadyRunning("SnapShoot")) {
System.err.println("An instance of this application is already running");
System.exit(1);
}
//注册系统热键为:ctrl + shift + p 截图,ctrl + shift + s 保存,ctrl + shift + o 打开主界面(为了不干扰其他程序的打印和保存快捷键)
JIntellitype.getInstance().registerHotKey(1, JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT, (int)'P');
JIntellitype.getInstance().registerHotKey(2, JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT, (int)'S');
JIntellitype.getInstance().registerHotKey(3, JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT, (int)'O');
JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {
public void onHotKey(int identifier) {
if(identifier == 1) {
printScreen();
} else if(identifier == 2) {
save();
} else if(identifier == 3) {
SnapShoot.this.setVisible(true);
}
}
});
}
这样这三个功能就完成了。
同时增加了 软件小图标;
将主界面中的 截图Button 响应修改为:不隐藏主界面。
想要隐藏主界面截图,直接最小化到托盘后,使用热键 Ctrl + Shift + P 即可。
软件提供三个热键:
Ctrl + Shift + P 截图
Ctrl + Shift + S 保存
Ctrl + Shift + O 打开主界面
退出软件 请 右击托盘图标,选择“exit"
由于软件用到了 JNI , 所以需要两个 DLL 文件支持。
直接运行请下载 附件:SnapShoot.zip 解压后,在SnapShoot 文件夹中双击 SnapShoot.jar
注意:软件运行,由于没有自动显示软件主界面,所以只会在系统托盘部分,增加一个图标。 看到图标后,就可使用热键了。
项目源码在附件:SnapShoot.jar