Normalize app designer window position

60 次查看(过去 30 天)
I have an application I made in app designer that has the line app.UIFigure.WindowState = 'maximized'. The UIFigure has a default position of [1 1 1920 1080] and fits to my screen very nicely and maximizes very well. However, it's only working for my display; on anyone elses it does not scale correctly. The matlab application I am using is located here. The "normalized" property is not yet supported in app designer so I have to use the pixel units. Whenever I change the size of the UIFigure programmatically it does not resize the children (yes I have resize children on). I've tried the following:
d=get(0,'screensize');
app.UIFigure.Position = [1 1 d(3) d(2)];
However, this isn't changing the size of the children recursively. I even tried scaling it like this:
d = get(0,'screensize');
app.UIFigure.Position = [1 1 d(3)*app.UIFigure.Position(3)/1920 d(4)*app.UIFigure.Position(4)/1080];
The above works for some elements (not UIFigure), but I have too many to do that for each one. I also tried making a recursive function. I dont remember exactly, but it was something like this:
d = get(0,'screensize');
updatePosition(app.UIFigure)
function updatePosition(R)
if ~isempty(R.Children)
R.Children.Position = [1 1 d(3)*R.Children.Position(3)/1920 d(4)*R.Children.Position(4)/1080];
updatePosition(R.Children)
end
end
Nothing is working. App designer needs some serious TLC. Does anybody know of a way to get the app to size appropriately for different screen sizes and resolutions (short of a startup fnc that changes the position of every single element)?
  1 个评论
Chris Portal
Chris Portal 2018-12-3
Do you have screenshots that shows what it looks like on your display vs someone elses, just to understand what you mean by “does not scale correctly”?

请先登录,再进行评论。

回答(5 个)

Jorge Barrasa Fano
The line:
movegui(app.UIFigure,"center");
at the beginning of startupFcn works fine for me. No need to do pause() and it avoids playing with window coordinates.
  1 个评论
Andrew Diamond
Andrew Diamond 2020-3-27
编辑:Andrew Diamond 2020-3-27
This doesn't rescale the children for me. On a display smaller than the application size, It will move the app and seems to resize the figure to fit but not the children. I have whole gui elements that are simply not available because of this (they get chopped off in the now smaller figure). Resizing the window manually afterwards doesn't do anything to help. The FigureSizeChanged callback does not get called.
It does seem that if I stop in the debugger in the startufcn it will do it but I tried to emulate that with a pause(5) and a drawnow but that didn't work. I guess I could set up a timer to try and resize in a few seconds but it's pretty nasty

请先登录,再进行评论。


Scott Guillaudeu
Scott Guillaudeu 2019-7-22
In startupFcn, try pause(2) before setting app.UIFigure.Position. It's ugly and I don't know exactly why it works, but It seems to take a while to layout the figure the first time and it will only resize all the children after it's done the first layout.
This works for me on a Mac, but I haven't tried any other OSes.

Skander Ayoub
Skander Ayoub 2021-11-25
Try this:
function startupFcn(app)
app.UIFigure.Visible = 'off';
movegui(app.UIFigure,"center")
app.UIFigure.Visible = 'on';
end
  2 个评论
Mike McCullough
Mike McCullough 2024-4-25
Really appreciate this set of commands!
I searched google for 1 hour before finding this...
Marten Claes
Marten Claes 2024-5-10
Is there any way that function startupFcn(app) can be called upon clicking run in appdesigner?

请先登录,再进行评论。


Mekiedje Jöel
Mekiedje Jöel 2019-1-21
Hello, i just have the same problem. Have you solved the problem ?

p su
p su 2023-1-2
All elemetns in the position vector have to be resized (including the x and y).
app.UIFigure.Position = [10, 20, 200, 100];
d = get(0, 'screensize'); % [1, 1, 1920, 1200]
ratio = [d(3)/app.UIFigure.Position(3), d(4)/app.UIFigure.Position(4)];
app.UIFigure.WindowState = 'maximized';
app.Children.Position = app.Children.Position .* [ratio, ratio]; % [x, y, w, h] .* [ratio_w, ratio_h, ratio_w, ratio_h]
I do find out that the above resizing is not perfect, since the ratio was calculated based on the whole screen size, which will include the heights of task bar and title bar and miscalculate the ratio. In order to fixe it, use the maximized UIFigure position instead.
app.UIFigure = uifigure('Visible', 'on');
app.UIFigure.Position = [10, 20, 200, 100];
oldPos = app.UIFigure.Position;
app.UIFigure.WindowState = 'maximized';
drawnow; % Force to draw the uifigure first
newPos = get(app.UIFigure, 'Position'); % [1, 41, 1920, 1137]
ratio = [newPos(3)/oldPos(3), newPos(4)/oldPos(4)];
app.Children.Position = app.Children.Position .* [ratio, ratio]; % [x, y, w, h] .* [ratio_w, ratio_h, ratio_w, ratio_h]

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by