且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在全屏应用程序中防止屏幕截图(打印屏幕)

更新时间:2022-12-21 11:16:41

您不能阻止它,但是可以在一定程度上尝试覆盖它.
这个想法是检测按下一个打印屏幕,隐藏要隐藏的内容,然后调用另一个打印屏幕,以覆盖前一个屏幕.
这不是很可靠,当然也有局限性,可以绕开它,但是可以为您提供基本控制.
这个swing基本演示已经在我的Windows机器上进行了测试:

You can not prevent it, but you can try, to a certain extent, overwrite it.
The idea is detect that a print screen was pressed, hide what you want to hide, and invoke another print screen, overwriting the previous one.
This is not robust, and of course has limitations and can be by-passed, but can give you basic control.
This swing basic demonstration was tested on my windows machine:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Robot;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NoPrintScreen extends JFrame{

    public final String RED_PAGE = "red page";
    public final String WHITE_PAGE = "white page";
    private CardLayout cLayout;
    private JPanel mainPane;
    boolean isRedPaneVisible = false;

    public NoPrintScreen(){

        setTitle("No Print Screen");
        setSize(400,250);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addKeyListener(new MyKeyListener());
        setFocusable(true);

        mainPane = new JPanel();
        cLayout = new CardLayout();
        mainPane.setLayout(cLayout);

        JPanel whitePane = new JPanel();
        whitePane.setBackground(Color.WHITE);
        whitePane.add(new JLabel("Hit Prtint Screen and check resuts"));

        JPanel redPane = new JPanel();
        redPane.setBackground(Color.RED);
        redPane.add(new JLabel("Print Screen is discouraged"));

        mainPane.add(WHITE_PAGE, whitePane);
        mainPane.add(RED_PAGE, redPane);
        add(mainPane,BorderLayout.CENTER);
        switchPanes();
        setVisible(true);
    }

    void switchPanes() {

        if (isRedPaneVisible) {showRedPane();}
        else { showWhitePane();}
    }

    void showWhitePane() {
        cLayout.show(mainPane, WHITE_PAGE);
        isRedPaneVisible = true;
    }

    void showRedPane() {
        cLayout.show(mainPane, RED_PAGE);
        isRedPaneVisible = false;
    }

    public static void main(String[] args) {
        new NoPrintScreen();
    }

    class MyKeyListener extends KeyAdapter {

        @Override
        public void keyReleased(KeyEvent e) {

            if(KeyEvent.VK_PRINTSCREEN== e.getKeyCode()) {

                switchPanes();

                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {

                        try {

                            final Robot robot = new Robot(); //invoke new print screen
                            robot.keyPress(KeyEvent.VK_PRINTSCREEN);

                        } catch (AWTException  ex) { ex.printStackTrace();  }
                    }
                });

            }
        }
    }
}

此快速代码的目的是演示这个想法.

The purpose of this quick-code is to demonstrate the idea.