Working with non-standard DPI displays

With high-density displays becoming increasingly popular, some users set their display’s DPI to a higher-than-standard (i.e., >100%) value, in order to compensate for the increased pixel density to achieve readable interfaces. This OS setting tells the running applications that there are fewer visible screen pixels, and these are spread over a larger number of physical pixels. This works well for most cases (at least on recent OSes, it was a bit buggy in non-recet ones). Unfortunately, in some cases we might actually want to know the screen size in physical, rather than logical, pixels. Apparently, Matlab root’s ScreenSize property only reports the logical (scaled) pixel size, not the physical (unscaled) one:

>> get(0,'ScreenSize')   % with 100% DPI (unscaled standard)
ans =
        1       1      1366       768
 
>> get(0,'ScreenSize')   % with 125% DPI (scaled)
ans =
        1       1      1092.8     614.4

The same phenomenon also affects other related properties, for example MonitorPositions.

Raimund Schlüßler, a reader on this blog, was kind enough to point me to this problem and its workaround, which I thought worthy to share here: To get the physical screen-size, use the following builtin Java command:

>> jScreenSize = java.awt.Toolkit.getDefaultToolkit.getScreenSize
jScreenSize =
java.awt.Dimension[width=1366,height=768]
 
>> width = jScreenSize.getWidth
width =
        1366
 
>> height = jScreenSize.getHeight
height =
        768

Upcoming travels – London/Belfast, Zürich & Geneva

I will shortly be traveling to consult some clients in Belfast (via London), Zürich and Geneva. If you are in the area and wish to meet me to discuss how I could bring value to your work, then please email me (altmany at gmail):

  • Belfast: Nov 28 – Dec 1 (flying via London)
  • Zürich: Dec 11-12
  • Geneva: Dec 13-15
Categories: Desktop, Figure window, GUI, Java, Low risk of breaking in future versions, Undocumented feature

Tags: , ,

Bookmark and SharePrint Print

6 Responses to Working with non-standard DPI displays

  1. Thomas Pieper says:

    If you feel more as a Microsoft family member you might want to use .NET commands. Try

    NET.addAssembly('System.Windows.Forms');
    System.Windows.Forms.Screen.PrimaryScreen.Bounds

    or

    System.Windows.Forms.SystemInformation.PrimaryMonitorSize

    and you get something like

    ans = 
     
      System.Drawing.Rectangle
      Package: System.Drawing
     
      Properties:
        Location: [1x1 System.Drawing.Point]
            Size: [1x1 System.Drawing.Size]
               X: 0
               Y: 0
           Width: 1920
          Height: 1080
            Left: 0
             Top: 0
           Right: 1920
          Bottom: 1080
         IsEmpty: 0
           Empty: [1x1 System.Drawing.Rectangle]

    Greetings,

    Thomas

    • @Thomas – the problem with your .NET solution is that it only works on Windows; the Java-based solution in my post works on all Matlab platforms, including Mac and Linux.

  2. Matthias Kirchhoff says:

    As for me the solution using java.awt.Toolkit.getDefaultToolkit.getScreenSize does not work correctly when using more than one monitor.
    Better use:

    ge = javaObjectEDT(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment());
    gs = ge.getScreenDevices();
    jmonitorposition = [];
    if ~isempty(gs)
      for iRun_ = 1:size(gs,1)
         dm = gs(iRun_).getDisplayMode();
         jmonitorposition(iRun_,:) = [1 1 dm.getWidth dm.getHeight];
      end
    end

    Best regards
    Matthias

  3. Yaroslav says:

    Hi Yair,

    Suppose I have two monitors. How can I tell a figure to appear on the secondary one, if it exists? I can do something like

    hFig = figure('Units','normalized','Position',[1.10 0.10 0.80 0.70]);

    but it doesn’t tell me whether the second screen exists or not (if not, it will effectively hide the figure).

    Best regards

    • marco says:

      Hi,
      maybe this can be helpful:

      >> monitorPos = get(0,'MonitorPositions')
      monitorPos =
                 1           1        1600         900
              1601           1        3200         900

      then you can play with ‘pixels’ positions

    • Yaroslav says:

      Hi @Marco,

      Thank you for your comment. Unfortunately, your solution tells me only how many monitors I can have. What I need is to know how many monitors I currently have.

      To be more precise, MonitorPositions tells me how many monitors I had at the time of Matlab’s startup. It does not change if I attach a monitor after startup. This is the behaviour in my Windows 8.1 laptop.

      I can do a little trick to circumvent this problem. By using the PointerLocation, I can check on which monitor the pointer is. For example

      % get root handle
      hRoot = groot;
      % check if (a) there are at least two monitors
      %          (b) pointer's x location is larger than 2nd monitor's x0 position
      if     size(hRoot.MonitorPositions,1) >= 2 ...
          && hRoot.PointerLocation(1) > hRoot.MonitorPositions(2,1)
          % put figure on the second monitor
      else
          % put figure on the first monitor
      end

      However, as you can see, it assumes you have control over the cursor’s position during execution. I wish there was an option to tell how many active monitors are there.

Leave a Reply


Your email address will not be published. Required fields are marked *