如何自定义App Designer图形的背景?

时间:2017-01-11 22:55:19

标签: matlab customization matlab-gui undocumented-behavior matlab-app-designer

我想附加徽标或更改App Designer uifigure的整个背景。怎么办呢?

2 个答案:

答案 0 :(得分:0)

  • 如果您想为整个数字设置纯色背景,则存在a documented way来执行此操作,例如:

    % When creating a new uifigure:
    fig = uifigure('Color',[R G B])
    % if the uifigure already exists:
    fig.Color = [R G B];
    
  • 如果您想更改某个地区的背景颜色,您可以添加没有标题或边框的uipaneluipanel(...,'BorderType','none','Title','','BackgroundColor',[R G B]))。
  • 如果您想将图片设置为整个图形的背景

    function q41602238a
    %% Turn off some warnings:
    warning off Matlab:structOnObject
    warning off Matlab:HandleGraphics:ObsoletedProperty:JavaFrame
    
    %% 0. Create a uifigure:
    app = uifigure();
    %% 1. Get a handle to the webwindow:
    while true
      try   
         win = struct(struct(app).Controller).Container.CEF;
         break
      catch
         pause(0.1); % Give the figure (webpage) some more time to load
      end
    end 
    %% 2. Find the data_tag of the DOM element we want to edit:
    data_tag = char(struct(app).Controller.ProxyView.PeerNode.getId);
    
    %% 3. Manipulate the DOM via a JS command
    while true
      try
        win.executeJS(['dojo.style(dojo.query("[data-tag^=''' data_tag ''']")[0],"background-image","url(https://upload.wikimedia.org/wikipedia/commons/8/80/Wikipedia-logo-v2.svg")']);
        break
      catch
        pause(0.1); % Maybe JS is still not ready.
      end
    end
    

    结果:

    Full-BG

  • 如果您想将图片设置为某个区域的背景

    function q41602238b
    %% Turn off some warnings:
    warning off Matlab:structOnObject
    warning off Matlab:HandleGraphics:ObsoletedProperty:JavaFrame
    
    %% 0. Create a some element:
    app = uifigure();
    pnl = uipanel(app);
    %% 1. Get a handle to the webwindow:
    while true
      try   
         win = struct(struct(app).Controller).Container.CEF;
         % disp(win.URL);
         break
      catch
         pause(0.1); % Give the figure (webpage) some more time to load
      end
    end 
    %% 2. Find the id of the DOM element we want to edit:
    data_tag = char(struct(pnl).Controller.ProxyView.PeerNode.getId);
    widgetId = win.executeJS(['dojo.getAttr(dojo.query("[data-tag^=''' data_tag ''']")[0],"widgetid")']);
    
    %% 3. Manipulate the DOM via a JS command
    dojo_style_prefix = ['dojo.style(dojo.query("#' widgetId(2:end-1) '")[0],'];
    while true
      try
        win.executeJS([dojo_style_prefix '"background-image","url(https://upload.wikimedia.org/wikipedia/commons/8/80/Wikipedia-logo-v2.svg")']);
    
        break
      catch
        pause(0.1); % Maybe JS is still not ready.
      end
    end
    

    结果:

    Panel BG

注意:

  1. 最后两个示例基于这两个帖子:12,操作原则是向background-image: "..."添加style条目某些所需UI元素的属性(恰好是HTML div)。

  2. 可以在this GitHub存储库中找到用于编程操作App Designer图形的工具。

  3. 示例图像恰好是.svg,这很有意思,因为我们可以以这种格式导出“常规” MATLAB图形,然后将它们用作背景uifigure:)

答案 1 :(得分:0)

不幸的是我还没有评论,所以这是另一个答案。

从Matlab 2017a开始,Controller不再具有Container属性。这有效:

warning off Matlab:structOnObject
warning off Matlab:HandleGraphics:ObsoletedProperty:JavaFrame

win = struct(struct(struct(app).Controller).PlatformHost).CEF;

data_tag = char(struct(app).Controller.ProxyView.PeerNode.getId);

win.executeJS(['dojo.style(dojo.query("[data-tag^=''' data_tag ''']")[0],"background-image","url(https://upload.wikimedia.org/wikipedia/commons/8/80/Wikipedia-logo-v2.svg")']);

还可以使用

查找所有活动的网页窗口
webWindows = matlab.internal.webwindowmanager.instance.findAllWebwindows();

不幸的是我还没找到,哪个窗口属于哪个UIFigure(你可以使用Title或Position来过滤,但是两个相同的UIFigures会导致问题)。

免责声明,Davide Miani在此发布了这些信息:https://undocumentedmatlab.com/blog/customizing-uifigures-part-1#comment-406524