calss1=====================
package com.screenCapture;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
public class HookKeyTest extends JFrame implements HotkeyListener {
public static final String sourcePath="C:/screenCaptureDll/";//JIntellitype.dll";
private final SystemTray sysTray = SystemTray.getSystemTray();
private final int identifier=10000;
private final TrayIcon trayIcon = new TrayIcon(new ImageIcon(sourcePath+"sc.PNG").getImage(),"ScreenCapture",getPopMenu());
public HookKeyTest() {
this.setTitle("ScreenCapture");
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
unInstallHotKey();
System.out.println("closing");
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
//JIntellitype
setSize(new Dimension(330, 220));
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
JButton btnRegCtrlalta = new JButton("reg Ctrl+Alt+A");
btnRegCtrlalta.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(btnRegCtrlalta);
JButton btnNewButton = new JButton("unreg Ctrl+Alt+A");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(btnNewButton);
installHotKey();
//setVisible(true);
sysTray.add(trayIcon);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onHotKey(int identifier) {
System.out.println("indentifier==="+identifier);
if(identifier==this.identifier&&!ScreenCapture.isCapture){
new ScreenCapture();
}
}
public static void main(String[] args) {
try {
//if(!isStartUp())
{
new HookKeyTest();
}
//else
{
// JOptionPane.showMessageDialog(null, "An Instance is running!!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public PopupMenu getPopMenu()
{
PopupMenu popupMenu = new PopupMenu();// 弹出菜单
//MenuItem mi = new MenuItem("open");
MenuItem exit = new MenuItem("exit");
popupMenu.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
unInstallHotKey();
System.exit(0);
}
});
return popupMenu;
}
private void installHotKey(){
JIntellitype t=JIntellitype.getInstance();
t.getInstance().addHotKeyListener(this);
t.registerHotKey(identifier,JIntellitype.MOD_CONTROL+JIntellitype.MOD_ALT,'A');
System.out.println("install hot key!");
}
private void unInstallHotKey(){
JIntellitype t=JIntellitype.getInstance();
t.unregisterHotKey(identifier);
System.out.println("unInstall hot key!");
}
private static boolean isStartUp(){
JIntellitype t=JIntellitype.getInstance();
return t.checkInstanceAlreadyRunning("ScreenCapture");
}
}
calss2=====================
package com.screenCapture;
import java.awt.AWTEvent;
public class ScreenCapture extends JDialog{
public static boolean isCapture=false;
ImageLabel imgLabel;
int xStart=0;
int yStart=0;
int subimageX=0;
int subimageY=0;
int subimageW=0;
int subimageH=0;
private BufferedImage fullScreenImage=null;
public static void main(String[] args) {
ScreenCapture sc=null;
try {
Thread.sleep(300);
sc=new ScreenCapture();
} catch (Exception e) {
JOptionPane.showMessageDialog(sc, e.getMessage());
e.printStackTrace();
}
}
public ScreenCapture() {
isCapture=true;
setUndecorated(true);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setAlwaysOnTop(true);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setModal(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
JPanel imgPanel = new JPanel();
getContentPane().add(imgPanel, BorderLayout.CENTER);
imgPanel.setLayout(new BorderLayout(0, 0));
imgLabel = new ImageLabel();
imgPanel.add(imgLabel, BorderLayout.CENTER);
//===============
imgLabel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent paramMouseEvent) {
xStart=paramMouseEvent.getX();
yStart=paramMouseEvent.getY();
}
public void mouseReleased(MouseEvent paramMouseEvent) {
System.out.println("DDDDDDD");
if(paramMouseEvent.getButton()==3){
//System.exit(0);
destroyMe();
}
if(subimageY>0&&subimageX>0){
BufferedImage pickedImage = fullScreenImage.getSubimage(subimageX, subimageY, subimageW,
subimageH);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
MyImageTransferable it = new MyImageTransferable(new ImageIcon(pickedImage));
cb.setContents(it, it);
//System.exit(0);
destroyMe();
}
}
});
imgLabel.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent paramMouseEvent) {
}
public void mouseDragged(MouseEvent paramMouseEvent) {
//paintRectOnImglabel(imgLabel.getGraphics(), paramMouseEvent.getX(),paramMouseEvent.getY());
int w=Math.abs(paramMouseEvent.getX()-xStart);
int h=Math.abs(paramMouseEvent.getY()-yStart);
int x=Math.min(xStart,paramMouseEvent.getX());
int y=Math.min(yStart,paramMouseEvent.getY());
subimageX=x;
subimageY=y;
subimageW=w;
subimageH=h;
imgLabel.drawRectangle(x, y, w, h);
}
});
setLabelIcon(imgLabel);
setVisible(true);
}
private void destroyMe(){
this.setVisible(false);
this.dispose();
isCapture=false;
}
private void setLabelIcon(JLabel label){
try {
if(fullScreenImage==null){
Robot robot =new Robot();
fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize()));
}
ImageIcon icon = new ImageIcon(fullScreenImage);
label.setIcon(icon);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ImageLabel extends JLabel {
int lineX, lineY;
int x, y, h, w;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(x, y, w, h);
String area = Integer.toString(w) + " * " + Integer.toString(h);
g.setColor(Color.blue);
g.drawString(area, x + (int) w / 2 - 15, y + (int) h / 2);
}
public void drawRectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
h = height;
w = width;
repaint();
}
public void drawCross(int x, int y) {
lineX = x;
lineY = y;
repaint();
}
}
class MyImageTransferable implements Transferable,ClipboardOwner{
private ImageIcon image=null;
public MyImageTransferable(ImageIcon img) {
this.image=img;
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("lostOwnership");
}
public Object getTransferData(DataFlavor arg0)
throws UnsupportedFlavorException, IOException {
return this.image.getImage();
}
public DataFlavor[] getTransferDataFlavors() {
DataFlavor []flavor=new DataFlavor[1];
flavor[0]=DataFlavor.imageFlavor;
return flavor;
}
public boolean isDataFlavorSupported(DataFlavor arg0) {
if(arg0.equals(DataFlavor.imageFlavor)){
System.out.println("support impageFlavor");
return true;
}
return false;
}
}
class MyAWTEvent implements AWTEventListener{
@Override
public void eventDispatched(AWTEvent paramAWTEvent) {
System.out.println("MMMMMMMMMMMMSSSSSSSS");
}
}
分享到:
相关推荐
AIR systemTrayIcon sample code how to set systemTrayIcon and how to change the app screen solution
jdic.jar+jdic.dll+tray.dll 全部下载 jdic.jar+jdic.dll+tray.dll 全部下载
一个简单的vb环境中system tray 编程例子.
A framework for system tray applets(32KB)
Applications which adds an item to the system tray.
PS Soft Lab presents tool for handling icons in taskbar's system tray area - PS Tray Factory. PS Tray Factory allows you to: ? move low-activity icons from the system tray into the menu, ? quickly ...
A class for adding icons to the system tray(120KB)
一个简单的记事本++工具,用托盘图标展开它。 它使您可以直接访问以下功能: - 创建新的文本文件。 - 创建新的文本文件并从剪贴板粘贴。 - 直接访问最近使用的文件和目录。
This shows you how to add your icon to the system tray without any third-party controls.
system tray (sort tray), - to restore system tray icons after Explorer.exe crash, - to replace the original tray icons with your own icons, - to quickly access hidden icons, - to hide seldom used...
PS Tray Factory is a powerful and handy tool meant to flexibly control ALL the other icons in the system tray of the taskbar. PS Tray Factory will help you to manage all the applications that place ...
NULL 博文链接:https://syxc.iteye.com/blog/777260
system tray demo BOOL SetIcon(HICON hIcon); BOOL SetIcon(LPCTSTR lpIconName); BOOL SetIcon(UINT nIDResource); BOOL SetStandardIcon(LPCTSTR lpIconName); BOOL SetStandardIcon(UINT nIDResource); ...
VB窗体系统托盘编程设置经典代码VB classic form programmed code system tray
添加图标至系统托盘的类CSyetemTray
使用Qt/QML创建程序小托盘,方便用户操作。实用、简介。
PS Tray Factory v2.0.rar
Delphi7 开发的 系统托盘控件: 由于一个项目用到,今天下午等人,又参考网上关于V5的新东东,整理成一个控件.大家来评评吧.
解压缩即可运行,汉化已注册。 PS Tray Factory The system tray icons management utility
S Tray Factory 是一个系统状态栏图标管理软件,可以将任何状态 栏的图标隐藏起来。很多软件都会往状态栏添加图标,这样使得状态栏变 得拥挤凌乱,这个小工具可以使状态栏重新成为简单而有效。你可以定义 任意一个...