Windows alpha compositing XOR mode

57 views Asked by At

I want to draw Text directly on a windows screen. The text should be erasable from a screenshot taken from that screen.

Here's an example to give you an idea:

    static final Color color = new Color(1, 1, 1, (float) .6f);
    static float opacity = .6f;
    static Color COL =  Color.BLACK;

    private void showOverlayWindow() {
        final Window w = new Window(null) {
            public void paint(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setXORMode(color);
                g2d.setColor(COL);
                g2d.fillRect(0, 0, 300, 100);
                //g2d.fillRect(0, 0, 300, 100);
            }
            public Dimension getPreferredSize() {
                final Dimension dimension = new Dimension(width,height);
                return dimension;
            }
        };

        w.pack();
        w.setBounds(0, 0, width, height);
        w.setLocationRelativeTo(null);
        w.setAlwaysOnTop(true);
        w.setOpacity(opacity);
                w.setBackground(color);
        setClickThrough(w);

        w.setVisible(true);
    }
    private  void setClickThrough(Component w) {
        overlayWindow = getHWnd(w);
        int wl = User32.INSTANCE.GetWindowLong(overlayWindow, WinUser.GWL_EXSTYLE);
        wl = wl | WinUser.WS_EX_LAYERED | WinUser.WS_EX_TRANSPARENT;
        User32.INSTANCE.SetWindowLong(overlayWindow, WinUser.GWL_EXSTYLE, wl);
    }

    private  HWND getHWnd(Component w) {
        HWND hwnd = new HWND();
        hwnd.setPointer(Native.getComponentPointer(w));
        return hwnd;
    }

Of course, this example doesn't work.

The second g2d.fillRect() will erase the previous operation, but it doesn't take into account the bits under the overlay window.

For this I have to give the overlay window the Porter/Duff composite operation mode XOR (see https://www.cairographics.org/operators/).

How do I do this in Windows (Win10)?

0

There are 0 answers