Normalize app designer window position

조회 수: 63 (최근 30일)
Brandon Campanile
Brandon Campanile 2018년 12월 1일
댓글: Mike McCullough 대략 22시간 전
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
Jorge Barrasa Fano 2019년 8월 6일
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
  댓글 수: 1
Mike McCullough
Mike McCullough 대략 22시간 전
Really appreciate this set of commands!
I searched google for 1 hour before finding this...

댓글을 달려면 로그인하십시오.


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