Set uifigure size limits on display with scaling [Win10,R2017b]

25 views (last 30 days)
Hi,
I am trying to set minimal size of my GUI based on uifigure using undocumented Matlab features.
Following this great blog, I get a webwindow handle:
MyFigureHandle = uifigure('Position',[100 100 500 500]);
window = struct(struct(struct(MyFigureHandle).Controller).PlatformHost).CEF;
and then set minimal figure size:
window.setMinSize([450,450])
The problem is, I am running Windows 10 with a 150% display scaling, so the command above sets the minimal window size to [450, 450] unscaled pixels which are [300, 300] scaled pixels. The following sets the correct limits for me:
window.setMinSize([450*1.5,450*1.5])
and indeed does not allow reduction of the window by mouse dragging below [450, 450] scaled pixels.
However, it also enlarges the figure to [650, 650] scaled pixels and blocks any attempts to set the limits below 650 scaled pixes through 'Position' property:
MyFigureHandle.Position = [100 100 450 450]
Error using matlab.internal.webwindow/set.Position (line 833)
The requested new size of the window is less than the specified
minimum size.
so I cannot restore the size programmatically after I set the size limits. Other than that I do not need to change figure size through 'Position' property anywhere in the code since the GUI will be resized by mouse dragging only.
In short, I need to set the minimal figure size on a scaled display without changing the current figure size.
Any advice apart from waiting for Matlab release that properly handles Win10 display scaling?
Thanks!

Accepted Answer

Matthew
Matthew on 4 Oct 2023
I know this is old, but I wanted to share our findings (for R2022a).
First off, it is impossible to "set the minimal figure size on a scaled display without changing the current figure size". You can check this by reading the setMinSize function in C:\Program Files\MATLAB\R2022a\toolbox\matlab\cefclient\+matlab\+internal\+cef\webwindow.m However, in R2022a I am not experiencing the issue where I am unable to set the firgure position to the scaled dimensions.
After much digging and testing to get it working for our program we found this was our best option was to
% Get the current window position
currPosition = fig.Position;
% Get the screen scaling
import java.awt.Toolkit;
% A DPI of 96 is typically considered 100% scaling on Windows
magnification = Toolkit.getDefaultToolkit().getScreenResolution() / 96;
% Get the Chromium Embedded Framework
warning off MATLAB:structOnObject
warning off MATLAB:ui:javaframe:PropertyToBeRemoved
window = struct(struct(struct(fig).Controller).PlatformHost).CEF;
warning on MATLAB:structOnObject
warning on MATLAB:ui:javaframe:PropertyToBeRemoved
% Set the figure limits
window.setMinSize([minWindowWidth * magnification, minWindowHeight * magnification]);
window.setMaxSize([maxWindowWidth * magnification, maxWindowHeight * magnification]);
% Then just set the figure position
fig.Position = currPosition;
% Alternatively, if you want the window maximized, you need to use the window method rather than
% fig.WindowState due to MATLAB callback delays
window.maximize();
Note: the setMinSize function call will automatically resize the window. This resize does not account for magnification. However, any time that you manually drag the window afterwards, zoom is accounted for. Basically what this means is that when you setMinSize, it will resize the window to your minimum size times the zoom factor. But the real minimum size will be correct as your manually drag the window size, using the Position property, or maximize the window.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!