【问题标题】:How to customize the background of an App Designer figure?如何自定义 App Designer 人物的背景?
【发布时间】:2017-01-11 22:55:18
【问题描述】:

我想附加一个徽标或更改 App Designer uifigure 的整个背景。如何做到这一点?

【问题讨论】:

  • app designer 做一个标签也是个好主意。
  • @hbaderts tnx 用于制作标签

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


【解决方案1】:
  • 如果你想为整个图形设置一个纯色背景色,有a documented way可以做到这一点,例如:

    % When creating a new uifigure:
    fig = uifigure('Color',[R G B])
    % if the uifigure already exists:
    fig.Color = [R G B];
    
  • 如果您想更改仅部分区域的背景颜色,您可以添加不带标题或边框的uipanel (uipanel(...,'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
    

    结果:

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

    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
    

    结果:

注意事项:

  1. 最后两个例子是基于这两个帖子:12,操作原理是在一些需要的 UI 元素的style 属性中添加一个background-image: "..." 条目(其中恰好是 HTML div)。

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

  3. 示例图像恰好是.svg,这很有趣,因为我们可以以这种格式导出 "regular" MATLAB 图形,然后将它们用作@ 的背景987654338@ :)

【讨论】:

    【解决方案2】:

    很遗憾,我还不能发表评论,所以这里有另一个答案。

    从 Matlab 2017a 开始,控制器不再具有 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 进行过滤,但是两个相同的 UIFigure 会导致问题)。

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

    【讨论】:

      猜你喜欢
      • 2016-12-20
      • 2010-10-19
      • 2023-04-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2010-09-21
      相关资源
      最近更新 更多