【问题标题】:MATLAB: Automatic resizing of GUI components/fontsMATLAB:自动调整 GUI 组件/字体的大小
【发布时间】:2013-04-17 10:33:26
【问题描述】:

我在尝试使我的 MATLAB GUI自动调整大小时遇到问题。 在彻底搜索网络寻求帮助和大量测试后,我找不到解决方案。

我一直在我的笔记本电脑(屏幕尺寸/分辨率 = 1366x768)中开发一个简单的 GUI(使用 MATLAB,不使用 GUIDE)。一个非常简化的版本如下所示:

当我在我的台式计算机上运行相同的 GUI(屏幕尺寸/分辨率 = 1920x1080)时,它显示如下:

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

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

非常感谢。

亲切的问候,

法比奥·内里

EDIT1:我也非常愿意接受有关初始化 GUI 维度的更好方法以及避免在不同显示器/屏幕分辨率下运行 GUI 时出现问题的策略的建议。

【问题讨论】:

    标签: matlab user-interface resize screen-resolution


    【解决方案1】:

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

    我强烈建议您查看并使用 Ben Tordoff 的 GUI Layout Toolbox。虽然您可以使用 ResizeFcn 属性来执行此类操作,但我可以告诉您,使用 GUI 布局工具箱更容易,它只会为您处理这些事情。

    管理一个可以在不同尺寸和分辨率的不同(可能是多个)显示器上运行的 GUI 是一件很痛苦的事情。我建议您预先指定您要支持的一系列尺寸/分辨率,并坚持下去(如果应用程序发现自己处于不受支持的设置中,甚至会出错),而不是试图完全通用。如果即使在最低公分母设置上您也必须让一切正常工作,您可能不得不在更正常的设置上牺牲我们的易用性。

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

    希望有帮助!

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 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 中各种对象的大小。

      【讨论】:

        【解决方案3】:

        uipanel 声明为uicontrol 的父级是使MATLAB GUI 字体大小独立于屏幕分辨率的一种方法。在设置fontsize 之前,您需要将fontunits 设置为normalized。还将fontsize 设置为介于 0 和 1 之间的小数值。

        示例代码如下。

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

        这种方法的注意事项是fontsize 将随父对象缩放,而父对象可能会随您的应用程序窗口缩放。在我的应用程序中,这种行为是可取的。

        【讨论】:

          【解决方案4】:

          你可以使用:

          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
          

          【讨论】:

            猜你喜欢
            • 2012-07-18
            • 2017-03-26
            • 1970-01-01
            • 1970-01-01
            • 2011-02-28
            • 2011-02-07
            • 2011-01-03
            • 2011-05-23
            • 2021-11-24
            相关资源
            最近更新 更多