Windows下的Java多显示处理-缩放显示的错误?

问题描述

tl; dr

在Windows 10下,如果我将辅助显示放置在主要显示的右侧,并对辅助显示进行缩放(例如150%),则显示坐标(由Java API返回)会重叠而不是让显示边界并排放置。换句话说,如果我将鼠标从主鼠标的左边缘缓慢移到辅助鼠标的右边缘,则Java的API MouseInfo.getPointerInfo().getLocation()返回从0到1920的递增X位置,然后一旦光标进入第二个屏幕上,该值跳回到1280,然后再次增加到2560。因此对于不同的区域,两次返回1280-1920范围。

文章的结尾,我包含了一个(更新的)演示,使问题显而易见。不要犹豫,尝试并举报。

长版:

本文提供了太多的背景信息,但同时也旨在分享我在搜索主题时学到的东西。

首先,为什么要打扰?因为我正在用Java构建一个屏幕捕获应用程序,所以需要正确处理多显示器配置,包括应用Windows缩放功能显示器。

使用Java API(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()),只要缩放比例为100%,就可以观察到主显示器的左上角在原点(0,0),而其他显示器具有将“ next”协调到主要对象。

以下图片是使用帖子末尾的代码制作的。

例如如果我们有2个全高清显示器,则主显示器的左上角是(0,0),而...

  • 如果次要对象位于同一级别的右侧,则其左上角为(1920,0):

enter image description here

  • 如果辅助节点位于其左侧,位于同一水平,则其左上角为(-1920,0):

enter image description here

  • 如果辅助节点位于下方,并且水平对齐,则其左上角为(0,1080):

enter image description here

  • 如果辅助线圈位于上方并水平对齐,则其左上角为(0,-1080):

enter image description here

  • 依此类推,如果显示未对齐:

enter image description here

  • 或具有不同的分辨率:

enter image description here

但是,如果对辅助显示进行缩放,事情就会出错:似乎缩放因子不仅应用于其尺寸,而且还应用于其原点,该比例更接近(0,0)

如果辅助线圈在左侧,则很有意义。例如,当辅助1920x1080缩放为150%时,它将使逻辑1280x720定位在(-1280,0):

enter image description here

但是,如果次要对象在右侧,则原点也会缩放到(1280,0),越来越接近原点并使它与主要原点“重叠”:

enter image description here

换句话说,如果鼠标位于(1800,0)-参见上方的红点-我无法得知它是否实际上位于第一台显示器的右侧(距右边缘120像素)或在次要对象的左侧(左侧边缘520px)。在这种情况下,将鼠标从主要显示移动到辅助显示时,鼠标的X位置到达主要显示的边界时会“跳回”。

将窗口放置在屏幕上也是如此。如果将对话框的X位置设置为1800,则无法知道对话框的打开位置。

经过大量浏览后,一些答案like this one表示查询Windows缩放比例的唯一方法是使用本机调用。确实,使用JNA,可以得到显示器的物理尺寸(尽管答案似乎表明调用应返回逻辑尺寸)。也就是说,当缩放比例为100%时,JNA调用会忽略缩放因子,其行为与Java API完全一样:

enter image description here

那么我想念什么吗?

不知道缩放比例是一个小问题,但是无法确定鼠标在哪个显示器上,或者无法在我想要的显示器上放置窗口,这对我来说似乎是一个真正的问题。是Java Bug吗?

注意:这是上面使用的应用程序的代码,可在Windows 10 64b上与OpenJDK14一起运行。它显示了Java感知到的显示设置和鼠标位置的缩小版本。如果您在小矩形内单击并拖动,它也可以在实际屏幕上放置和移动一个小对话框。鸣谢:该UI受到here发布的WheresMyMouse代码的启发。

照原样,该代码仅使用Java API。 如果要与JNA进行比较,请搜索标记为“ JNA_ONLY”的4个块,取消注释,然后添加jna库。然后,该演示将在JNA和Java API之间切换,以在每次单击鼠标右键时显示​​屏幕边界和鼠标光标。在此版本中,对话框定位从不使用JNA。

// JNA_ONLY
//import com.sun.jna.platform.win32.User32;
//import com.sun.jna.platform.win32.WinDef;
//import com.sun.jna.platform.win32.WinUser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

/**
 * Java multi-display detection and analysis.
 * UI idea based on WheresMyMouse - https://stackoverflow.com/a/21592711/13551878
 */
public class Showdisplays {

    private static boolean useJna = false;

    public static void main(String[] args) {
        EventQueue.invokelater(() -> {
            JFrame frame = new JFrame("display Configuration");
            frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new TestPane());
            frame.pack();
            frame.setLocationRelativeto(null);
            frame.setVisible(true);
        });
    }

    public static class TestPane extends JPanel {
        private List<Rectangle> screenBounds;
        jdialog dlg;

        public TestPane() {
            screenBounds = getScreenBounds();
            // refresh screen details every second to reflect changes in Windows Preferences in "real time"
            new Timer(1000,e -> screenBounds = getScreenBounds()).start();

            // Refresh mouse position at 25fps
            new Timer(40,e -> repaint()).start();

            MouseAdapter mouseAdapter = new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() != MouseEvent.BUTTON1) {
                        useJna = !useJna;
                        repaint();
                    }
                }

                @Override
                public void mousepressed(MouseEvent e) {
                    System.out.println(e.getButton());
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        if (!dlg.isVisible()) {
                            dlg.setVisible(true);
                        }
                        moveDialogTo(e.getPoint());
                    }
                }


                @Override
                public void mouseDragged(MouseEvent e) {
                    moveDialogTo(e.getPoint());
                }


                private void moveDialogTo(Point mouseLocation) {
                    final Rectangle surroundingRectangle = getSurroundingRectangle(screenBounds);
                    double scaleFactor = Math.min((double) getWidth() / surroundingRectangle.width,(double) getHeight() / surroundingRectangle.height);

                    int xOffset = (getWidth() - (int) (surroundingRectangle.width * scaleFactor)) / 2;
                    int yOffset = (getHeight() - (int) (surroundingRectangle.height * scaleFactor)) / 2;

                    int screenX = surroundingRectangle.x + (int) ((mouseLocation.x - xOffset) / scaleFactor);
                    int screenY = surroundingRectangle.y + (int) ((mouseLocation.y - yOffset) / scaleFactor);

                    dlg.setLocation(screenX - dlg.getWidth() / 2,screenY - dlg.getHeight() / 2);
                }


            };

            addMouseListener(mouseAdapter);
            addMouseMotionListener(mouseAdapter);

            // Prepare the test dialog
            dlg = new jdialog();
            dlg.setTitle("Here");
            dlg.setSize(50,50);
            dlg.setDefaultCloSEOperation(JFrame.HIDE_ON_CLOSE);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400,400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            // Mouse position
            Point mousePoint = getMouseLocation();

            g2d.setColor(Color.BLACK);
            g2d.fillRect(0,getWidth(),getHeight());

            final Rectangle surroundingRectangle = getSurroundingRectangle(screenBounds);
            double scaleFactor = Math.min((double) getWidth() / surroundingRectangle.width,(double) getHeight() / surroundingRectangle.height);

            int xOffset = (getWidth() - (int) (surroundingRectangle.width * scaleFactor)) / 2;
            int yOffset = (getHeight() - (int) (surroundingRectangle.height * scaleFactor)) / 2;

            g2d.setColor(Color.BLUE);
            g2d.fillRect(xOffset,yOffset,(int) (surroundingRectangle.width * scaleFactor),(int) (surroundingRectangle.height * scaleFactor));

            Font defaultFont = g2d.getFont();
            for (int screenIndex = 0; screenIndex < screenBounds.size(); screenIndex++) {
                Rectangle screen = screenBounds.get(screenIndex);
                Rectangle scaledRectangle = new Rectangle(
                        xOffset + (int) ((screen.x - surroundingRectangle.x) * scaleFactor),yOffset + (int) ((screen.y - surroundingRectangle.y) * scaleFactor),(int) (screen.width * scaleFactor),(int) (screen.height * scaleFactor));

                // System.out.println(screen + " x " + scaleFactor + " -> " + scaledRectangle);
                g2d.setColor(Color.DARK_GRAY);
                g2d.fill(scaledRectangle);
                g2d.setColor(Color.GRAY);
                g2d.draw(scaledRectangle);

                // Screen text details
                g2d.setColor(Color.WHITE);

                // display number
                final Font largeFont = new Font(defaultFont.getName(),defaultFont.getStyle(),(int) (screen.height * scaleFactor) / 2);
                g2d.setFont(largeFont);
                String label = String.valueOf(screenIndex + 1);
                FontRenderContext frc = g2d.getFontRenderContext();
                TextLayout layout = new TextLayout(label,largeFont,frc);
                Rectangle2D bounds = layout.getBounds();
                g2d.setColor(Color.WHITE);
                g2d.drawString(
                        label,(int) (scaledRectangle.x + (scaledRectangle.width - bounds.getWidth()) / 2),(int) (scaledRectangle.y + (scaledRectangle.height + bounds.getHeight()) / 2)
                );

                // Resolution + corner
                final Font smallFont = new Font(defaultFont.getName(),(int) (screen.height * scaleFactor) / 10);
                g2d.setFont(smallFont);

                // Resolution
                String resolution = screen.width + "x" + screen.height;
                layout = new TextLayout(resolution,smallFont,frc);
                bounds = layout.getBounds();
                g2d.drawString(
                        resolution,(int) (scaledRectangle.y + scaledRectangle.height - bounds.getHeight())
                );

                // Corner
                String corner = "(" + screen.x + "," + screen.y + ")";
                g2d.drawString(
                        corner,scaledRectangle.x,(int) (scaledRectangle.y + bounds.getHeight() * 1.5)
                );

            }

            g2d.setFont(defaultFont);
            FontMetrics fm = g2d.getFontMetrics();

            if (mousePoint != null) {
                g2d.filloval(xOffset + (int) ((mousePoint.x - surroundingRectangle.x) * scaleFactor) - 2,yOffset + (int) ((mousePoint.y - surroundingRectangle.y) * scaleFactor) - 2,4,4
                );
                g2d.drawString("Mouse pointer is at (" + mousePoint.x + "," + mousePoint.y + ")",fm.getHeight());
            }

            g2d.drawString("Click and drag in this area to move a dialog on the actual screens",fm.getHeight() * 2);

            // JNA_ONLY
            // g2d.drawString("Now using " + (useJna ? "JNA" : "Java API") + ". Right-click to toggle",fm.getHeight() * 3);

            g2d.dispose();
        }
    }

    public static Rectangle getSurroundingRectangle(List<Rectangle> screenRectangles) {
        Rectangle surroundingBounds = null;
        for (Rectangle screenBound : screenRectangles) {
            if (surroundingBounds == null) {
                surroundingBounds = new Rectangle(screenRectangles.get(0));
            }
            else {
                surroundingBounds.add(screenBound);
            }
        }
        return surroundingBounds;
    }

    private static Point getMouseLocation() {
        // JNA_ONLY
//        if (useJna) {
//            final WinDef.POINT point = new WinDef.POINT();
//            if (User32.INSTANCE.GetCursorPos(point)) {
//                return new Point(point.x,point.y);
//            }
//            else {
//                return null;
//            }
//        }
        return MouseInfo.getPointerInfo().getLocation();
    }

    public static List<Rectangle> getScreenBounds() {
        List<Rectangle> screenBounds;

        // JNA_ONLY
//        if (useJna) {
//            screenBounds = new ArrayList<>();
//            // Enumerate all monitors,and call a code block for each of them
//            // See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaymonitors
//            // See http://www.pinvoke.net/default.aspx/user32/EnumdisplayMonitors.html
//            User32.INSTANCE.EnumdisplayMonitors(
//                    null,// => the virtual screen that encompasses all the displays on the desktop.
//                    null,// => don't clip the region
//                    (hmonitor,hdc,rect,lparam) -> {
//                        // For each found monitor,get more @R_669_4045@ion
//                        // See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmonitorinfoa
//                        // See http://www.pinvoke.net/default.aspx/user32/GetMonitorInfo.html
//                        WinUser.MONITORINFOEX monitorInfoEx = new WinUser.MONITORINFOEX();
//                        User32.INSTANCE.GetMonitorInfo(hmonitor,monitorInfoEx);
//                        // Retrieve its coordinates
//                        final WinDef.RECT rcMonitor = monitorInfoEx.rcMonitor;
//                        // And convert them to a Java rectangle,to be added to the list of monitors
//                        screenBounds.add(new Rectangle(rcMonitor.left,rcMonitor.top,rcMonitor.right - rcMonitor.left,rcMonitor.bottom - rcMonitor.top));
//                        // Then return "true" to continue enumeration
//                        return 1;
//                    },//                    null // => No additional info to pass as lparam to the callback
//            );
//            return screenBounds;
//        }

        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices();
        screenBounds = new ArrayList<>(screenDevices.length);
        for (GraphicsDevice screenDevice : screenDevices) {
            GraphicsConfiguration configuration = screenDevice.getDefaultConfiguration();
            screenBounds.add(configuration.getBounds());
        }
        return screenBounds;
    }

}

解决方法

这似乎是您遇到的错误JDK-8211999的体现:

在涉及一个HiDPI屏幕的多显示器设置中,该屏幕位于一个常规显示器的右侧,在Windows 10上,GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[x].getDefaultConfiguration().getBounds()返回的边界是重叠的。这会导致各种次要错误...

评论请注意:

Linux上也存在相同的错误,macOS不受影响。

似乎没有简单的纯Java解决方法。

一种fix has been proposed,它甚至不尝试使用Java进行坐标数学运算,而是将解决方案委托给本机代码,因此适用于Windows。

由于似乎使用JNA(本机)实现似乎可行,因此,在修复此错误之前(可能在JDK 16中),这似乎也是最好的方法。

根据该错误报告,它会影响JDK 9+,因此尽管我看到与此相关的帐户存在冲突,但恢复到JDK 8可能会解决该问题。