首页 文章

MATLAB:自动调整GUI组件/字体的大小

提问于
浏览
11

我在尝试制作MATLAB GUI时出现问题 automatically resizeable . 在彻底搜索网络寻求帮助和大量测试之后,我找不到解决方案 .

我一直在我的笔记本电脑( Screen size/resolution = 1366x768 )中开发一个简单的GUI(使用MATLAB,不使用GUIDE) . 一个非常简化的版本如下所示:

当我在台式计算机( Screen size/resolution = 1920x1080 )中运行相同的GUI时,它以下列方式显示:

考虑到屏幕尺寸( the code is provided in the bottom of this post ),GUI的尺寸会自动初始化 . 如您所见(红色箭头突出显示),组件之间的字体/间距不会自动调整大小,因此无论我们在何处运行文件,GUI都具有相同的方面 .

此外,当手动调整GUI大小时,会出现一些组件重叠:


用于此最小工作示例的代码如下:

function resizingGUIexample()

%% SET UP GUI
hdl.mainfig = figure(); 

% MANAGE FIGURE DIMENSIONS -------------------------------------------------------------------------------------
set(hdl.mainfig, 'Units', 'pixels');
dims              = get(0, 'ScreenSize');
screenHeight      = dims(4);
verticalMargins   = floor((0.2*screenHeight)/2);          % =10% of the screen height in each side
figureHeight      =       (0.8*screenHeight);
figureWidth       =       (0.8*screenHeight)*(4/3);       % 4/3 Aspect Ratio
set(hdl.mainfig, 'Position', [0, verticalMargins, ... 
                figureWidth, figureHeight]);

movegui(hdl.mainfig,'center')     % move GUI to center

color = get(hdl.mainfig,'Color'); % get background color to hide static texts, etc...

% AXES ---------------------------------------------------------------------------------------------------------
hdl.axes = axes('Parent',   hdl.mainfig,  ...
             'Units',   'Normalized', ...
          'Position',   [0.295 0.05 0.63 0.63*(4/3)]);

% PUSH BUTTONS -------------------------------------------------------------------------------------------------
hdl.donePB = uicontrol(hdl.mainfig,                          ...
                  'Position',   [0.85 0.91 0.075 0.075], ...
                    'String',   'Done',                  ...
                  'Fontsize',   16,                      ...
                     'Units',   'normalized',            ...
                'FontWeight',   'Bold');

% BUTTON GROUP and RADIO BUTTONS -------------------------------------------------------------------------------
hdl.buttonGroup = uibuttongroup('Parent',    hdl.mainfig,  ...
                          'FontSize',    16,           ...
                        'FontWeight',    'Bold',       ...
                   'BackgroundColor',    color,        ...
                             'Units',    'Normalized', ... 
                          'Position',    [0.05 0.69 0.2 0.2]);
titleBG = sprintf('Intensity\nNormalization');
set(hdl.buttonGroup, 'Title', titleBG);

hdl.VolumeRB = uicontrol(hdl.buttonGroup,                   ...
                             'Style',    'radiobutton', ...
                            'String',    'Volume',      ...
                          'FontSize',    14,            ...
                        'FontWeight',    'Bold',        ...
                             'Units',    'normalized',  ...
                   'BackgroundColor',    color,         ...
                          'Position',    [0.1 0.67 0.8 0.3]);

hdl.SliceRB = uicontrol(hdl.buttonGroup,                   ...
                            'Style',    'radiobutton', ...
                           'String',    'Slice',       ...
                         'FontSize',    14,            ...
                       'FontWeight',    'Bold',        ...
                            'Units',    'normalized',  ...
                  'BackgroundColor',    color,         ...
                         'Position',    [0.1 .25 0.8 0.3]);

end

关于如何解决这些问题的任何想法?

非常感谢提前 .

亲切的问候,

FábioNery

EDIT1: 我也非常愿意接受有关更好地初始化GUI维度和策略的建议,以避免在不同监视器/屏幕分辨率下运行GUI时出现问题 .

4 回答

  • 0

    首先,不使用GUIDE做得好 - 你已经通过了第一次测试:)

    我强烈建议您查看并使用Ben Tordoff的GUI Layout Toolbox . 虽然你可以使用 ResizeFcn 属性做这种事情,但我可以告诉你,使用GUI Layout Toolbox会更容易,它只是为你处理这些东西 .

    管理可以在不同大小和分辨率的不同(可能是多个)监视器上运行的GUI是一件痛苦的事 . 我建议您预先指定一系列您将支持的大小/分辨率,并坚持这一点(即使应用程序发现自己处于不支持的设置时也会出错),而不是试图完全通用 . 如果你必须在一个最低的公分母设置上使一切工作,你可能不得不牺牲我们在更正常的设置 .

    您好像发现了 get(0, 'ScreenSize')movegui 命令 . 想到的其他有用的东西是 get(0, 'MonitorPositions')get(0, 'ScreenPixelsPerInch') ,并使用数字的 OuterPosition 而不是 Position 属性 .

    希望有所帮助!

  • 6

    您是否尝试过使用ResizeFcn命令?您可以使用它来让MATLAB自动调整您的无线电按钮,面板等,以适应您需要的任何尺寸 . 您可以使用此功能使按钮和面板的大小成为GUI尺寸的函数 .

    关于如何使用它有很多关于在线的文献记录 . 例如,这解释了如何将它用于uipanel:http://www.mathworks.com/help/matlab/creating_plots/using-panel-containers-in-figures--uipanels.html#f7-53231

    你也可以输入:

    编辑([docroot'/ techdoc / creating_plots / examples / doc_uipanel1']);

    在你的命令窗口中启动一个示例gui,在这个示例中,他们使用resize函数来调整gui中各种对象的大小 .

  • 0

    uipanel 声明为 uicontrol 的父级是使MATLAB GUI字体大小与屏幕分辨率无关的一种方法 . 在设置 fontsize 之前,您需要将 fontunits 设置为 normalized . 同时将 fontsize 设置为0到1之间的小数值 .

    示例代码如下 .

    hp = uipanel(...);
    uicontrol(hp,'text','fontunits','normalized','fontsize',0.5,...);
    

    这种方法的警告是 fontsize 将与父对象一起缩放,父对象可能与您的应用程序窗口一起缩放 . 在我的应用程序中,这种行为是可取的 .

  • 0

    你可以使用:

    function yourfunction
    scrsz = get(0,'ScreenSize');%Obtem o tamanho do monitor
    
    hFigure = figure(...  %Insere Uma figura para construção da interface
        'NumberTitle','off',...
        'Menubar','none',...
        'Tag','Figure',...
        'Name','You Figure',...
        'Units','pixels',...
        'Resize','on',...
        'Position',[(scrsz(3)-300)/2 (scrsz(4)-600)/2 800 700]);
    
    Button = uicontrol('parent',hFigure,...%Insere objeto do tipo Pushbutton!
        'Style','pushbutton',...
        'String','Processa',...
        'Units','normalized',... 
        'Position',[0.25 0.25 0.5 0.5],...
        'Callback',{@callbackButton}); 
    
    function callbackButton(hObject,eventdata)
    a = 1
    

相关问题