How can I set a minimum window size for an app developed in app designer?

43 ビュー (過去 30 日間)
Erika Yoshikawa
Erika Yoshikawa 2021 年 7 月 26 日
編集済み: Fryderyk Kukowski 2024 年 1 月 18 日
I am currently working on a app in app designer and I've been using the SizeChangedFcn call back to code for resizing the components. I want to set a minimum window size so it cannot be resized smaller than a certain size. I've been trying methods such as the one mentioned here (https://www.mathworks.com/matlabcentral/answers/361224-set-uifigure-size-limits-on-display-with-scaling-win10-r2017b) but it's not working, If someone knows how to do this, please can you help me?

採用された回答

Adam Danz
Adam Danz 2021 年 7 月 26 日
編集済み: Adam Danz 2023 年 6 月 12 日
  1. Set the minimum size as an app property named minSize defined by 1x2 vector describing the minimum [width, height] (see how to define an app property). Example: minSize = [400, 300];
  2. Set the SizeChangedFcn to the two lines below. The second line assures that the App stays on the screen.
Don't forget that AutoResizeChildren needs to be set to off to use SizeChangedFcn.
function UIFigureSizeChanged(app, event)
app.UIFigure.Position(3:4) = max(app.UIFigure.Position(3:4), minSize);
movegui(app.UIFigure)
end
  4 件のコメント
Erika Yoshikawa
Erika Yoshikawa 2021 年 7 月 27 日
No problem, thank you for your help!
Fryderyk Kukowski
Fryderyk Kukowski 2024 年 1 月 18 日
@Adam Danz, are there any plans on adding this feature? The workaround you proposed doesn't look very good. Disabling Resize all together is also not so good option.
The main problem why it doesn't look very good is that when user resizes app to smaller size than minimal, the programatic resizing doesn't force them to let go the corner or border of app's window and it looks very glitchy.

サインインしてコメントする。

その他の回答 (1 件)

Fryderyk Kukowski
Fryderyk Kukowski 2024 年 1 月 18 日
編集済み: Fryderyk Kukowski 2024 年 1 月 18 日
Actually I've found a way of doing it (setting minimum window size without the glichiness (see my comment above)):
properties (Access = private)
mouseRelease
minWidth = 1600;
minHeight = 700;
lastPos
end
function startupFcn(app)
import java.awt.*;
import java.awt.event.*;
rob = Robot;
app.mouseRelease = @() rob.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
app.lastPos = app.UIFigure.Position;
end
function UIFigureSizeChanged(app, event)
pos = app.UIFigure.Position;
if pos(3) < app.minWidth || pos(4) < app.minHeight
app.mouseRelease();
app.UIFigure.Position(1:2) = app.lastPos(1:2);
if pos(3) < app.minWidth
app.UIFigure.Position(3) = app.minWidth;
end
if pos(4) < app.minHeight
app.UIFigure.Position(4) = app.minHeight;
end
else
app.lastPos = pos;
end
end
Your startupFcn and SizeChanged functions should look like that. You should also add mouseRelease property to your app.
Its not perfect but its better than figting with the user over the size of UIFigure.
Edit: It now works almost as well as native minimum size restriction.

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by