⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jxcapturedemo.java

📁 Java版本的屏幕截图 工具,可以自己放到系统托盘使用
💻 JAVA
字号:
/*
 * Copyright (c) 2002-2008 TeamDev Ltd. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * The complete licence text can be found at
 * http://www.teamdev.com/winpack/license.jsf
 */
package teamdev.jxcapture.samples.demo;

import teamdev.jxdesktop.Keyboard;
import teamdev.jxdesktop.PlatformContext;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author Ikryanov Vladimir
 */
public class JxCaptureDemo implements Runnable {
    private teamdev.jxdesktop.win32.shell.TrayIcon trayIcon;
    private InputStream inputStream;

    private Keyboard keyboard;
    private KeyListener keyboardListener = new KeyListener();
    private ResourceBundle resource = ApplicationSettings.getResourceBundle();

    public void run() {
        ApplicationSettings.getInstance().loadSettings();

        if (PlatformContext.isWindows()) {
            String messageTitle = resource.getString("TrayIcon.Message.Title");
            String messageText = resource.getString("TrayIcon.Message.Message");
            String tooltip = resource.getString("TrayIcon.ToolTip.Text");

            TrayPopupMenu popupMenu = new TrayPopupMenu(this);
            trayIcon = new teamdev.jxdesktop.win32.shell.TrayIcon(getTrayIcon());
            trayIcon.setToolTip(tooltip);
            trayIcon.setPopupMenu(popupMenu);
            trayIcon.setVisible(true);
            trayIcon.showMessage(new teamdev.jxdesktop.win32.shell.TrayMessage.Info(messageTitle, messageText));

            keyboard = Keyboard.createKeyboard();
            keyboard.addKeyListener(keyboardListener);
        } else {
            showApplicationWindow();
        }
    }

    /**
     * If System tray is not supported the method display window with the same functionality.
     */
    private void showApplicationWindow() {
        ApplicationWindow window = new ApplicationWindow(this);
        window.setVisible(true);
        window.toFront();
    }

    private teamdev.jxdesktop.win32.gdi.Icon getTrayIcon() {
        URL resource = JxCaptureDemo.class.getResource("resources/images/JxCapture.ico");
        try {
            inputStream = resource.openStream();
            return new teamdev.jxdesktop.win32.gdi.Icon(inputStream);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void close() {
        if (!canExit()) return;

        ApplicationSettings.getInstance().saveSettings();

        if (PlatformContext.isWindows()) {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            trayIcon.setVisible(false);
            trayIcon.dispose();

            keyboard.removeKeyListener(keyboardListener);
            keyboard.dispose();
        }

        System.exit(0);
    }

    private boolean canExit() {
        List activeViewers = CaptureOperations.getInstance().getActiveViewers();
        for (int i = 0; i < activeViewers.size(); i++) {
            ImageViewer activeViewer = (ImageViewer) activeViewers.get(i);
            if (!activeViewer.close()) return false;
        }
        return true;
    }

    private class KeyListener extends KeyAdapter {
        private CaptureOperations operations = CaptureOperations.getInstance();

        public void keyPressed(KeyEvent e) {
            if (e.isControlDown() && e.isShiftDown() && !operations.isBusy()) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_A:
                        operations.activeWindowCapture();
                        break;
                    case KeyEvent.VK_W:
                        operations.objectCapture();
                        break;
                    case KeyEvent.VK_R:
                        operations.regionCapture();
                        break;
                    case KeyEvent.VK_F:
                        operations.desktopCapture();
                }
            }
        }
    }

    static {
        if (PlatformContext.isMacOS()) {
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", "JxCapture Demo");
            System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
            System.setProperty("apple.laf.useScreenMenuBar", "true");
        }
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new JxCaptureDemo());
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -