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");
}
}
分享到:
相关推荐
Notepad++ Tray Launcher是一款开源软件,专为提升记事本++(Notepad++)的使用体验而设计。这款工具的独特之处在于它通过托盘图标来启动和管理Notepad++,使得用户可以更方便地进行文本编辑操作。接下来,我们将...
在本文中,我们将深入探讨Adobe Integrated Runtime (AIR) 中的`SystemTrayIcon`功能,以及如何使用它来创建和管理应用程序的系统托盘图标。系统托盘图标是桌面应用程序中一个常见且实用的特性,它允许用户在任务栏...
在本文中,我们将深入探讨Delphi7中的SystemTrayIcon控件及其应用。SystemTrayIcon控件是Delphi开发环境中用于创建系统托盘图标的一个组件,它允许开发者将应用程序的主窗口最小化到任务栏的系统托盘区域,提供更...
标题 "AIR System Tray and Dock Example" 提供了一个关键线索,表明我们要探讨的是关于Adobe Integrated Runtime (AIR)的应用程序,特别是如何在系统托盘或Dock区域实现功能。在计算机用户界面中,系统托盘通常位于...
`SystemTrayIcon`还提供了其他属性和信号,如`message`(显示系统消息)和`activated`(当用户点击图标时触发)。 总之,通过Qt和QML,我们可以轻松地为应用程序创建一个实用且简洁的系统托盘图标,提供方便的快捷...
《关于Java与Windows环境下的字典支持:jdic.jar、jdic.dll与tray.dll的解析》 在信息技术领域,跨平台的开发能力和本地化功能是至关重要的。当我们谈论到"jdic.jar+jdic.dll+tray.dll"时,这涉及到的是Java(JDBC...
标题中的"system tray example"指的是在Visual Basic (VB)编程环境中创建系统托盘图标的应用示例。系统托盘,也称为通知区域,是Windows操作系统任务栏右下角的一个区域,通常用于显示各种程序的图标,如网络连接、...
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 ...
在这个案例中,我们关注的是基于MFC(Microsoft Foundation Classes)和SDK(Software Development Kit)实现的`System Tray`源码。 MFC是微软提供的一个C++类库,用于简化Windows应用程序的开发,它封装了Windows ...
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 ...
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...
system tray demo BOOL SetIcon(HICON hIcon); BOOL SetIcon(LPCTSTR lpIconName); BOOL SetIcon(UINT nIDResource); BOOL SetStandardIcon(LPCTSTR lpIconName); BOOL SetStandardIcon(UINT nIDResource); ...
标题中的"A class for adding icons to the system tray"指的是在计算机操作系统中,特别是在Windows环境下,创建一个能够将图标显示在任务栏系统托盘区域的程序或类。系统托盘区域是位于任务栏右下角的区域,通常...
标题 "A framework for system tray applets(32KB)" 提示我们这可能是一个关于创建系统托盘(也称为通知区域)小程序的框架,且整个框架的大小仅为32KB,这意味着它可能是轻量级且高效的。系统托盘小程序通常是指在...
《PS Tray Factory v2.0》是一款功能强大的系统托盘管理工具,专为优化和整理电脑右下角任务栏中的图标而设计。该软件能够帮助用户有效地管理和控制那些经常出现在系统托盘区域的应用程序,使得桌面环境更加整洁,...
标题中的“Applications which adds an item to the system tray”指的是那些在操作系统任务栏右侧通知区域(通常称为系统托盘或系统 tray)添加图标的程序。这些应用程序通常以图标的形式驻留在系统托盘中,以便...
托盘修改添加右下角弹窗托盘修改添加右下角弹窗托盘修改添加右下角弹窗托盘修改添加右下角弹窗托盘修改添加右下角弹窗托盘修改添加右下角弹窗托盘修改添加右下角弹窗托盘修改添加右下角弹窗托盘修改添加右下角弹窗...
在计算机软件开发中,"程序缩小时缩为系统托盘(system tray)的小图标"是一种常见的设计模式,尤其适用于需要在后台运行的服务类程序。系统托盘,也被称为通知区域,是Windows操作系统桌面任务栏右下角的一个区域,...
在VB(Visual Basic)经典版本中,系统托盘(System Tray)编程是创建应用程序时一个重要的功能,它允许用户在任务栏的右下角图标区域显示和管理程序。本资源包含的是一个关于VB窗体系统托盘编程的经典代码示例,...